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

...
Автор SHA1 Повідомлення Дата
l0ng-ai 7af0ff3754 fix(editor): don't indent C++ namespace and extern "C" contents on Enter
Enter inside `namespace x { … }` or `extern "C" { … }` indented the new line
one level, like any brace body. clang-format's built-in styles (LLVM, Google,
Chromium, Mozilla, Microsoft, GNU) keep those contents at the level of the
braces' line (NamespaceIndentation: None), so the editor now does too; other
brace bodies keep their indentation.

Fixes #3993
2026-11-17 22:09:46 +08:00
2 змінених файлів з 44 додано та 3 видалено

No files matched your search

+23
Переглянути файл
@@ -1,6 +1,7 @@
import { EditorState } from "@codemirror/state";
import { LanguageDescription, 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";
@@ -271,6 +272,28 @@ describe("language modes", () => {
}
});
it('keeps C++ namespace and extern "C" contents at the level of their braces', async () => {
const support = await loadSupport(findLanguageDescription("C++", "/p/a.cpp")!);
const doc = [
"namespace foo {",
"class Bar {",
" int x;",
"};",
"namespace inner {",
"void f() {",
" g();",
"}",
"} // namespace inner",
"} // namespace foo",
'extern "C" {',
"int h();",
"}",
].join("\n");
const state = EditorState.create({ doc, extensions: [support!, EditorState.tabSize.of(2)] });
const indent = (line: number) => getIndentation(state, state.doc.line(line).from);
expect([2, 3, 4, 5, 6, 7, 9, 10, 12, 13].map(indent)).toEqual([0, 2, 0, 0, 0, 2, 0, 0, 0, 0]);
});
it("colours `this`, `super` and Rust's `self` as keywords", async () => {
const highlighter = tagHighlighter([
{ tag: t.keyword, class: "keyword" },
+21 -3
Переглянути файл
@@ -11,6 +11,8 @@ import {
LanguageDescription,
LanguageSupport,
StreamLanguage,
delimitedIndent,
indentNodeProp,
indentService,
language as languageFacet,
type StreamParser,
@@ -421,12 +423,28 @@ const EXTRA_TAGS: Record<string, Parameters<typeof styleTags>[0]> = {
YAML: { "Key/Literal Key/QuotedLiteral": t.keyword },
};
const bracedIndent = delimitedIndent({ closing: "}" });
/** Grammar props the stock languages lack: indentation that follows the language's usual style. */
const EXTRA_PROPS: Record<string, NonNullable<Parameters<LRLanguage["configure"]>[0]["props"]>> = {
// clang-format's built-in styles (NamespaceIndentation: None, no extern block indent) keep the
// contents of `namespace x { … }` and `extern "C" { … }` at the level of the braces' line.
"C++": [
indentNodeProp.add({
// The stock rule for every `{ … }` body, except the bodies of these two.
CompoundStatement: (cx) =>
cx.node.parent?.name === "NamespaceDefinition" || cx.node.parent?.name === "LinkageSpecification"
? cx.baseIndent
: bracedIndent(cx),
}),
],
};
function patchSupport(desc: LanguageDescription, support: LanguageSupport): LanguageSupport {
const extra = EXTRA_TAGS[desc.name];
const props = [...(extra ? [styleTags(extra)] : []), ...(EXTRA_PROPS[desc.name] ?? [])];
const language =
extra && support.language instanceof LRLanguage
? support.language.configure({ props: [styleTags(extra)] })
: support.language;
props.length && support.language instanceof LRLanguage ? support.language.configure({ props }) : support.language;
const result = new LanguageSupport(language, [
support.support,
docCommentTags(desc.name),