├── .gitignore ├── .vscode └── launch.json ├── LICENSE ├── README.md ├── images └── cpp.png ├── package.json └── snippets ├── c.json └── cpp.json /.gitignore: -------------------------------------------------------------------------------- 1 | *.vsix -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.1.0", 3 | "configurations": [ 4 | { 5 | "name": "Launch Extension", 6 | "type": "extensionHost", 7 | "request": "launch", 8 | "runtimeExecutable": "${execPath}", 9 | "args": [ 10 | "--extensionDevelopmentPath=${workspaceRoot}" 11 | ] 12 | } 13 | ] 14 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Harsh 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # C/C++ Snippets 2 | This extension for Visual Studio Code adds snippets for C/C++. 3 | 4 | ## Usage 5 | Type a part of the keywords in snippet e.g., "for" and press enter. 6 | 7 | ```cpp 8 | for // Creates a for loop snippet 9 | ``` 10 | 11 | Alternatively, one can also just press Ctrl + Space (works on Windows, Linux, or Mac) to access the available snippets in the editor. 12 | 13 | ## Installation 14 | 15 | 1. Install Visual Studio Code 0.10.1 or higher 16 | 2. Launch VS Code 17 | 3. From the command palette `Ctrl`+`Shift`+`P` (Windows, Linux) or `Cmd`+`Shift`+`P` (OSX) 18 | 4. Type `ext install` or just simply select `Install Extension` 19 | 5. Choose the extension - Cpp Snippets 20 | 6. Relaunch VS Code 21 | 22 | 23 | *Suggestions for improvement are welcome.* 24 | -------------------------------------------------------------------------------- /images/cpp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/one-harsh/vscode-cpp-snippets/f481dcfe34ef410ce71f67500706a3043e81ca03/images/cpp.png -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "CppSnippets", 3 | "publisher": "hars", 4 | "displayName": "C/C++ Snippets", 5 | "description": "Code snippets for C/C++", 6 | "version": "0.0.15", 7 | "engines": { 8 | "vscode": "^0.10.1" 9 | }, 10 | "categories": [ 11 | "Snippets" 12 | ], 13 | "contributes": { 14 | "snippets": [ 15 | { 16 | "language": "c", 17 | "path": "./snippets/c.json" 18 | }, 19 | { 20 | "language": "cpp", 21 | "path": "./snippets/cpp.json" 22 | } 23 | ] 24 | }, 25 | "galleryBanner": { 26 | "color": "#E6E0EA", 27 | "theme": "light" 28 | }, 29 | "repository": { 30 | "type": "git", 31 | "url": "https://github.com/one-harsh/vscode-cpp-snippets" 32 | }, 33 | "icon": "images/cpp.png", 34 | "license": "Check in License in the root folder" 35 | } 36 | -------------------------------------------------------------------------------- /snippets/c.json: -------------------------------------------------------------------------------- 1 | { 2 | "for": { 3 | "prefix": "for", 4 | "body": [ 5 | "for (${size_t} ${i} = ${1:0}; ${i} < ${2:length}; ${i}++)", 6 | "{", 7 | " $3", 8 | "}" 9 | ], 10 | "description": "Code snippet for 'for' loop" 11 | }, 12 | "forr": { 13 | "prefix": "forr", 14 | "body": [ 15 | "for (int ${i} = ${1:length} - 1; ${i} >= ${2:0}; ${i}--)", 16 | "{", 17 | " $3", 18 | "}" 19 | ], 20 | "description": "Code snippet for reverse 'for' loop" 21 | }, 22 | "while": { 23 | "prefix": "while", 24 | "body": [ 25 | "while ($1)", 26 | "{", 27 | " $2", 28 | "}" 29 | ], 30 | "description": "" 31 | }, 32 | "if": { 33 | "prefix": "if", 34 | "body": [ 35 | "if ($1)", 36 | "{", 37 | " $2", 38 | "}" 39 | ], 40 | "description": "Code snippet for if statement" 41 | }, 42 | "else": { 43 | "prefix": "else", 44 | "body": [ 45 | "else", 46 | "{", 47 | " $1", 48 | "}" 49 | ], 50 | "description": "Code snippet for else statement" 51 | }, 52 | "else if": { 53 | "prefix": "else if", 54 | "body": [ 55 | "else if ($1)", 56 | "{", 57 | " $2", 58 | "}" 59 | ], 60 | "description": "Code snippet for else-if statement" 61 | }, 62 | "enum": { 63 | "prefix": "enum", 64 | "body": [ 65 | "enum ${MyEnum}", 66 | "{", 67 | " $1", 68 | "};" 69 | ], 70 | "description": "Code snippet for enum" 71 | }, 72 | "#ifdef": { 73 | "prefix": "#ifdef", 74 | "body": [ 75 | "#ifdef ${DEBUG}", 76 | "$1", 77 | "#endif // ${DEBUG}" 78 | ], 79 | "description": "Code snippet for #ifdef" 80 | }, 81 | "#ifndef": { 82 | "prefix": "#ifndef", 83 | "body": [ 84 | "#ifndef ${1:1}", 85 | "$2", 86 | "#endif // !$1" 87 | ], 88 | "description": "Code snippet for #ifndef" 89 | }, 90 | "#if": { 91 | "prefix": "#if", 92 | "body": [ 93 | "#ifdef ${1:0}", 94 | "$2", 95 | "#endif // $1" 96 | ], 97 | "description": "Code snippet for #if" 98 | }, 99 | "struct": { 100 | "prefix": "struct", 101 | "body": [ 102 | "struct ${MyStruct}", 103 | "{", 104 | " $1", 105 | "};" 106 | ], 107 | "description": "Code snippet for struct" 108 | }, 109 | "switch": { 110 | "prefix": "switch", 111 | "body": [ 112 | "switch (${switch_on})", 113 | "{", 114 | "default:", 115 | " break;$1", 116 | "}" 117 | ], 118 | "description": "Code snippet for switch statement" 119 | }, 120 | "union": { 121 | "prefix": "union", 122 | "body": [ 123 | "union ${MyUnion}", 124 | "{", 125 | " $1", 126 | "};" 127 | ], 128 | "description": "Code snippet for union" 129 | }, 130 | "#inc": { 131 | "prefix": "#inc", 132 | "body": [ 133 | "#include \"$1\"" 134 | ], 135 | "description": "Code snippet for #include \" \"" 136 | }, 137 | "#inc<": { 138 | "prefix": "#inc<", 139 | "body": [ 140 | "#include <$1>" 141 | ], 142 | "description": "Code snippet for #include \" \"" 143 | }, 144 | "#def": { 145 | "prefix": "#def", 146 | "body": [ 147 | "#define \"$1\" \"$2\" " 148 | ], 149 | "description": "Code snippet for #define \" \"" 150 | } 151 | } 152 | -------------------------------------------------------------------------------- /snippets/cpp.json: -------------------------------------------------------------------------------- 1 | { 2 | "for": { 3 | "prefix": "for", 4 | "body": [ 5 | "for (${size_t} ${i} = ${1:0}; ${i} < ${2:length}; ${i}++)", 6 | "{", 7 | " $3", 8 | "}" 9 | ], 10 | "description": "Code snippet for 'for' loop" 11 | }, 12 | "forr": { 13 | "prefix": "forr", 14 | "body": [ 15 | "for (int ${i} = ${1:length} - 1; ${i} >= ${2:0}; ${i}--)", 16 | "{", 17 | " $3", 18 | "}" 19 | ], 20 | "description": "Code snippet for reverse 'for' loop" 21 | }, 22 | "do": { 23 | "prefix": "do", 24 | "body": [ 25 | "do", 26 | "{", 27 | " $1", 28 | "} while($2);" 29 | ], 30 | "description": "Code snippet for do...while loop" 31 | }, 32 | "while": { 33 | "prefix": "while", 34 | "body": [ 35 | "while ($1)", 36 | "{", 37 | " $2", 38 | "}" 39 | ], 40 | "description": "Code snippet for while loop" 41 | }, 42 | "foreach": { 43 | "prefix": "foreach", 44 | "body": [ 45 | "for(auto ${var} : ${collection_to_loop})", 46 | "{", 47 | " $1", 48 | "}" 49 | ], 50 | "description": "Code snippet for range-based for loop (c++11) statement" 51 | }, 52 | "if": { 53 | "prefix": "if", 54 | "body": [ 55 | "if ($1)", 56 | "{", 57 | " $2", 58 | "}" 59 | ], 60 | "description": "Code snippet for if statement" 61 | }, 62 | "else": { 63 | "prefix": "else", 64 | "body": [ 65 | "else", 66 | "{", 67 | " $1", 68 | "}" 69 | ], 70 | "description": "Code snippet for else statement" 71 | }, 72 | "else if": { 73 | "prefix": "else if", 74 | "body": [ 75 | "else if ($1)", 76 | "{", 77 | " $2", 78 | "}" 79 | ], 80 | "description": "Code snippet for else-if statement" 81 | }, 82 | "enum": { 83 | "prefix": "enum", 84 | "body": [ 85 | "enum ${MyEnum}", 86 | "{", 87 | " $1", 88 | "};" 89 | ], 90 | "description": "Code snippet for enum" 91 | }, 92 | "enum class": { 93 | "prefix": "enum class", 94 | "body": [ 95 | "enum class ${MyClass} { };" 96 | ], 97 | "description": "Code snippet for enum class (c++11)" 98 | }, 99 | "class": { 100 | "prefix": "class", 101 | "body": [ 102 | "class ${MyClass}", 103 | "{", 104 | "public:", 105 | " ${MyClass}();", 106 | " ${MyClass}(${MyClass} &&) = default;", 107 | " ${MyClass}(const ${MyClass} &) = default;", 108 | " ${MyClass} &operator=(${MyClass} &&) = default;", 109 | " ${MyClass} &operator=(const ${MyClass} &) = default;", 110 | " ~${MyClass}();", 111 | "", 112 | "private:", 113 | " $1", 114 | "};", 115 | "", 116 | "${MyClass}::${MyClass}()", 117 | "{", 118 | "}", 119 | "", 120 | "${MyClass}::~${MyClass}()", 121 | "{", 122 | "}" 123 | ], 124 | "description": "Code snippet for class" 125 | }, 126 | "classi": { 127 | "prefix": "classi", 128 | "body": [ 129 | "class ${MyClass}", 130 | "{", 131 | "public:", 132 | " ${MyClass}() = default;", 133 | " ${MyClass}(${MyClass} &&) = default;", 134 | " ${MyClass}(const ${MyClass} &) = default;", 135 | " ${MyClass} &operator=(${MyClass} &&) = default;", 136 | " ${MyClass} &operator=(const ${MyClass} &) = default;", 137 | " ~${MyClass}() = default;", 138 | "", 139 | "private:", 140 | " $1", 141 | "};" 142 | ], 143 | "description": "Code snippet for class with inline constructor/destructor" 144 | }, 145 | "interface": { 146 | "prefix": "interface", 147 | "body": [ 148 | "__interface I${Interface}", 149 | "{", 150 | " $1", 151 | "};" 152 | ], 153 | "description": "Code snippet for interface (Visual C++)" 154 | }, 155 | "namespace": { 156 | "prefix": "namespace", 157 | "body": [ 158 | "namespace ${MyNamespace}", 159 | "{", 160 | " $1", 161 | "}" 162 | ], 163 | "description": "Code snippet for namespace" 164 | }, 165 | "#ifdef": { 166 | "prefix": "#ifdef", 167 | "body": [ 168 | "#ifdef ${DEBUG}", 169 | "$1", 170 | "#endif // ${DEBUG}" 171 | ], 172 | "description": "Code snippet for #ifdef" 173 | }, 174 | "#ifndef": { 175 | "prefix": "#ifndef", 176 | "body": [ 177 | "#ifndef ${1:1}", 178 | "$2", 179 | "#endif // !$1" 180 | ], 181 | "description": "Code snippet for #ifndef" 182 | }, 183 | "#if": { 184 | "prefix": "#if", 185 | "body": [ 186 | "#ifdef ${1:0}", 187 | "$2", 188 | "#endif // $1" 189 | ], 190 | "description": "Code snippet for #if" 191 | }, 192 | "struct": { 193 | "prefix": "struct", 194 | "body": [ 195 | "struct ${MyStruct}", 196 | "{", 197 | " $1", 198 | "};" 199 | ], 200 | "description": "Code snippet for struct" 201 | }, 202 | "switch": { 203 | "prefix": "switch", 204 | "body": [ 205 | "switch (${switch_on})", 206 | "{", 207 | "default:", 208 | " break;$1", 209 | "}" 210 | ], 211 | "description": "Code snippet for switch statement" 212 | }, 213 | "try": { 214 | "prefix": "try", 215 | "body": [ 216 | "try", 217 | "{", 218 | " ", 219 | "}", 220 | "catch (const std::exception&)", 221 | "{", 222 | " $1", 223 | "}" 224 | ], 225 | "description": "Code snippet for try catch" 226 | }, 227 | "union": { 228 | "prefix": "union", 229 | "body": [ 230 | "union ${MyUnion}", 231 | "{", 232 | " $1", 233 | "};" 234 | ], 235 | "description": "Code snippet for union" 236 | }, 237 | "cout": { 238 | "prefix": "cout", 239 | "body": [ 240 | "std::cout << \"${1:/* message */}\" << std::endl;" 241 | ], 242 | "description": "Code snippet for printing to std::cout, provided the header is set" 243 | }, 244 | "#inc": { 245 | "prefix": "#inc", 246 | "body": [ 247 | "#include \"$1\"" 248 | ], 249 | "description": "Code snippet for #include \" \"" 250 | }, 251 | "#inc<": { 252 | "prefix": "#inc<", 253 | "body": [ 254 | "#include <$1>" 255 | ], 256 | "description": "Code snippet for #include \" \"" 257 | }, 258 | "#def": { 259 | "prefix": "#def", 260 | "body": [ 261 | "#define \"$1\" \"$2\" " 262 | ], 263 | "description": "Code snippet for #define \" \"" 264 | }, 265 | "main": { 266 | "prefix": "main", 267 | "body": [ 268 | "int main(int argc, const char** argv) {", 269 | " return 0;", 270 | "}" 271 | ], 272 | "description": "Code snippet for main function" 273 | } 274 | } 275 | --------------------------------------------------------------------------------