diff --git a/src/editor/core/cm/editing.ts b/src/editor/core/cm/editing.ts index 7aae665da..20f002f7d 100644 --- a/src/editor/core/cm/editing.ts +++ b/src/editor/core/cm/editing.ts @@ -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 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. diff --git a/src/editor/core/cm/structural.test.ts b/src/editor/core/cm/structural.test.ts index 2f2f424d8..8a0a00783 100644 --- a/src/editor/core/cm/structural.test.ts +++ b/src/editor/core/cm/structural.test.ts @@ -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 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(" ")];