Порівняти коміти

...
1 Коміти
Автор SHA1 Повідомлення Дата
l0ng-ai 462f4a5e1d fix(editor): indent Slint element blocks by their braces
Slint borrows the C mode for highlighting until slint-lsp's semantic tokens
arrive, and Enter used that mode's statement indentation: after a child
element's closing brace the next line went back to column 0, and nested
blocks below it opened one level short. Slint now uses the brace-based
fallback indentation even though a highlighting mode is loaded.

Fixes #6976
2026-11-30 18:21:56 +08:00
2 змінених файлів з 50 додано та 3 видалено

No files matched your search

+42 -2
Переглянути файл
@@ -1,12 +1,12 @@
import { EditorSelection, EditorState } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { insertNewlineAndIndent } from "@codemirror/commands";
import { LanguageDescription, type Language } from "@codemirror/language";
import { LanguageDescription, indentUnit, type Language } from "@codemirror/language";
import { describe, expect, it } from "vitest";
import { getIndentation } from "@codemirror/language";
import { highlightTree, tagHighlighter, tags as t } from "@lezer/highlight";
import { logWarning } from "./highlight";
import { fencedCodeMode, findLanguageDescription, loadSupport } from "./languages";
import { commentTokens, fencedCodeMode, findLanguageDescription, loadSupport } from "./languages";
describe("language modes", () => {
it("highlights Julia declarations, calls, types, macros and literals without semantic tokens", async () => {
@@ -765,3 +765,43 @@ describe("language modes", () => {
expect(found).not.toContain("yes:keyword");
});
});
describe("Slint indentation", () => {
it("indents element blocks by their braces under the C highlighting mode", async () => {
const support = await loadSupport(findLanguageDescription("C", "/p/app.slint")!);
const slint = commentTokens({
id: "slint",
name: "Slint",
extensions: ["slint"],
uiMode: "C",
lineComment: "//",
blockComment: ["/*", "*/"],
indent: { tabSize: 4, insertSpaces: true },
});
const enter = (doc: string, at = doc.length) => {
const view = new EditorView({
state: EditorState.create({
doc,
selection: EditorSelection.cursor(at),
extensions: [support!, slint, indentUnit.of(" ")],
}),
parent: document.body,
});
insertNewlineAndIndent(view);
const out = view.state.doc.toString();
view.destroy();
return out;
};
const app = "export component App inherits Window {";
expect(enter(app)).toBe(`${app}\n `);
expect(enter(`${app}}`, app.length)).toBe(`${app}\n \n}`);
expect(enter(`${app}\n width: 100px;`)).toBe(`${app}\n width: 100px;\n `);
// After a child element closes, the next line stays at the child's level.
expect(enter(`${app}\n Text {\n text: "hi";\n }`)).toBe(
`${app}\n Text {\n text: "hi";\n }\n `,
);
expect(enter(`${app}\n Text {\n }\n Rectangle {`)).toBe(
`${app}\n Text {\n }\n Rectangle {\n `,
);
});
});
+8 -1
Переглянути файл
@@ -740,6 +740,13 @@ const NO_INDENT_LANGS = new Set([
"gitattributes",
]);
/**
* Languages highlighted by a borrowed mode whose own indentation doesn't fit them: Slint's
* element blocks (`Text { … }` without a `;`) read as unfinished C statements, so Enter after
* one lands at the wrong column. These use the brace-based fallback indentation instead.
*/
const BRACE_INDENT_OVER_MODE = new Set(["slint"]);
function opensBlock(code: string, language: string | undefined): boolean {
if (!code) return false;
if (/[{([]$/.test(code)) return true;
@@ -779,7 +786,7 @@ function indentationCode(text: string, lineComment: string | undefined): string
*/
export function fallbackIndent(language?: string): Extension {
return indentService.of((cx, pos) => {
if (cx.state.facet(languageFacet)) return undefined;
if (cx.state.facet(languageFacet) && !BRACE_INDENT_OVER_MODE.has(language ?? "")) return undefined;
const lineComment = cx.state.languageDataAt<{ line?: string }>("commentTokens", pos)[0]?.line;
const stripComment = (text: string) => indentationCode(text, lineComment);