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,