mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 08:03:19 +00:00
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:
@@ -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.
|
||||
|
||||
@@ -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(" ")];
|
||||
|
||||
Reference in New Issue
Block a user