added label syntax support and highlighting

This commit is contained in:
sergeych committed 2026-01-06 02:37:16 +01:00
1 parent 660a80a26b
commit 26b8370b01
12 files changed
+122 -19

No files matched your search

@@ -477,7 +477,7 @@ private fun splitIntoParts(
private fun applyMinimalSpacingRules(code: String): String {
var s = code
// Ensure space before '(' for control-flow keywords
s = s.replace(Regex("\\b(if|for|while)\\("), "$1 (")
s = s.replace(Regex("\\b(if|for|while|return|break|continue)\\("), "$1 (")
// Space before '{' for control-flow headers only (avoid function declarations)
s = s.replace(Regex("\\b(if|for|while)(\\s*\\([^)]*\\))\\s*\\{"), "$1$2 {")
s = s.replace(Regex("\\belse\\s+if(\\s*\\([^)]*\\))\\s*\\{"), "else if$1 {")
@@ -498,6 +498,8 @@ private fun applyMinimalSpacingRules(code: String): String {
s = s.replace(Regex("(\\bcatch\\s*\\([^)]*\\))\\s*\\{"), "$1 {")
// Ensure space before '(' for catch parameter
s = s.replace(Regex("\\bcatch\\("), "catch (")
// Remove space between control keyword and label: return @label -> return@label
s = s.replace(Regex("\\b(return|break|continue)\\s+(@[\\p{L}_][\\p{L}\\p{N}_]*)"), "$1$2")
// Remove spaces just inside parentheses/brackets: "( a )" -> "(a)"
s = s.replace(Regex("\\(\\s+"), "(")
// Do not strip leading indentation before a closing bracket/paren on its own line
@@ -21,6 +21,15 @@ import kotlin.test.assertEquals
class LyngFormatterTest {
@Test
fun labelFormatting() {
val src = "return @label; break @outer; continue @inner"
val expected = "return@label; break@outer; continue@inner"
val cfg = LyngFormatConfig(applySpacing = true)
val out = LyngFormatter.format(src, cfg)
assertEquals(expected, out)
}
@Test
fun reindent_simpleFunction() {
val src = """
@@ -25,6 +25,16 @@ class HighlightMappingTest {
private fun spansToLabeled(text: String, spans: List<HighlightSpan>): List<Pair<String, HighlightKind>> =
spans.map { text.substring(it.range.start, it.range.endExclusive) to it.kind }
@Test
fun returnAndExits() {
val text = "return 42; break@outer null"
val spans = SimpleLyngHighlighter().highlight(text)
val labeled = spansToLabeled(text, spans)
assertTrue(labeled.any { it.first == "return" && it.second == HighlightKind.Keyword })
assertTrue(labeled.any { it.first == "break" && it.second == HighlightKind.Keyword })
assertTrue(labeled.any { it.first == "@outer" && it.second == HighlightKind.Label })
}
@Test
fun keywordsAndIdentifiers() {
val text = "a and b or not c"