Skip to content

Commit 947fb78

Browse files
Jim Griesmerbobbrow
authored andcommitted
Users/jimgries/snippets (#1823)
1 parent 0521bbe commit 947fb78

File tree

6 files changed

+242
-36
lines changed

6 files changed

+242
-36
lines changed

Extension/ThirdPartyNotices.txt

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,41 @@ Microsoft C/C++ Extension for Visual Studio Code incorporates components from th
77
1. agent-base (https://github.com/TooTallNate/node-agent-base)
88
2. ANTLR (http://www.antlr2.org/)
99
3. Boost (http://www.boost.org)
10-
4. Clang (https://clang.llvm.org/)
11-
5. debug (https://github.com/visionmedia/debug)
12-
6. extend (https://github.com/justmoon/node-extend)
13-
7. fd-slicer (https://github.com/andrewrk/node-fd-slicer)
14-
8. gcc-6/libgcc (http://packages.ubuntu.com/yakkety/main/gcc-6)
15-
9. Guidelines Support Library (https://github.com/Microsoft/GSL)
16-
10. http-proxy-agent (https://github.com/TooTallNate/node-http-proxy-agent)
17-
11. https-proxy-agent (https://github.com/TooTallNate/node-https-proxy-agent)
18-
12. jsonc-parser (https://github.com/Microsoft/node-jsonc-parser)
19-
13. libc++ (https://libcxx.llvm.org/index.html)
20-
14. LLDB (https://lldb.llvm.org/)
21-
15. LLVM (http://llvm.org/)
22-
16. MI Debug Engine (https://github.com/Microsoft/MIEngine)
23-
17. Minimatch (https://github.com/isaacs/minimatch)
24-
18. minimist (https://github.com/substack/minimist)
25-
19. mkdirp (https://github.com/substack/node-mkdirp)
26-
20. Mono (https://github.com/mono/mono)
27-
21. ms (https://github.com/rauchg/ms.js)
28-
22. msgpack for C/C++ (https://github.com/msgpack/msgpack-c)
29-
23. node-http-proxy-agent (https://github.com/TooTallNate/node-https-proxy-agent)
30-
24. node-https-proxy-agent (https://github.com/TooTallNate/node-https-proxy-agent)
31-
25. os-tmpdir (https://github.com/sindresorhus/os-tmpdir)
32-
26. Pend (https://github.com/andrewrk/node-pend)
33-
27. pevents (https://github.com/neosmart/pevents)
34-
28. RapidJSON (https://github.com/miloyip/rapidjson)
35-
29. semver (https://github.com/npm/node-semver)
36-
30. SQLite (https://www.sqlite.org/)
10+
4. C++11 Sublime Text Snippets (https://github.com/Rapptz/cpp-sublime-snippet)
11+
5. Clang (https://clang.llvm.org/)
12+
6. debug (https://github.com/visionmedia/debug)
13+
7. extend (https://github.com/justmoon/node-extend)
14+
8. fd-slicer (https://github.com/andrewrk/node-fd-slicer)
15+
9. gcc-6/libgcc (http://packages.ubuntu.com/yakkety/main/gcc-6)
16+
10. Guidelines Support Library (https://github.com/Microsoft/GSL)
17+
11. http-proxy-agent (https://github.com/TooTallNate/node-http-proxy-agent)
18+
12. https-proxy-agent (https://github.com/TooTallNate/node-https-proxy-agent)
19+
13. jsonc-parser (https://github.com/Microsoft/node-jsonc-parser)
20+
14. libc++ (https://libcxx.llvm.org/index.html)
21+
15. LLDB (https://lldb.llvm.org/)
22+
16. LLVM (http://llvm.org/)
23+
17. MI Debug Engine (https://github.com/Microsoft/MIEngine)
24+
18. Minimatch (https://github.com/isaacs/minimatch)
25+
19. minimist (https://github.com/substack/minimist)
26+
20. mkdirp (https://github.com/substack/node-mkdirp)
27+
21. Mono (https://github.com/mono/mono)
28+
22. ms (https://github.com/rauchg/ms.js)
29+
23. msgpack for C/C++ (https://github.com/msgpack/msgpack-c)
30+
24. node-http-proxy-agent (https://github.com/TooTallNate/node-https-proxy-agent)
31+
25. node-https-proxy-agent (https://github.com/TooTallNate/node-https-proxy-agent)
32+
26. os-tmpdir (https://github.com/sindresorhus/os-tmpdir)
33+
27. Pend (https://github.com/andrewrk/node-pend)
34+
28. pevents (https://github.com/neosmart/pevents)
35+
29. RapidJSON (https://github.com/miloyip/rapidjson)
36+
30. semver (https://github.com/npm/node-semver)
37+
31. SQLite (https://www.sqlite.org/)
3738
Includes:functions (from fossil) (https://fossil-scm.org)
38-
31. Tmp (https://github.com/raszi/node-tmp)
39+
32. Tmp (https://github.com/raszi/node-tmp)
3940
Includes:sample code (https://blog.tompawlak.org/generate-random-values-nodejs-javascript)
40-
32. vscode-debugadapter (https://github.com/Microsoft/vscode-debugadapter-node)
41-
33. vscode-extension-telemetry (https://github.com/Microsoft/vscode-extension-telemetry)
42-
34. vscode-languageserver-node (https://github.com/Microsoft/vscode-languageserver-node)
43-
35. yauzl (https://github.com/thejoshwolfe/yauzl)
41+
33 vscode-debugadapter (https://github.com/Microsoft/vscode-debugadapter-node)
42+
34. vscode-extension-telemetry (https://github.com/Microsoft/vscode-extension-telemetry)
43+
35. vscode-languageserver-node (https://github.com/Microsoft/vscode-languageserver-node)
44+
36. yauzl (https://github.com/thejoshwolfe/yauzl)
4445

4546

4647
%% agent-base NOTICES AND INFORMATION BEGIN HERE
@@ -1412,6 +1413,11 @@ SOFTWARE.
14121413
=========================================
14131414
END OF yauzl NOTICES AND INFORMATION
14141415

1416+
%% C++11 Sublime Text Snippets NOTICES AND INFORMATION BEGIN HERE
1417+
=========================================
1418+
C++ Snippets for Sublime Text (https://packagecontrol.io/packages/C%2B%2B%20Snippets)
14151419

1416-
1420+
Individual snippets based on those from the C++ Snippets for Sublime Text collection are licensed under CC0 1.0 Universal
1421+
=========================================
1422+
END OF C++11 Sublime Text Snippets NOTICES AND INFORMATION
14171423

Extension/cpp_snippets.json

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
{
2+
"class": {
3+
"prefix": "class",
4+
"body": "\nclass ${1:${TM_FILENAME_BASE}}\n{\nprivate:\n\t${2:/* data */}\npublic:\n\t${1}(${3:/* args */});\n\t~${1}();\n};\n\n${1}::${1}(${3})\n{\n}\n\n${1}::~${1}()\n{\n}\n",
5+
"description": "Code snippet for class",
6+
"scope": "source.c++, source.objc++"
7+
},
8+
"classi": {
9+
"prefix": "classi",
10+
"body": "\nclass ${1:${TM_FILENAME_BASE}}\n{\nprivate:\n\t${2:/* data */}\npublic:\n\t${1}::${1}(${3:/* args */}) { $0 }\n\t${1}::~${1}() { }\n};",
11+
"description": "Code snippet for class with inlined constructor/destructor",
12+
"scope": "source.c++, source.objc++"
13+
},
14+
"classt": {
15+
"prefix": "classt",
16+
"body": "\ntemplate<typename T>\nclass ${1:${TM_FILENAME_BASE}}\n{\nprivate:\n\tT ${2:/* data */}\npublic:\n\t${1}(T ${3:/* args */});\n};\n",
17+
"description": "Code snippet for class template",
18+
"scope": "source.c++, source.objc++"
19+
},
20+
"struct": {
21+
"prefix": "struct",
22+
"body": "\nstruct ${1:${TM_FILENAME_BASE}}\n{\n\t${0:/* data */}\n};\n",
23+
"description": "Code snippet for struct",
24+
"scope": "source.c, source.objc, source.c++, source.objc++"
25+
},
26+
"union": {
27+
"prefix": "union",
28+
"body": "\nunion ${1:${TM_FILENAME_BASE}}\n{\n\t${0:/* data */}\n};\n",
29+
"description": "Code snippet for union",
30+
"scope": "source.c, source.objc, source.c++, source.objc++"
31+
},
32+
"if": {
33+
"prefix": "if",
34+
"body": "\nif (${1:/* condition */}) {\n\t${0:/* code */}\n}\n",
35+
"description": "Code snippet for if()",
36+
"scope": "source.c, source.objc, source.c++, source.objc++"
37+
},
38+
"ifel": {
39+
"prefix": "ifel",
40+
"body": "\nif (${1:/* condition */}) {\n\t${2:/* code */}\n}\nelse {\n\t${0:/* code */}\n}\n",
41+
"description": "Code snippet for if() else",
42+
"scope": "source.c, source.objc, source.c++, source.objc++"
43+
},
44+
"ifelif": {
45+
"prefix": "ifelif",
46+
"body": "\nif (${1:/* condition */}) {\n\t${2:/* code */}\n}\nelse if(${3:/* condition */}) {\n\t${4:/* code */}\n}\nelse {\n\t${0:/* code */}\n}\n",
47+
"description": "Code snippet for if() else if() else",
48+
"scope": "source.c, source.objc, source.c++, source.objc++"
49+
},
50+
"else": {
51+
"prefix": "else",
52+
"body": "\nelse\n{\n\t${0:/* code */}\n}\n",
53+
"description": "Code snippet for else",
54+
"scope": "source.c, source.objc, source.c++, source.objc++"
55+
},
56+
"elif": {
57+
"prefix": "elif",
58+
"body": "\nelse if (${1:/* condition */})\n{\n\t${0:/* code */}\n}\n",
59+
"description": "Code snippet for else if ()",
60+
"scope": "source.c, source.objc, source.c++, source.objc++"
61+
},
62+
"switch": {
63+
"prefix": "switch",
64+
"body": "\nswitch (${1:expression})\n{\n\tcase ${2:/* constant-expression */}:\n\t\t${3:/* code */}\n\t\tbreak;\n\n\tdefault:\n\t\tbreak;\n}",
65+
"description": "Code snippet for switch",
66+
"scope": "source.c, source.objc, source.c++, source.objc++"
67+
},
68+
"main": {
69+
"prefix": "main",
70+
"body": "\nint main(int argc, char const *argv[])\n{\n\t${1:/* code */}\n\treturn 0;\n}\n",
71+
"description": "Code snippet for main()",
72+
"scope": "source.c, source.objc, source.c++, source.objc++"
73+
},
74+
"do": {
75+
"prefix": "do_",
76+
"body": "\ndo\n{\n\t${0:/* code */}\n} while (${1:/* condition */});\n",
77+
"description": "Code snippet for do while loop",
78+
"scope": "source.c, source.objc, source.c++, source.objc++"
79+
},
80+
"for": {
81+
"prefix": "for",
82+
"body": "\nfor(${1:size_t} ${2:i} = 0; $2 < ${3:count}; ${4:$2++})\n{\n\t${0:/* code */}\n}\n",
83+
"description": "Code snippet for for loop",
84+
"scope": "source.c, source.objc, source.c++, source.objc++"
85+
},
86+
"forr": {
87+
"prefix": "forr",
88+
"body": "\nfor(int ${1:i} = ${2:length} - 1; $1 >= 0; ${3:$1--})\n{\n\t${0:/* code */}\n}\n",
89+
"description": "Code snippet for reverse for loop",
90+
"scope": "source.c, source.objc, source.c++, source.objc++"
91+
},
92+
"foreach": {
93+
"prefix": "foreach",
94+
"body": "\nfor(${1:object} = ${2:var} in ${3:collection})\n{\n\t${0:/* code */}\n}\n",
95+
"description": "Code snippet for foreach loop",
96+
"scope": "source.c, source.objc, source.c++, source.objc++"
97+
},
98+
"forrange": {
99+
"prefix": "forrange",
100+
"body": "\nfor(auto&& ${1:i} : ${2:v})\n{\n\t$0\n}\n",
101+
"description": "Code snippet for range-based forloop",
102+
"scope": "source.c, source.objc, source.c++, source.objc++"
103+
},
104+
"while": {
105+
"prefix": "while",
106+
"body": "\nwhile(${1:/* condition */}){\n\t${2:/* code */}\n}\n",
107+
"description": "Code snippet for while loop",
108+
"scope": "source.c, source.objc, source.c++, source.objc++"
109+
},
110+
"ifd": {
111+
"prefix": "ifnd",
112+
"body": "\n#if defined($1)\n\n${0}\n\n#endif // $1\n",
113+
"description": "Code snippet for if defined()",
114+
"scope": "source.c, source.objc, source.c++, source.objc++"
115+
},
116+
"ifnd": {
117+
"prefix": "ifnd",
118+
"body": "\n#if !defined($1)\n#define ${1:MACRO}\n\n${0}\n\n#endif // $1\n",
119+
"description": "Code snippet for if !defined()",
120+
"scope": "source.c, source.objc, source.c++, source.objc++"
121+
},
122+
"mitl": {
123+
"prefix": "mitl",
124+
"body": "\n// The MIT License (MIT)\n\n// Copyright (c) ${1:YEAR} ${2:NAME}\n\n// Permission is hereby granted, free of charge, to any person obtaining a\n// copy of this software and associated documentation files (the \"Software\"),\n// to deal in the Software without restriction, including without limitation\n// the rights to use, copy, modify, merge, publish, distribute, sublicense,\n// and/or sell copies of the Software, and to permit persons to whom the\n// Software is furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS\n// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\n// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER\n// DEALINGS IN THE SOFTWARE.\n\n${0:/* code */}\n",
125+
"description": "Code snippet for MIT License",
126+
"scope": ""
127+
},
128+
"namesp": {
129+
"prefix": "namesp",
130+
"body": "\nnamespace ${1:name}\n{\n\t$0\n} // $1\n",
131+
"description": "Code snippet for namespace",
132+
"scope": "source.c++, source.objc++"
133+
},
134+
"try": {
135+
"prefix": "try",
136+
"body": "\ntry\n{\n\t${1:/* code */}\n}\ncatch(${2:const std::exception& e})\n{\n\t${0:std::cerr << e.what() << '\\n';}\n}\n",
137+
"description": "Code snippet for try catch block",
138+
"scope": "source.c++, source.objc++, source.c++11"
139+
}
140+
}

Extension/package.json

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"Languages",
3636
"Debuggers",
3737
"Formatters",
38-
"Linters"
38+
"Linters",
39+
"Snippets"
3940
],
4041
"activationEvents": [
4142
"*"
@@ -421,6 +422,11 @@
421422
"title": "Navigate...",
422423
"category": "C/Cpp"
423424
},
425+
{
426+
"command": "C_Cpp.ToggleSnippets",
427+
"title": "Toggle Snippets",
428+
"category": "C/Cpp"
429+
},
424430
{
425431
"command": "C_Cpp.ToggleErrorSquiggles",
426432
"title": "Toggle Error Squiggles",
@@ -1252,7 +1258,17 @@
12521258
"[c]": {
12531259
"editor.autoIndent": false
12541260
}
1255-
}
1261+
},
1262+
"snippets": [
1263+
{
1264+
"language": "cpp",
1265+
"path": "./cpp_snippets.json"
1266+
},
1267+
{
1268+
"language": "c",
1269+
"path": "./cpp_snippets.json"
1270+
}
1271+
]
12561272
},
12571273
"scripts": {
12581274
"compile": "npm run vscode:prepublish",
@@ -1458,4 +1474,4 @@
14581474
"binaries": []
14591475
}
14601476
]
1461-
}
1477+
}

Extension/src/LanguageServer/extension.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ function registerCommands(): void {
206206
disposables.push(vscode.commands.registerCommand('C_Cpp.ConfigurationEdit', onEditConfiguration));
207207
disposables.push(vscode.commands.registerCommand('C_Cpp.AddToIncludePath', onAddToIncludePath));
208208
disposables.push(vscode.commands.registerCommand('C_Cpp.ToggleErrorSquiggles', onToggleSquiggles));
209+
disposables.push(vscode.commands.registerCommand('C_Cpp.ToggleSnippets', onToggleSnippets));
209210
disposables.push(vscode.commands.registerCommand('C_Cpp.ToggleIncludeFallback', onToggleIncludeFallback));
210211
disposables.push(vscode.commands.registerCommand('C_Cpp.ToggleDimInactiveRegions', onToggleDimInactiveRegions));
211212
disposables.push(vscode.commands.registerCommand('C_Cpp.ShowReleaseNotes', onShowReleaseNotes));
@@ -343,6 +344,47 @@ function onToggleSquiggles(): void {
343344
settings.toggleSetting("errorSquiggles", "Enabled", "Disabled");
344345
}
345346

347+
function onToggleSnippets(): void {
348+
onActivationEvent();
349+
350+
// This will apply to all clients as it's a global toggle. It will require a reload.
351+
const snippetsCatName: string = "Snippets";
352+
let newPackageJson: any = util.getRawPackageJson();
353+
354+
if (newPackageJson.categories.findIndex(cat => cat === snippetsCatName) === -1) {
355+
// Add the Snippet category and snippets node.
356+
357+
newPackageJson.categories.push(snippetsCatName);
358+
newPackageJson.contributes.snippets = [{"language": "cpp", "path": "./cpp_snippets.json"}, {"language": "c", "path": "./cpp_snippets.json"}];
359+
360+
fs.writeFile(util.getPackageJsonPath(), util.stringifyPackageJson(newPackageJson), () => {
361+
showReloadPrompt("Reload Window to finish enabling C++ snippets");
362+
});
363+
364+
} else {
365+
// Remove the category and snippets node.
366+
let ndxCat: number = newPackageJson.categories.indexOf(snippetsCatName);
367+
if (ndxCat !== -1) {
368+
newPackageJson.categories.splice(ndxCat, 1);
369+
}
370+
371+
delete newPackageJson.contributes.snippets;
372+
373+
fs.writeFile(util.getPackageJsonPath(), util.stringifyPackageJson(newPackageJson), () => {
374+
showReloadPrompt("Reload Window to finish disabling C++ snippets");
375+
});
376+
}
377+
}
378+
379+
function showReloadPrompt(msg: string): void {
380+
let reload: string = "Reload";
381+
vscode.window.showInformationMessage(msg, reload).then(value => {
382+
if (value === reload) {
383+
vscode.commands.executeCommand("workbench.action.reloadWindow");
384+
}
385+
});
386+
}
387+
346388
function onToggleIncludeFallback(): void {
347389
onActivationEvent();
348390
// This only applies to the active client.

Extension/src/commands.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class TemporaryCommandRegistrar {
2222
"C_Cpp.ToggleErrorSquiggles",
2323
"C_Cpp.ToggleIncludeFallback",
2424
"C_Cpp.ToggleDimInactiveRegions",
25+
"C_Cpp.ToggleSnippets",
2526
"C_Cpp.ShowReleaseNotes",
2627
"C_Cpp.ResetDatabase",
2728
"C_Cpp.PauseParsing",

Extension/src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ function rewriteManifest(): Promise<void> {
307307
"onCommand:C_Cpp.ToggleErrorSquiggles",
308308
"onCommand:C_Cpp.ToggleIncludeFallback",
309309
"onCommand:C_Cpp.ToggleDimInactiveRegions",
310+
"onCommand:C_Cpp.ToggleSnippets",
310311
"onCommand:C_Cpp.ShowReleaseNotes",
311312
"onCommand:C_Cpp.ResetDatabase",
312313
"onCommand:C_Cpp.PauseParsing",

0 commit comments

Comments
 (0)