From cdd6a43fdb24f5d5cccb124fb2305b6a169df18b Mon Sep 17 00:00:00 2001 From: MDXZ-delti Date: Sat, 20 Feb 2021 11:57:19 +0100 Subject: [PATCH 1/6] Improve code readability --- snippets/language-c.cson | 119 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 111 insertions(+), 8 deletions(-) diff --git a/snippets/language-c.cson b/snippets/language-c.cson index eedf889..1cd2201 100644 --- a/snippets/language-c.cson +++ b/snippets/language-c.cson @@ -1,113 +1,216 @@ +# Snippets for C, C++, Obj-C and Obj-C++ '.source.c, .source.cpp, .source.objc, .source.objcpp': '#ifndef … #define … #endif': 'prefix': 'def' 'body': '#ifndef ${1:SYMBOL}\n#define $1 ${2:value}\n#endif' + '#include <>': 'prefix': 'Inc' 'body': '#include <${1:.h}>' + '#include ""': 'prefix': 'inc' 'body': '#include "${1:.h}"' + '#pragma mark': 'prefix': 'mark' 'body': '#if 0\n${1:#pragma mark -\n}#pragma mark $2\n#endif\n\n$0' + 'main()': 'prefix': 'main' 'body': 'int main(int argc, char const *argv[]) {\n\t${1:/* code */}\n\treturn 0;\n}' + 'For Loop': 'prefix': 'for' 'body': 'for (size_t ${1:i} = 0; ${1:i} < ${2:count}; ${1:i}${3:++}) {\n\t${4:/* code */}\n}' + 'Header Include-Guard': 'prefix': 'once' 'body': '#ifndef ${1:SYMBOL}\n#define $1\n\n${2}\n\n#endif /* end of include guard: $1 */\n' + 'Shared Pointer': 'prefix': 'sp' 'body': 'typedef std::shared_ptr<${2:${1:my_type}_t}> ${3:${4:my_type}_ptr};' + 'Typedef': 'prefix': 'td' 'body': 'typedef ${1:int} ${2:MyCustomType};' + 'Do While Loop': 'prefix': 'do' 'body': 'do {\n\t${0:/* code */}\n} while(${1:/* condition */});' + 'While Loop': 'prefix': 'while' 'body': 'while (${1:/* condition */}) {\n\t${2:/* code */}\n}' + 'fprintf': 'prefix': 'fprintf' 'body': 'fprintf(${1:stderr}, "${2:%s}\\\\n", $3);$4' + 'If Condition': 'prefix': 'if' 'body': 'if (${1:/* condition */}) {\n\t${2:/* code */}\n}' + 'If Else': 'prefix': 'ife' - 'body': 'if (${1:/* condition */}) {\n\t${2:/* code */}\n} else {\n\t${3:/* code */}\n}' + 'body': ''' + if (${1:/* condition */}) { + ${2:/* code */} + } else { + ${3:/* code */} + } + ''' + 'If ElseIf': 'prefix': 'iff' - 'body': 'if (${1:/* condition */}) {\n\t${2:/* code */}\n} else if (${3:/* condition */}) {\n\t${4:/* code */}\n}' + 'body': ''' + if (${1:/* condition */}) { + ${2:/* code */} + } else if (${3:/* condition */}) { + ${4:/* code */} + } + ''' + 'If ElseIf Else': 'prefix': 'iffe' - 'body': 'if (${1:/* condition */}) {\n\t${2:/* code */}\n} else if (${3:/* condition */}) {\n\t${4:/* code */}\n} else {\n\t${5:/* code */}\n}' + 'body': ''' + if (${1:/* condition */}) { + ${2:/* code */} + } else if (${3:/* condition */}) { + ${4:/* code */} + } else { + ${5:/* code */} + } + ''' + 'Switch Statement': 'prefix': 'switch' 'body': 'switch (${1:/* expression */}) {\n\tcase ${2:/* value */}:\n}' + 'case': 'prefix': 'cs' 'body': 'case ${1:/* value */}:$0' + 'printf': 'prefix': 'printf' 'body': 'printf("${1:%s}\\\\n", $2);$3' + 'scanf': 'prefix': 'scanf' 'body': 'scanf(\"${1:%s}\\\\n\", $2);$3' + 'Struct': 'prefix': 'st' 'body': 'struct ${1:name_t} {\n\t${2:/* data */}\n};' + 'void': 'prefix': 'void' 'body': 'void ${1:name}(${2:/* arguments */}) {\n\t${3:/* code */}\n}' + 'any function': 'prefix': 'func' 'body': '${1:int} ${2:name}(${3:/* arguments */}) {\n\t${5:/* code */}\n\treturn ${4:0};\n}' + 'write file': 'prefix': 'wf' - 'body': 'FILE *${1:fp};\n${1:fp} = fopen ("${2:filename.txt}","w");\nif (${1:fp}!=NULL)\n{\n\tfprintf(${1:fp},"${3:Some String\\\\n}");\n\tfclose (${1:fp});\n}' + 'body': ''' + FILE *${1:fp}; + ${1:fp} = fopen ("${2:filename.txt}","w"); + if (${1:fp}!=NULL) + { + fprintf(${1:fp},"${3:Some String\\\\n}"); + fclose (${1:fp}); + } + ''' + 'read file': 'prefix': 'rf' - 'body': 'FILE *${1:fp};\n${1:fp} = fopen ("${2:filename.txt}","r");\nif (${1:fp}!=NULL)\n{\n\tfscanf(${1:fp},"${3:Some String\\\\n}", ${3:&var});\n\tfclose (${1:fp});\n}' + 'body': ''' + FILE *${1:fp}; + ${1:fp} = fopen ("${2:filename.txt}","r"); + if (${1:fp}!=NULL) + { + fscanf(${1:fp},"${3:Some String\\\\n}", ${3:&var}); + fclose (${1:fp}); + } + ''' + + + +# Snippets for C++ and Obj-C++ '.source.cpp, .source.objcpp': 'Enumeration': 'prefix': 'enum' 'body': 'enum ${1:name} { $0 };' + 'Class': 'prefix': 'cl' - 'body': 'class ${1:name_t} {\nprivate:\n\t${0:/* data */}\n\npublic:\n\t${1:name_t} (${2:arguments});\n\tvirtual ~${1:name_t} ();\n};' + 'body': ''' + class ${1:name_t} { + private: + ${0:/* data */} + + public: + ${1:name_t} (${2:arguments}); + virtual ~${1:name_t} (); + }; + ''' + 'Namespace': 'prefix': 'ns' 'body': 'namespace ${1:name} {\n\t$2\n} /* $1 */' + 'cout': 'prefix': 'cout' 'body': 'std::cout << \"${1:/* message */}\" << \'\\\\n\';' + 'cin': 'prefix': 'cin' 'body': 'std::cin >> ${1:/* variable */};' + 'cerr': 'prefix': 'cerr' 'body': 'std::cerr << \"${1:/* error message */}\" << \'\\\\n\';' + 'std::map': 'prefix': 'map' 'body': 'std::map<${1:key}, ${2:value}> map$3;' + 'std::string': 'prefix': 'str' 'body': 'std::string' + 'std::vector': 'prefix': 'vector' 'body': 'std::vector<${1:int}> v$2;' + 'template ': 'prefix': 'tp' 'body': 'template ' + 'output file': 'prefix': 'outf' - 'body': 'std::ofstream ${1:afile}("${2:filename.txt}", std::ios::out);\nif (${1:afile}.is_open()) {\n\t${1:afile} << "${3:This is a line.}\\\\n";\n\t${1:afile}.close();\n}' + 'body': ''' + std::ofstream ${1:afile}("${2:filename.txt}", std::ios::out); + if (${1:afile}.is_open()) { + ${1:afile} << "${3:This is a line.}\\\\n"; + ${1:afile}.close(); + } + ''' + 'input file': 'prefix': 'inf' - 'body': 'std::ifstream ${1:afile}("${2:filename.txt}", std::ios::in);\nif (${1:afile}.is_open()) {\n\tstd::string line;\n\twhile (std::getline(${1:afile}, line)) {\n\t\tstd::cout << line << \'\\\\n\';\n\t}\n\t${1:afile}.close();\n}\nelse {\n\tstd::cerr << "Unable to open file\\\\n";\n}' + 'body': ''' + std::ifstream ${1:afile}("${2:filename.txt}", std::ios::in); + if (${1:afile}.is_open()) { + std::string line; + while (std::getline(${1:afile}, line)) { + std::cout << line << '\\\\n'; + } + ${1:afile}.close(); + } + else { + std::cerr << "Unable to open file\\\\n"; + } + ''' From 4ba7914dcea3bea088e3788af98cf30a3101e46a Mon Sep 17 00:00:00 2001 From: MDXZ-delti Date: Sat, 20 Feb 2021 12:22:04 +0100 Subject: [PATCH 2/6] Improve snippet consistency and quality --- snippets/language-c.cson | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/snippets/language-c.cson b/snippets/language-c.cson index 1cd2201..9b021dd 100644 --- a/snippets/language-c.cson +++ b/snippets/language-c.cson @@ -6,11 +6,11 @@ '#include <>': 'prefix': 'Inc' - 'body': '#include <${1:.h}>' + 'body': '#include <${1:filename}.h>$2' '#include ""': 'prefix': 'inc' - 'body': '#include "${1:.h}"' + 'body': '#include "${1:filename}.h"$2' '#pragma mark': 'prefix': 'mark' @@ -98,7 +98,7 @@ 'scanf': 'prefix': 'scanf' - 'body': 'scanf(\"${1:%s}\\\\n\", $2);$3' + 'body': 'scanf("${1:%s}\\\\n", $2);$3' 'Struct': 'prefix': 'st' @@ -116,11 +116,11 @@ 'prefix': 'wf' 'body': ''' FILE *${1:fp}; - ${1:fp} = fopen ("${2:filename.txt}","w"); - if (${1:fp}!=NULL) + ${1:fp} = fopen ("${2:filename.txt}", "w"); + if (${1:fp} != NULL) { - fprintf(${1:fp},"${3:Some String\\\\n}"); - fclose (${1:fp}); + fprintf(${1:fp}, "${3:Some String\\\\n}"); + fclose(${1:fp}); } ''' @@ -128,11 +128,11 @@ 'prefix': 'rf' 'body': ''' FILE *${1:fp}; - ${1:fp} = fopen ("${2:filename.txt}","r"); - if (${1:fp}!=NULL) + ${1:fp} = fopen ("${2:filename.txt}", "r"); + if (${1:fp} != NULL) { - fscanf(${1:fp},"${3:Some String\\\\n}", ${3:&var}); - fclose (${1:fp}); + fscanf(${1:fp}, "${3:Some String\\\\n}", ${3:&var}); + fclose(${1:fp}); } ''' @@ -148,12 +148,12 @@ 'prefix': 'cl' 'body': ''' class ${1:name_t} { - private: - ${0:/* data */} + private: + ${0:/* data */} - public: - ${1:name_t} (${2:arguments}); - virtual ~${1:name_t} (); + public: + ${1:name_t} (${2:arguments}); + virtual ~${1:name_t} (); }; ''' @@ -163,7 +163,7 @@ 'cout': 'prefix': 'cout' - 'body': 'std::cout << \"${1:/* message */}\" << \'\\\\n\';' + 'body': 'std::cout << "${1:/* message */}" << \'\\\\n\';' 'cin': 'prefix': 'cin' @@ -171,7 +171,7 @@ 'cerr': 'prefix': 'cerr' - 'body': 'std::cerr << \"${1:/* error message */}\" << \'\\\\n\';' + 'body': 'std::cerr << "${1:/* error message */}" << \'\\\\n\';' 'std::map': 'prefix': 'map' @@ -209,8 +209,7 @@ std::cout << line << '\\\\n'; } ${1:afile}.close(); - } - else { + } else { std::cerr << "Unable to open file\\\\n"; } ''' From 922eeb422c0030c345d9995013e40e9f29b4c10a Mon Sep 17 00:00:00 2001 From: MDXZ-delti Date: Sat, 20 Feb 2021 12:27:00 +0100 Subject: [PATCH 3/6] Move snippets in wrong section --- snippets/language-c.cson | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/snippets/language-c.cson b/snippets/language-c.cson index 9b021dd..d36ea33 100644 --- a/snippets/language-c.cson +++ b/snippets/language-c.cson @@ -28,10 +28,6 @@ 'prefix': 'once' 'body': '#ifndef ${1:SYMBOL}\n#define $1\n\n${2}\n\n#endif /* end of include guard: $1 */\n' - 'Shared Pointer': - 'prefix': 'sp' - 'body': 'typedef std::shared_ptr<${2:${1:my_type}_t}> ${3:${4:my_type}_ptr};' - 'Typedef': 'prefix': 'td' 'body': 'typedef ${1:int} ${2:MyCustomType};' @@ -94,11 +90,11 @@ 'printf': 'prefix': 'printf' - 'body': 'printf("${1:%s}\\\\n", $2);$3' + 'body': 'printf("${1:%s}\\\\n", ${2:str});$3' 'scanf': 'prefix': 'scanf' - 'body': 'scanf("${1:%s}\\\\n", $2);$3' + 'body': 'scanf("${1:%s}\\\\n", ${2:str});$3' 'Struct': 'prefix': 'st' @@ -117,9 +113,8 @@ 'body': ''' FILE *${1:fp}; ${1:fp} = fopen ("${2:filename.txt}", "w"); - if (${1:fp} != NULL) - { - fprintf(${1:fp}, "${3:Some String\\\\n}"); + if (${1:fp} != NULL) { + fprintf(${1:fp}, "${3:Some string\\\\n}"); fclose(${1:fp}); } ''' @@ -129,21 +124,19 @@ 'body': ''' FILE *${1:fp}; ${1:fp} = fopen ("${2:filename.txt}", "r"); - if (${1:fp} != NULL) - { - fscanf(${1:fp}, "${3:Some String\\\\n}", ${3:&var}); + if (${1:fp} != NULL) { + fscanf(${1:fp}, "${3:Some string\\\\n}", ${3:&var}); fclose(${1:fp}); } ''' + 'Enumeration': + 'prefix': 'enum' + 'body': 'enum ${1:name} { $0 };' # Snippets for C++ and Obj-C++ '.source.cpp, .source.objcpp': - 'Enumeration': - 'prefix': 'enum' - 'body': 'enum ${1:name} { $0 };' - 'Class': 'prefix': 'cl' 'body': ''' @@ -185,6 +178,10 @@ 'prefix': 'vector' 'body': 'std::vector<${1:int}> v$2;' + 'Shared Pointer': + 'prefix': 'sp' + 'body': 'typedef std::shared_ptr<${2:${1:my_type}_t}> ${3:${4:my_type}_ptr};' + 'template ': 'prefix': 'tp' 'body': 'template ' From d3a5c36b2f13116fecca9ad7d49291ed8350751b Mon Sep 17 00:00:00 2001 From: MDXZ-delti Date: Sat, 20 Feb 2021 12:43:03 +0100 Subject: [PATCH 4/6] Uniform snippet names and sort them in a logical order --- snippets/language-c.cson | 77 ++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 38 deletions(-) diff --git a/snippets/language-c.cson b/snippets/language-c.cson index d36ea33..1e9e75d 100644 --- a/snippets/language-c.cson +++ b/snippets/language-c.cson @@ -1,54 +1,58 @@ # Snippets for C, C++, Obj-C and Obj-C++ '.source.c, .source.cpp, .source.objc, .source.objcpp': - '#ifndef … #define … #endif': - 'prefix': 'def' - 'body': '#ifndef ${1:SYMBOL}\n#define $1 ${2:value}\n#endif' + 'header guard': + 'prefix': 'once' + 'body': '#ifndef ${1:SYMBOL}\n#define $1\n\n${2}\n\n#endif /* end of include guard: $1 */\n' - '#include <>': + 'include (default)': 'prefix': 'Inc' 'body': '#include <${1:filename}.h>$2' - '#include ""': + 'include (current)': 'prefix': 'inc' 'body': '#include "${1:filename}.h"$2' - '#pragma mark': + 'pragma mark': 'prefix': 'mark' 'body': '#if 0\n${1:#pragma mark -\n}#pragma mark $2\n#endif\n\n$0' - 'main()': + 'define': + 'prefix': 'def' + 'body': '#ifndef ${1:SYMBOL}\n#define $1 ${2:value}\n#endif' + + 'typedef': + 'prefix': 'td' + 'body': 'typedef ${1:int} ${2:MyCustomType};' + + 'enumeration': + 'prefix': 'enum' + 'body': 'enum ${1:name} { $0 };' + + 'struct': + 'prefix': 'st' + 'body': 'struct ${1:name_t} {\n\t${2:/* data */}\n};' + + 'main': 'prefix': 'main' 'body': 'int main(int argc, char const *argv[]) {\n\t${1:/* code */}\n\treturn 0;\n}' - 'For Loop': + 'for': 'prefix': 'for' 'body': 'for (size_t ${1:i} = 0; ${1:i} < ${2:count}; ${1:i}${3:++}) {\n\t${4:/* code */}\n}' - 'Header Include-Guard': - 'prefix': 'once' - 'body': '#ifndef ${1:SYMBOL}\n#define $1\n\n${2}\n\n#endif /* end of include guard: $1 */\n' - - 'Typedef': - 'prefix': 'td' - 'body': 'typedef ${1:int} ${2:MyCustomType};' - - 'Do While Loop': + 'do … while': 'prefix': 'do' 'body': 'do {\n\t${0:/* code */}\n} while(${1:/* condition */});' - 'While Loop': + 'while': 'prefix': 'while' 'body': 'while (${1:/* condition */}) {\n\t${2:/* code */}\n}' - 'fprintf': - 'prefix': 'fprintf' - 'body': 'fprintf(${1:stderr}, "${2:%s}\\\\n", $3);$4' - - 'If Condition': + 'if': 'prefix': 'if' 'body': 'if (${1:/* condition */}) {\n\t${2:/* code */}\n}' - 'If Else': + 'if … else': 'prefix': 'ife' 'body': ''' if (${1:/* condition */}) { @@ -58,7 +62,7 @@ } ''' - 'If ElseIf': + 'if … else if': 'prefix': 'iff' 'body': ''' if (${1:/* condition */}) { @@ -68,7 +72,7 @@ } ''' - 'If ElseIf Else': + 'if … else if … else': 'prefix': 'iffe' 'body': ''' if (${1:/* condition */}) { @@ -80,7 +84,7 @@ } ''' - 'Switch Statement': + 'switch': 'prefix': 'switch' 'body': 'switch (${1:/* expression */}) {\n\tcase ${2:/* value */}:\n}' @@ -92,19 +96,19 @@ 'prefix': 'printf' 'body': 'printf("${1:%s}\\\\n", ${2:str});$3' + 'fprintf': + 'prefix': 'fprintf' + 'body': 'fprintf(${1:stderr}, "${2:%s}\\\\n", $3);$4' + 'scanf': 'prefix': 'scanf' 'body': 'scanf("${1:%s}\\\\n", ${2:str});$3' - 'Struct': - 'prefix': 'st' - 'body': 'struct ${1:name_t} {\n\t${2:/* data */}\n};' - 'void': 'prefix': 'void' 'body': 'void ${1:name}(${2:/* arguments */}) {\n\t${3:/* code */}\n}' - 'any function': + 'function': 'prefix': 'func' 'body': '${1:int} ${2:name}(${3:/* arguments */}) {\n\t${5:/* code */}\n\treturn ${4:0};\n}' @@ -129,15 +133,12 @@ fclose(${1:fp}); } ''' - 'Enumeration': - 'prefix': 'enum' - 'body': 'enum ${1:name} { $0 };' # Snippets for C++ and Obj-C++ '.source.cpp, .source.objcpp': - 'Class': + 'class': 'prefix': 'cl' 'body': ''' class ${1:name_t} { @@ -150,7 +151,7 @@ }; ''' - 'Namespace': + 'namespace': 'prefix': 'ns' 'body': 'namespace ${1:name} {\n\t$2\n} /* $1 */' @@ -178,7 +179,7 @@ 'prefix': 'vector' 'body': 'std::vector<${1:int}> v$2;' - 'Shared Pointer': + 'shared pointer': 'prefix': 'sp' 'body': 'typedef std::shared_ptr<${2:${1:my_type}_t}> ${3:${4:my_type}_ptr};' From 3de71c591a3e6e348bfe79ccb322c06dac03d81c Mon Sep 17 00:00:00 2001 From: MDXZ-delti Date: Sat, 20 Feb 2021 12:47:01 +0100 Subject: [PATCH 5/6] Add fscanf and improve fprintf snippets --- snippets/language-c.cson | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/snippets/language-c.cson b/snippets/language-c.cson index 1e9e75d..de0764d 100644 --- a/snippets/language-c.cson +++ b/snippets/language-c.cson @@ -98,12 +98,16 @@ 'fprintf': 'prefix': 'fprintf' - 'body': 'fprintf(${1:stderr}, "${2:%s}\\\\n", $3);$4' + 'body': 'fprintf(${1:stderr}, "${2:%s}\\\\n", ${3:str});$4' 'scanf': 'prefix': 'scanf' 'body': 'scanf("${1:%s}\\\\n", ${2:str});$3' + 'fscanf': + 'prefix': 'fscanf' + 'body': 'fscanf(${1:fp}, "${2:%s}\\\\n", ${3:str});$4' + 'void': 'prefix': 'void' 'body': 'void ${1:name}(${2:/* arguments */}) {\n\t${3:/* code */}\n}' From 729d038aee5def6be8e6b570138ee9152d24516a Mon Sep 17 00:00:00 2001 From: MDXZ-delti Date: Sat, 20 Feb 2021 12:57:21 +0100 Subject: [PATCH 6/6] Fix whitespace and snippet name --- snippets/language-c.cson | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/snippets/language-c.cson b/snippets/language-c.cson index de0764d..69fd6ac 100644 --- a/snippets/language-c.cson +++ b/snippets/language-c.cson @@ -42,7 +42,7 @@ 'do … while': 'prefix': 'do' - 'body': 'do {\n\t${0:/* code */}\n} while(${1:/* condition */});' + 'body': 'do {\n\t${0:/* code */}\n} while (${1:/* condition */});' 'while': 'prefix': 'while' @@ -120,7 +120,7 @@ 'prefix': 'wf' 'body': ''' FILE *${1:fp}; - ${1:fp} = fopen ("${2:filename.txt}", "w"); + ${1:fp} = fopen("${2:filename.txt}", "w"); if (${1:fp} != NULL) { fprintf(${1:fp}, "${3:Some string\\\\n}"); fclose(${1:fp}); @@ -131,7 +131,7 @@ 'prefix': 'rf' 'body': ''' FILE *${1:fp}; - ${1:fp} = fopen ("${2:filename.txt}", "r"); + ${1:fp} = fopen("${2:filename.txt}", "r"); if (${1:fp} != NULL) { fscanf(${1:fp}, "${3:Some string\\\\n}", ${3:&var}); fclose(${1:fp}); @@ -187,7 +187,7 @@ 'prefix': 'sp' 'body': 'typedef std::shared_ptr<${2:${1:my_type}_t}> ${3:${4:my_type}_ptr};' - 'template ': + 'template': 'prefix': 'tp' 'body': 'template '