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).
This commit is contained in:
l0ng-ai
2026-11-15 19:21:48 +08:00
committed by GitHub
parent 7ec0a4fed2
commit 8caeb1305f
2 changed files with 23 additions and 1 deletions
+18
View File
@@ -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" }));
+5 -1
View File
@@ -114,7 +114,11 @@ export function interpolationParser<S>(base: StreamParser<S>, 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,