Add backtick string literals and formatter support

This commit is contained in:
sergeych committed 2026-04-13 23:08:18 +03:00
1 parent e925195495
commit fada848907
15 files changed
+273 -32

No files matched your search

@@ -310,7 +310,7 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
// Try literal and call-based receiver inference around the dot
val i = TextCtx.prevNonWs(text, dotPos - 1)
val className: String? = when {
i >= 0 && text[i] == '"' -> "String"
i >= 0 && (text[i] == '"' || text[i] == '`') -> "String"
i >= 0 && text[i] == ']' -> "List"
i >= 0 && text[i] == '}' -> "Dict"
i >= 0 && text[i] == ')' -> {
@@ -24,6 +24,7 @@ import com.intellij.psi.codeStyle.CodeStyleManager
import com.intellij.psi.impl.source.codeStyle.PreFormatProcessor
import net.sergeych.lyng.format.LyngFormatConfig
import net.sergeych.lyng.format.LyngFormatter
import net.sergeych.lyng.format.LyngStringDelimiterPolicy
import net.sergeych.lyng.idea.LyngLanguage
/**
@@ -170,6 +171,7 @@ class LyngPreFormatProcessor : PreFormatProcessor {
continuationIndentSize = options.CONTINUATION_INDENT_SIZE.coerceAtLeast(options.INDENT_SIZE.coerceAtLeast(1)),
applySpacing = true,
applyWrapping = false,
stringDelimiterPolicy = LyngStringDelimiterPolicy.PreferFewerEscapes,
)
val r = if (runFullFileIndent) currentLocalRange() else workingRangeLocal.intersection(currentLocalRange()) ?: currentLocalRange()
val text = doc.getText(r)
@@ -189,6 +191,7 @@ class LyngPreFormatProcessor : PreFormatProcessor {
continuationIndentSize = options.CONTINUATION_INDENT_SIZE.coerceAtLeast(options.INDENT_SIZE.coerceAtLeast(1)),
applySpacing = settings.enableSpacing,
applyWrapping = true,
stringDelimiterPolicy = LyngStringDelimiterPolicy.PreferFewerEscapes,
)
val r = if (runFullFileIndent) currentLocalRange() else workingRangeLocal.intersection(currentLocalRange()) ?: currentLocalRange()
val text = doc.getText(r)
@@ -101,8 +101,8 @@ class LyngLexer : LexerBase() {
return
}
// String "..." or '...' with simple escape handling
if (ch == '"' || ch == '\'') {
// String "...", `...`, or '...' with simple escape handling
if (ch == '"' || ch == '\'' || ch == '`') {
val quote = ch
i++
while (i < endOffset) {
@@ -0,0 +1,58 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyng.idea.highlight
import org.junit.Assert.assertArrayEquals
import org.junit.Assert.assertEquals
import org.junit.Test
class LyngLexerBacktickStringTest {
@Test
fun backtickStringGetsStringTokenAndColor() {
val lexer = LyngLexer()
val source = """val json = `{"name":"lyng","doc":"use \`quotes\`"}`"""
lexer.start(source, 0, source.length, 0)
val tokens = mutableListOf<Pair<String, String>>()
while (lexer.tokenType != null) {
val tokenText = source.substring(lexer.tokenStart, lexer.tokenEnd)
tokens += lexer.tokenType.toString() to tokenText
lexer.advance()
}
assertEquals(
listOf(
"KEYWORD" to "val",
"WHITESPACE" to " ",
"IDENTIFIER" to "json",
"WHITESPACE" to " ",
"PUNCT" to "=",
"WHITESPACE" to " ",
"STRING" to "`{\"name\":\"lyng\",\"doc\":\"use \\`quotes\\`\"}`"
),
tokens
)
val highlighter = LyngSyntaxHighlighter()
assertArrayEquals(
arrayOf(LyngHighlighterColors.STRING),
highlighter.getTokenHighlights(LyngTokenTypes.STRING)
)
}
}