fix(editor): end completed C and C++ type definitions with }; (#5122)

⌘⇧↵ after `struct S` (C) or `class A` (C++) produced `struct S {` / `|` / `}`: a type definition without its terminating `;`, which does not compile.

`completeStatementSpec` now closes the body with `};` in C, C++, Objective-C and Objective-C++ when the header is a type definition: `struct` / `class` / `union` / `enum` / `enum class`, optionally with `typedef`, `template <…>`, `final` and base classes. `typedef struct Node`, `union U` and `template <typename T> class Box` are now recognised as block heads too. Namespaces and control flow keep a plain `}`.
This commit is contained in:
l0ng-ai
2026-11-22 13:20:15 +08:00
committed by GitHub
parent 3cbe327a6a
commit bf4fff8fcf
2 changed files with 35 additions and 1 deletions
+9 -1
View File
@@ -585,6 +585,11 @@ const KEYWORD_HEAD =
/\b(do(\s*\|[^|]*\|)?|then|where|of)$|^\s*(def|defp|defmodule|class|module|function|local function|if|unless|elsif|elseif|else|while|until|for|case|begin)\b/;
const BLOCK_HEAD =
/^\s*(if|else|for|while|do|switch|try|catch|finally|fn|fun|func|function|class|struct|enum|impl|trait|interface|object|match|when|loop|unsafe|mod|namespace)\b/;
/** Languages whose `struct` / `class` / `union` / `enum` definitions end with `};`. */
const C_TYPE_LANGS = new Set(["c", "cpp", "objective-c", "objective-cpp"]);
/** `struct S`, `class A : public B`, `enum class E : int`, `template <typename T> class A`. */
const C_TYPE_HEAD =
/^\s*(template\s*<[^>]*>\s*)?(typedef\s+)?(struct|class|union|enum(\s+(class|struct))?)(\s+[\w:]+)?(\s+final)?(\s*:\s*[^{};()]+)?\s*$/;
/** A function header that names the function but has no parameter list yet. */
const FUNCTION_NAME_ONLY = new RegExp(
String.raw`^\s*((pub(\([^)]*\))?|async|const|unsafe|extern|export|default|static|private|public|protected|internal|open|override|suspend|inline|operator|infix|tailrec)\s+)*(func|fn|fun|function)\s+[\w$]+(<[^>]*>)?$`,
@@ -628,6 +633,7 @@ const METHOD_HEAD_LANGS = new Set(["java", "csharp", "groovy"]);
export function isBlockHead(code: string, language: string): boolean {
if (KEYWORD_BLOCK_LANGS.has(language) || /[{;,]$/.test(code)) return false;
if (BLOCK_HEAD.test(code) || DECLARATION_HEAD.test(code) || FUNCTION_NAME_ONLY.test(code)) return true;
if (C_TYPE_LANGS.has(language) && C_TYPE_HEAD.test(code)) return true;
return (
METHOD_HEAD_LANGS.has(language) &&
!STATEMENT_WORDS.test(code) &&
@@ -759,7 +765,9 @@ export function completeStatementSpec(state: EditorState, language: string): Tra
if (braces && !comment && isBlockHead(text, language)) {
// `func f` / `fn f` / `fun f` → `f()`: a function needs its parameter list.
const params = FUNCTION_NAME_ONLY.test(text) ? "()" : "";
const insert = `${params} {${br}${baseIndent}${unit}${br}${baseIndent}}`;
// A C / C++ type definition ends with `};`.
const close = C_TYPE_LANGS.has(language) && C_TYPE_HEAD.test(text) ? "};" : "}";
const insert = `${params} {${br}${baseIndent}${unit}${br}${baseIndent}${close}`;
const from = line.from + text.length;
return {
// A trailing comment moves to the end of the header line.
+26
View File
@@ -270,6 +270,32 @@ describe("completeStatementSpec (text rules)", () => {
});
});
describe("completeStatementSpec: C and C++ type definitions", () => {
const ext = [comments("//"), indentUnit.of(" ")];
it("ends a struct, class, union or enum with `};`", () => {
for (const [head, language] of [
["struct S", "c"],
["typedef struct Node", "c"],
["union U", "c"],
["enum Color", "c"],
["class A", "cpp"],
["class B : public A", "cpp"],
["enum class E : int", "cpp"],
["template <typename T> class Box", "cpp"],
] as const) {
const s = state(`${head}|`, ext);
expect(withCaret(s, completeStatementSpec(s, language))).toBe(`${head} {\n |\n};`);
}
});
it("keeps a plain `}` for namespaces and control flow", () => {
for (const head of ["if (x)", "namespace n"]) {
const s = state(`${head}|`, ext);
expect(withCaret(s, completeStatementSpec(s, "cpp"))).toBe(`${head} {\n |\n}`);
}
});
});
describe("completeStatementSpec: TypeScript and Python", () => {
const ts = [comments("//", ["/*", "*/"]), indentUnit.of(" ")];
const py = [comments("#"), indentUnit.of(" ")];