From dcce9a8b28e6a98459eff181ea6dddbbe822c0d8 Mon Sep 17 00:00:00 2001 From: FalsePattern Date: Tue, 21 Jan 2025 15:38:54 +0100 Subject: [PATCH] feat: live templates --- CHANGELOG.md | 5 ++ .../zigbrains/zig/codeInsight/ZigContext.kt | 48 +++++++++++++++++++ .../resources/META-INF/zigbrains-core.xml | 5 ++ .../resources/zigbrains/Bundle.properties | 3 +- 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 core/src/main/kotlin/com/falsepattern/zigbrains/zig/codeInsight/ZigContext.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index a8850f3a..5dc2655e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,11 @@ Changelog structure reference: ## [Unreleased] +### Added + +- Zig + - Live template support + ## [20.1.3] ### Added diff --git a/core/src/main/kotlin/com/falsepattern/zigbrains/zig/codeInsight/ZigContext.kt b/core/src/main/kotlin/com/falsepattern/zigbrains/zig/codeInsight/ZigContext.kt new file mode 100644 index 00000000..52068252 --- /dev/null +++ b/core/src/main/kotlin/com/falsepattern/zigbrains/zig/codeInsight/ZigContext.kt @@ -0,0 +1,48 @@ +/* + * This file is part of ZigBrains. + * + * Copyright (C) 2023-2024 FalsePattern + * All Rights Reserved + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * ZigBrains is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, only version 3 of the License. + * + * ZigBrains is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with ZigBrains. If not, see . + */ + +package com.falsepattern.zigbrains.zig.codeInsight + +import com.falsepattern.zigbrains.ZigBrainsBundle +import com.falsepattern.zigbrains.zig.psi.ZigFile +import com.falsepattern.zigbrains.zig.psi.ZigTypes +import com.intellij.codeInsight.template.TemplateActionContext +import com.intellij.codeInsight.template.TemplateContextType +import com.intellij.psi.util.elementType + +class ZigContext: TemplateContextType(ZigBrainsBundle.message("zig")) { + override fun isInContext(templateActionContext: TemplateActionContext): Boolean { + val file = templateActionContext.file + val offset = templateActionContext.startOffset + + if (file !is ZigFile) + return false + + val element = file.findElementAt(offset) ?: return true + + return when (element.elementType) { + ZigTypes.LINE_COMMENT, ZigTypes.CONTAINER_DOC_COMMENT, ZigTypes.DOC_COMMENT -> false + ZigTypes.STRING_LITERAL_SINGLE, ZigTypes.STRING_LITERAL_MULTI -> false + else -> true + } + } +} \ No newline at end of file diff --git a/core/src/main/resources/META-INF/zigbrains-core.xml b/core/src/main/resources/META-INF/zigbrains-core.xml index 12dd4b6c..e790d200 100644 --- a/core/src/main/resources/META-INF/zigbrains-core.xml +++ b/core/src/main/resources/META-INF/zigbrains-core.xml @@ -170,6 +170,11 @@ icon="/icons/zig_build_tool.svg" id="zigbrains.build" /> + + diff --git a/core/src/main/resources/zigbrains/Bundle.properties b/core/src/main/resources/zigbrains/Bundle.properties index 9952d628..c40b6893 100644 --- a/core/src/main/resources/zigbrains/Bundle.properties +++ b/core/src/main/resources/zigbrains/Bundle.properties @@ -114,4 +114,5 @@ build.tool.window.status.loading=Running zig build -l build.tool.window.status.error.missing-build-zig=No build.zig file found build.tool.window.status.error.missing-toolchain=No zig toolchain configured build.tool.window.status.error.general=Error while running zig build -l -build.tool.window.status.timeout=zig build -l timed out after {0} seconds. \ No newline at end of file +build.tool.window.status.timeout=zig build -l timed out after {0} seconds. +zig=Zig \ No newline at end of file