From 323ba12fbc57b1afd14a19c20bce16690cfe99d0 Mon Sep 17 00:00:00 2001 From: FalsePattern Date: Sun, 22 Dec 2024 22:55:47 +0100 Subject: [PATCH] backport: 20.1.0 --- CHANGELOG.md | 12 +++++++ core/src/main/grammar/Zig.bnf | 2 ++ core/src/main/grammar/Zig.flex | 12 ++++--- .../zigbrains/zig/pairing/ZigQuoteHandler.kt | 36 +++++++++++++++++++ .../zigbrains/zon/pairing/ZonQuoteHandler.kt | 2 +- .../resources/META-INF/zigbrains-core.xml | 3 ++ gradle.properties | 2 +- 7 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 core/src/main/kotlin/com/falsepattern/zigbrains/zig/pairing/ZigQuoteHandler.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 00e062cb..b08bfa4a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,18 @@ Changelog structure reference: ## [Unreleased] +## [20.1.0] + +### Added + +- Zig + - String, character literal, and `@"identifier"` quote matching + +### Fixed + +- Zon + - Broken string quote handling + ## [20.0.4] ### Fixed diff --git a/core/src/main/grammar/Zig.bnf b/core/src/main/grammar/Zig.bnf index db8a997f..fa980fe7 100644 --- a/core/src/main/grammar/Zig.bnf +++ b/core/src/main/grammar/Zig.bnf @@ -165,6 +165,8 @@ STRING_LITERAL_SINGLE='quoted string literal' STRING_LITERAL_MULTI='multiline string literal' + BAD_SQUOT='unterminated character literal' + BAD_DQUOT='unterminated string' IDENTIFIER='identifier' BUILTINIDENTIFIER='builtin identifier' diff --git a/core/src/main/grammar/Zig.flex b/core/src/main/grammar/Zig.flex index 3a011010..307239dc 100644 --- a/core/src/main/grammar/Zig.flex +++ b/core/src/main/grammar/Zig.flex @@ -80,7 +80,8 @@ BUILTINIDENTIFIER="@"[A-Za-z_][A-Za-z0-9_]* %state CHAR_LIT %state ID_QUOT -%state UNT_QUOT +%state UNT_SQUOT +%state UNT_DQUOT %state CDOC_CMT %state DOC_CMT @@ -221,14 +222,14 @@ BUILTINIDENTIFIER="@"[A-Za-z_][A-Za-z0-9_]* "'" { yybegin(CHAR_LIT); } {char_char}*"'" { yybegin(YYINITIAL); return CHAR_LITERAL; } - [^] { yypushback(1); yybegin(UNT_QUOT); } + [^] { yypushback(1); yybegin(UNT_SQUOT); } {FLOAT} { return FLOAT; } {INTEGER} { return INTEGER; } "\"" { yybegin(STR_LIT); } {string_char}*"\"" { yybegin(YYINITIAL); return STRING_LITERAL_SINGLE; } - [^] { yypushback(1); yybegin(UNT_QUOT); } + [^] { yypushback(1); yybegin(UNT_DQUOT); } "\\\\" { yybegin(STR_MULT_LINE); } {all_nl_wrap} "\\\\" { } {all_nl_nowrap} { yybegin(YYINITIAL); return STRING_LITERAL_MULTI; } @@ -236,11 +237,12 @@ BUILTINIDENTIFIER="@"[A-Za-z_][A-Za-z0-9_]* {IDENTIFIER_PLAIN} { return IDENTIFIER; } "@\"" { yybegin(ID_QUOT); } {string_char}*"\"" { yybegin(YYINITIAL); return IDENTIFIER; } - [^] { yypushback(1); yybegin(UNT_QUOT); } + [^] { yypushback(1); yybegin(UNT_DQUOT); } {BUILTINIDENTIFIER} { return BUILTINIDENTIFIER; } - [^\n]*{CRLF} { yybegin(YYINITIAL); return BAD_CHARACTER; } + [^\n]*{CRLF} { yybegin(YYINITIAL); return BAD_SQUOT; } + [^\n]*{CRLF} { yybegin(YYINITIAL); return BAD_DQUOT; } {WHITE_SPACE} { return WHITE_SPACE; } diff --git a/core/src/main/kotlin/com/falsepattern/zigbrains/zig/pairing/ZigQuoteHandler.kt b/core/src/main/kotlin/com/falsepattern/zigbrains/zig/pairing/ZigQuoteHandler.kt new file mode 100644 index 00000000..5e93e376 --- /dev/null +++ b/core/src/main/kotlin/com/falsepattern/zigbrains/zig/pairing/ZigQuoteHandler.kt @@ -0,0 +1,36 @@ +/* + * 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.pairing + +import com.falsepattern.zigbrains.zig.psi.ZigTypes +import com.intellij.codeInsight.editorActions.MultiCharQuoteHandler +import com.intellij.codeInsight.editorActions.SimpleTokenSetQuoteHandler +import com.intellij.openapi.editor.highlighter.HighlighterIterator + +class ZigQuoteHandler: SimpleTokenSetQuoteHandler(ZigTypes.STRING_LITERAL_SINGLE, ZigTypes.IDENTIFIER, ZigTypes.BAD_DQUOT, ZigTypes.CHAR_LITERAL, ZigTypes.BAD_SQUOT), MultiCharQuoteHandler { + override fun getClosingQuote(iterator: HighlighterIterator, offset: Int) = when(iterator.tokenType) { + ZigTypes.STRING_LITERAL_SINGLE, ZigTypes.IDENTIFIER, ZigTypes.BAD_DQUOT -> "\"" + ZigTypes.CHAR_LITERAL, ZigTypes.BAD_SQUOT -> "'" + else -> null + } +} \ No newline at end of file diff --git a/core/src/main/kotlin/com/falsepattern/zigbrains/zon/pairing/ZonQuoteHandler.kt b/core/src/main/kotlin/com/falsepattern/zigbrains/zon/pairing/ZonQuoteHandler.kt index a4f53df1..786edb3f 100644 --- a/core/src/main/kotlin/com/falsepattern/zigbrains/zon/pairing/ZonQuoteHandler.kt +++ b/core/src/main/kotlin/com/falsepattern/zigbrains/zon/pairing/ZonQuoteHandler.kt @@ -27,7 +27,7 @@ import com.intellij.codeInsight.editorActions.MultiCharQuoteHandler import com.intellij.codeInsight.editorActions.SimpleTokenSetQuoteHandler import com.intellij.openapi.editor.highlighter.HighlighterIterator -class ZonQuoteHandler: SimpleTokenSetQuoteHandler(ZonTypes.STRING_LITERAL, ZonTypes.BAD_STRING), MultiCharQuoteHandler { +class ZonQuoteHandler: SimpleTokenSetQuoteHandler(ZonTypes.STRING_LITERAL_SINGLE, ZonTypes.BAD_STRING), MultiCharQuoteHandler { override fun getClosingQuote(iterator: HighlighterIterator, offset: Int) = "\"" } \ 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 4d5ed253..12dd4b6c 100644 --- a/core/src/main/resources/META-INF/zigbrains-core.xml +++ b/core/src/main/resources/META-INF/zigbrains-core.xml @@ -22,6 +22,9 @@ + diff --git a/gradle.properties b/gradle.properties index b04898a0..bd364753 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,7 +1,7 @@ pluginName=ZigBrains pluginRepositoryUrl=https://github.com/FalsePattern/ZigBrains -pluginVersion=20.0.4 +pluginVersion=20.1.0 pluginSinceBuild=241 pluginUntilBuild=241.*