From 8caeb1305fc5aaa5146cf2ed18559f55c21ededa Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Sun, 15 Nov 2026 19:21:48 +0800 Subject: [PATCH] fix(editor): indent Kotlin, Scala, Dart and Groovy brackets opened after a string (#3520) The string-interpolation wrapper let the stock clike tokenizer consume an empty string on a new stream at column 0 whenever a literal opened; clike read that as a new line and reset the line's indentation to 0, so a bracket opened later on the same line indented its contents from column 0. Enter and paste with Reformat on Paste put the following lines at the wrong column. The empty string is now tokenized at the literal's own column, which fixes all dialects served by the wrapper (Kotlin, Scala, Dart, Groovy; C# shares the code path). --- src/editor/core/cm/interpolation.test.ts | 18 ++++++++++++++++++ src/editor/core/cm/interpolation.ts | 6 +++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/editor/core/cm/interpolation.test.ts b/src/editor/core/cm/interpolation.test.ts index a3dbeb2d7..dfb48513a 100644 --- a/src/editor/core/cm/interpolation.test.ts +++ b/src/editor/core/cm/interpolation.test.ts @@ -1,5 +1,6 @@ import { describe, expect, it } from "vitest"; import { EditorState } from "@codemirror/state"; +import { getIndentation, indentUnit } from "@codemirror/language"; import { completedSyntaxTree } from "@/test/syntax"; import { classHighlighter, highlightTree } from "@lezer/highlight"; import { findLanguageDescription, loadSupport, loadedSupport } from "./languages"; @@ -81,6 +82,23 @@ describe("frontend string interpolation", () => { expect(out.some((t) => t.text.includes("{brace}") && t.style === "tok-string")).toBe(true); }); + it.each(["Kotlin", "Scala", "Dart", "Groovy", "C#"])( + "%s indents brackets opened after a string from the line's indentation", + async (mode) => { + const support = await loadSupport(findLanguageDescription(mode, "")!); + const doc = 'f(\n pair("a", g(\n x\n )),\n)'; + const state = EditorState.create({ + doc, + extensions: [support!, indentUnit.of(" "), EditorState.tabSize.of(4)], + }); + completedSyntaxTree(state); + const indent = (line: number) => getIndentation(state, state.doc.line(line).from); + expect(indent(3)).toBe(8); + // C#'s mode leaves a closing bracket's indentation to the editor. + if (mode !== "C#") expect(indent(4)).toBe(4); + }, + ); + it("keeps Groovy dotted interpolation in code", async () => { const out = await tokens("Groovy", '"$person.name tail"'); expect(out).toContainEqual(expect.objectContaining({ text: "name", style: "tok-variableName" })); diff --git a/src/editor/core/cm/interpolation.ts b/src/editor/core/cm/interpolation.ts index ea894d2b1..e0709d9f8 100644 --- a/src/editor/core/cm/interpolation.ts +++ b/src/editor/core/cm/interpolation.ts @@ -114,7 +114,11 @@ export function interpolationParser(base: StreamParser, dialect: Interpola } if (opening && style !== "comment" && style !== "string.special") { stream.pos = from + opening.length; - base.token(new StringStream('""', 4, state.indentUnit), frame.code); + // Let the base tokenizer see an empty string at the literal's own column: a stream + // at column 0 would read as a new line and reset the base's line indentation. + const empty = new StringStream(stream.string.slice(0, from) + '""', 4, state.indentUnit); + empty.pos = empty.start = from; + base.token(empty, frame.code); frame.literal = { quote, braces,