From 8dc1fecc0eb9d24dfec2c9ede39c88b4824bb09b Mon Sep 17 00:00:00 2001 From: Yohann CATHERINE Date: Wed, 2 Dec 2026 14:40:11 +0100 Subject: [PATCH] feat(templates): C and C++ live templates (#7535) Adds live templates for C, C++ and Objective-C(++): a C set (main, fori, if/ife, while, do, sw, st, tds, en, inc/incl, incg include guard named after the file, ifdef/ifndef, pf) and a C++ set (iter, itit, ritit, cl, cls, nsp, tmpl, lam, try, cout/cerr/cin, uptr/sptr, sc, GoogleTest test, Catch2/doctest tc). Documented on the C/C++ language page. Co-authored-by: yohannlog <8901733+yohannlog@users.noreply.github.com> --- docs/en/languages/cpp.mdx | 4 + docs/zh/languages/cpp.mdx | 4 + src/features/templates/__tests__/live.test.ts | 10 ++ src/features/templates/live/builtins.ts | 105 ++++++++++++++++++ src/platform/locales/text/fr.json | 10 ++ src/platform/locales/text/zh-TW.json | 10 ++ src/platform/locales/text/zh.json | 10 ++ 7 files changed, 153 insertions(+) diff --git a/docs/en/languages/cpp.mdx b/docs/en/languages/cpp.mdx index c010b9ab0..cdf0d8c48 100644 --- a/docs/en/languages/cpp.mdx +++ b/docs/en/languages/cpp.mdx @@ -38,6 +38,10 @@ clangd looks for `compile_commands.json` in the file's parent directories and in In a folder with a `buildServer.json` (the build server config for Xcode projects, see [Swift › Xcode projects](/en/languages/swift#xcode-projects)), C, C++, Objective-C and Objective-C++ files use sourcekit-lsp instead of clangd. sourcekit-lsp gets each file's flags from the build server and passes them to its own clangd, so headers on the target's search paths and the generated `-Swift.h` resolve. Swift and C-family files of the project share one sourcekit-lsp. A build server config only under `.bsp/` is not used for this, because sbt, Mill and Bazel write that folder too. Without `buildServer.json`, or when sourcekit-lsp is not installed, clangd serves the files as described above. +### Live templates + +Live templates for C and C++ include `main`, `fori`, `iter` (range-based `for`), `itit`, `incg` (an include guard named after the file), `cl`, `nsp`, `lam`, `cout`, `uptr` and `test` (GoogleTest) / `tc` (Catch2 or doctest). They are listed under **Settings › Editor › Live Templates**. + ### Header and source **Switch Header/Source** (in Find Action; `F10`, `⌥O` / `Alt+O` in the VS Code keymaps) opens the counterpart of the current file: `Foo.m` or `Foo.cpp` from `Foo.h`, and back. It asks clangd first (`textDocument/switchSourceHeader`, which also finds a source file in another folder). When clangd is not running for the file or has no answer, it opens a project file with the same name and a counterpart extension, preferring the same folder. **Related Symbol…** (`⌃⌘↑` / `Ctrl+Alt+Home`) lists every match by name instead. diff --git a/docs/zh/languages/cpp.mdx b/docs/zh/languages/cpp.mdx index c2bcb84d9..e28f17c47 100644 --- a/docs/zh/languages/cpp.mdx +++ b/docs/zh/languages/cpp.mdx @@ -38,6 +38,10 @@ clangd 会在文件的各级父目录以及这些目录下的 `build/` 子目录 文件夹里有 `buildServer.json`(Xcode 项目的构建服务器配置,见 [Swift › Xcode 项目](/zh/languages/swift#xcode-项目))时,C、C++、Objective-C 和 Objective-C++ 文件改由 sourcekit-lsp 处理,不再用 clangd。sourcekit-lsp 从构建服务器拿到每个文件的编译参数,再交给它自带的 clangd,因此 target 搜索路径里的头文件和生成的 `-Swift.h` 都能找到。同一项目的 Swift 文件和 C 系文件共用一个 sourcekit-lsp。只放在 `.bsp/` 下的构建服务器配置不在此列,因为 sbt、Mill 和 Bazel 也会写这个文件夹。没有 `buildServer.json`,或者没有安装 sourcekit-lsp 时,仍由 clangd 处理,方式同上。 +### 实时模板 + +C/C++ 的实时模板包括 `main`、`fori`、`iter`(基于范围的 `for`)、`itit`、`incg`(按文件名命名的头文件保护)、`cl`、`nsp`、`lam`、`cout`、`uptr`,以及 `test`(GoogleTest)/ `tc`(Catch2 或 doctest),列在 **设置 › 编辑器 › 实时模板** 中。 + ### 头文件与源文件 **切换头文件/源文件**(可在“查找操作”中运行;`F10`,VS Code 键位下为 `⌥O` / `Alt+O`)打开当前文件对应的另一半:在 `Foo.h` 中打开 `Foo.m` 或 `Foo.cpp`,反之亦然。它先询问 clangd(`textDocument/switchSourceHeader`,能找到放在其他文件夹里的源文件);clangd 没有为该文件运行或没有给出结果时,打开项目中同名、扩展名对应的文件,优先同一文件夹。**相关符号…**(`⌃⌘↑` / `Ctrl+Alt+Home`)则按名称列出所有匹配。 diff --git a/src/features/templates/__tests__/live.test.ts b/src/features/templates/__tests__/live.test.ts index 894bd9a4b..20c9f525e 100644 --- a/src/features/templates/__tests__/live.test.ts +++ b/src/features/templates/__tests__/live.test.ts @@ -165,6 +165,16 @@ describe("expansion", () => { for (const a of ["todo", "fixme"]) expect(has("go", a)).toBe(true); for (const a of ["fore", "forek", "pubf", "prisf", "thr", "eco", "rqro"]) expect(has("php", a)).toBe(true); expect(has("java", "fore")).toBe(false); + for (const a of ["main", "fori", "incg", "inc", "st", "sw"]) expect(has("c", a)).toBe(true); + for (const a of ["fori", "iter", "itit", "cl", "nsp", "cout", "lam", "test"]) expect(has("cpp", a)).toBe(true); + for (const a of ["iter", "cout", "cl"]) expect(has("c", a)).toBe(false); + }); + + it("names a C / C++ include guard after the file", () => { + const incg = BUILTIN_TEMPLATES.find((t) => t.id === "c.incg")!; + const fns = { fileName: () => "my_widget.hpp" }; + const r = apply("", expandTemplate(incg, { fns }).snippet, 0); + expect(r.text).toBe("#ifndef MY_WIDGET_HPP\n#define MY_WIDGET_HPP\n\n\n\n#endif // MY_WIDGET_HPP"); }); it("offers React component templates only in JSX / TSX, named after the file", () => { diff --git a/src/features/templates/live/builtins.ts b/src/features/templates/live/builtins.ts index db6d76cab..5ce2c8e15 100644 --- a/src/features/templates/live/builtins.ts +++ b/src/features/templates/live/builtins.ts @@ -279,6 +279,109 @@ const zig = group( ], ); +/* + * C / C++ (CLion's set plus common C++ idioms). The C group also applies to + * C++ and Objective-C(++), since they accept the same statements. + */ +const C_LANGS = ["c", "cpp", "objective-c", "objective-cpp"]; +const CPP_LANGS = ["cpp", "objective-cpp"]; +const guard = v("GUARD", "capitalizeAndUnderscore(fileName())", '"HEADER_H"', true); +const cFamily = group("C / C++", "c", C_LANGS, [ + { abbrev: "main", description: "main function", body: "int main(int argc, char *argv[]) {\n\t$END$\n\treturn 0;\n}" }, + { + abbrev: "fori", + description: "Indexed for loop", + body: "for (int $I$ = 0; $I$ < $N$; ++$I$) {\n\t$END$\n}", + variables: [v("N"), v("I", undefined, '"i"')], + }, + { abbrev: "if", description: "if statement", body: "if ($COND$) {\n\t$END$\n}" }, + { abbrev: "ife", description: "if / else", body: "if ($COND$) {\n\t$END$\n} else {\n\t\n}" }, + { abbrev: "while", description: "while loop", body: "while ($COND$) {\n\t$END$\n}" }, + { abbrev: "do", description: "do / while loop", body: "do {\n\t$END$\n} while ($COND$);" }, + { + abbrev: "sw", + description: "switch statement", + body: "switch ($EXPR$) {\n\tcase $VAL$:\n\t\t$END$\n\t\tbreak;\n\tdefault:\n\t\tbreak;\n}", + }, + { abbrev: "st", description: "struct", body: "struct $NAME$ {\n\t$END$\n};" }, + { abbrev: "tds", description: "typedef struct", body: "typedef struct $NAME$ {\n\t$END$\n} $NAME$;" }, + { abbrev: "en", description: "enum", body: "enum $NAME$ {\n\t$END$\n};" }, + { abbrev: "inc", description: "#include <…>", body: "#include <$END$>" }, + { abbrev: "incl", description: '#include "…"', body: '#include "$END$"' }, + { + abbrev: "incg", + description: "Include guard", + body: "#ifndef $GUARD$\n#define $GUARD$\n\n$END$\n\n#endif // $GUARD$", + variables: [guard], + }, + { abbrev: "ifdef", description: "#ifdef … #endif", body: "#ifdef $NAME$\n$END$\n#endif" }, + { abbrev: "ifndef", description: "#ifndef … #endif", body: "#ifndef $NAME$\n$END$\n#endif" }, + { abbrev: "pf", description: "printf", body: 'printf("$FMT$\\n"$END$);', variables: [v("FMT", undefined, '"%d"')] }, +]); +const cpp = group("C++", "cpp", CPP_LANGS, [ + { + abbrev: "iter", + description: "Range-based for loop", + body: "for ($TYPE$ $ITEM$ : $RANGE$) {\n\t$END$\n}", + variables: [v("RANGE"), v("TYPE", undefined, '"auto &"'), v("ITEM", "suggestVariableName()", '"item"')], + }, + { + abbrev: "itit", + description: "Iterate with begin() / end()", + body: "for (auto $IT$ = $C$.begin(); $IT$ != $C$.end(); ++$IT$) {\n\t$END$\n}", + variables: [v("C"), v("IT", undefined, '"it"')], + }, + { + abbrev: "ritit", + description: "Iterate with rbegin() / rend()", + body: "for (auto $IT$ = $C$.rbegin(); $IT$ != $C$.rend(); ++$IT$) {\n\t$END$\n}", + variables: [v("C"), v("IT", undefined, '"it"')], + }, + { abbrev: "cl", description: "class", body: "class $NAME$ {\npublic:\n\t$END$\n};" }, + { + abbrev: "cls", + description: "class with constructor and virtual destructor", + body: "class $NAME$ {\npublic:\n\t$NAME$();\n\tvirtual ~$NAME$();\n\t$END$\n};", + }, + { abbrev: "nsp", description: "namespace", body: "namespace $NAME$ {\n$END$\n} // namespace $NAME$" }, + { + abbrev: "tmpl", + description: "template ", + body: "template \n$END$", + variables: [v("T", undefined, '"T"')], + }, + { + abbrev: "lam", + description: "Lambda", + body: "[$CAPTURE$]($PARAMS$) {\n\t$END$\n}", + variables: [v("CAPTURE", undefined, '"&"')], + }, + { + abbrev: "try", + description: "try / catch", + body: "try {\n\t$END$\n} catch (const $EXC$ &$E$) {\n\t\n}", + variables: [v("EXC", undefined, '"std::exception"'), v("E", undefined, '"e"')], + }, + { abbrev: "cout", description: "std::cout", body: "std::cout << $END$ << std::endl;" }, + { abbrev: "cerr", description: "std::cerr", body: "std::cerr << $END$ << std::endl;" }, + { abbrev: "cin", description: "std::cin", body: "std::cin >> $END$;" }, + { abbrev: "uptr", description: "std::make_unique", body: "auto $NAME$ = std::make_unique<$TYPE$>($END$);" }, + { abbrev: "sptr", description: "std::make_shared", body: "auto $NAME$ = std::make_shared<$TYPE$>($END$);" }, + { abbrev: "sc", description: "static_cast", body: "static_cast<$TYPE$>($END$)" }, + { + abbrev: "test", + description: "GoogleTest test", + body: "TEST($SUITE$, $NAME$) {\n\t$END$\n}", + variables: [v("SUITE", undefined, '"Suite"'), v("NAME", undefined, '"Works"')], + }, + { + abbrev: "tc", + description: "Catch2 / doctest test case", + body: 'TEST_CASE("$NAME$") {\n\t$END$\n}', + variables: [v("NAME", undefined, '"it works"')], + }, +]); + const java = group( "Java", "java", @@ -591,6 +694,8 @@ export const BUILTIN_TEMPLATES: readonly LiveTemplate[] = [ ...python, ...go, ...zig, + ...cFamily, + ...cpp, ...java, ...kotlin, ...php, diff --git a/src/platform/locales/text/fr.json b/src/platform/locales/text/fr.json index c6b97ad91..951f24de2 100644 --- a/src/platform/locales/text/fr.json +++ b/src/platform/locales/text/fr.json @@ -1182,6 +1182,16 @@ "Merge incoming changes into the current branch": "Fusionner les modifications entrantes dans la branche actuelle", "Merge…": "Fusionner…", "Message:": "Message :", + "while loop": "Boucle while", + "do / while loop": "Boucle do / while", + "switch statement": "Instruction switch", + "Include guard": "Garde d’inclusion", + "Range-based for loop": "Boucle for sur une plage", + "Iterate with begin() / end()": "Itérer avec begin() / end()", + "Iterate with rbegin() / rend()": "Itérer avec rbegin() / rend()", + "class with constructor and virtual destructor": "Classe avec constructeur et destructeur virtuel", + "GoogleTest test": "Test GoogleTest", + "Catch2 / doctest test case": "Cas de test Catch2 / doctest", "Method": "Méthode", "Method List": "Liste des méthodes", "Method name": "Nom de la méthode", diff --git a/src/platform/locales/text/zh-TW.json b/src/platform/locales/text/zh-TW.json index 6fe73378a..239fbefc4 100644 --- a/src/platform/locales/text/zh-TW.json +++ b/src/platform/locales/text/zh-TW.json @@ -1943,6 +1943,16 @@ "Make File Read-only": "使檔案唯讀", "Make File Writable": "使檔案可寫", "Make Read-only": "設為唯讀", + "while loop": "while 迴圈", + "do / while loop": "do / while 迴圈", + "switch statement": "switch 陳述式", + "Include guard": "標頭檔保護", + "Range-based for loop": "基於範圍的 for 迴圈", + "Iterate with begin() / end()": "用 begin() / end() 迭代", + "Iterate with rbegin() / rend()": "用 rbegin() / rend() 迭代", + "class with constructor and virtual destructor": "含建構函式和虛擬解構函式的類別", + "GoogleTest test": "GoogleTest 測試", + "Catch2 / doctest test case": "Catch2 / doctest 測試案例", "Make Writable": "設為可寫", "Make another changelist active first.": "請先將其他更改清單設為活動。", "Manage": "管理", diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index 6a8ce35d3..3b3809e13 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -2413,6 +2413,16 @@ "Interpreter type": "解释器类型", "Interpreter:": "解释器:", "Introduce Constant…": "引入常量…", + "while loop": "while 循环", + "do / while loop": "do / while 循环", + "switch statement": "switch 语句", + "Include guard": "头文件保护", + "Range-based for loop": "基于范围的 for 循环", + "Iterate with begin() / end()": "用 begin() / end() 迭代", + "Iterate with rbegin() / rend()": "用 rbegin() / rend() 迭代", + "class with constructor and virtual destructor": "带构造函数和虚析构函数的类", + "GoogleTest test": "GoogleTest 测试", + "Catch2 / doctest test case": "Catch2 / doctest 测试用例", "Introduce Field…": "引入字段…", "Introduce Parameter…": "引入参数…", "Introduce Variable…": "引入变量…",