vscode:C文件格式规范之代码片段

一、简介

项目开发中,经常需要创建.c 和 .h等文件,为了代码的美观性和整洁性,本文引入一个vscode snippets 的功能实现生成代码模版

二、官方文档资料

官方文档:https://code.visualstudio.com/docs/editor/userdefinedsnippets

三、使用步骤

1、启用Tab补全

打开vscode的设置,输入“editor.tabCompletion” 开启补全功能“ON
在这里插入图片描述

2、选择自定义代码片段

快捷键:ctrl+shift+p
输入:configure user snippets
在这里插入图片描述

输入对应的语言:c, cpp,回车,c语言对应配置文件是c.json,cpp语言对应配置文件是cpp.json
注意:VScode会把*.h头文件识别成cpp代码,所以在编写*.h文件代码时,生效的是cpp.json配置,所以这里也需要添加cpp的配置

在这里插入图片描述
将下面内容分别复制到vscode中
c.json

在这里插入代码片`{
    // Place your snippets for c here. Each snippet is defined under a snippet name and has a prefix, body and
    // description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
    // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
    // same ids are connected.
    // Example:
    // "Print to console": {
    //  "prefix": "log",
    //  "body": [
    //      "console.log('$1');",
    //      "$2"
    //  ],
    //  "description": "Log output to console"
    // },
    "init main": {
        "prefix": "main",
        "body": [
            "#include <stdio.h>",
            "#include <stdlib.h>",
            "#include <string.h>",
            "",
            "int main(int argc, char* argv[])",
            "{",
            "    $0",
            "    return 0;",
            "}"
        ],
        "description": "Basic main function"
    },
    "Print format": {
        "prefix": "prin",
        "body": [
            "printf(\"$1\",$0);"
        ],
        "description": "Quick print format"
    },
    "Print message": {
        "prefix": "pr",
        "body": [
            "printf(\"$0\");"
        ],
        "description": "Quick print format"
    },
    "sizeof": {
        "prefix": "si",
        "body": [
            "sizeof(${1:name})"
        ],
        "description": "sizeof(name)"
    },
    "for": {
        "prefix": "for",
        "body": [
            "for (${int} ${i} = ${1:0}; ${i} < ${2:length}; ${i}++)",
            "{",
            "    $0",
            "}"
        ],
        "description": "Code snippet for 'for' loop"
    },
    "forj": {
        "prefix": "forj",
        "body": [
            "for (${int} ${j} = ${1:0}; ${j} < ${2:length}; ${j}++)",
            "{",
            "    $0",
            "}"
        ],
        "description": "Code snippet for 'for' loop"
    },
    "fork": {
        "prefix": "fork",
        "body": [
            "for (${size_t} ${k} = ${1:0}; ${k} < ${2:length}; ${k}++)",
            "{",
            "    $0",
            "}"
        ],
        "description": "Code snippet for 'for' loop"
    },
    "forr": {
        "prefix": "forr",
        "body": [
            "for (int ${i} = ${1:length} - 1; ${i} >= ${2:0}; ${i}--)",
            "{",
            "    $0",
            "}"
        ],
        "description": "Code snippet for reverse 'for' loop"
    },
    "while": {
        "prefix": "while",
        "body": [
            "while ($1)",
            "{",
            "    $0",
            "}"
        ],
        "description": ""
    },
    "if": {
        "prefix": "if",
        "body": [
            "if ($1)",
            "{",
            "    $0",
            "}"
        ],
        "description": "Code snippet for if statement"
    },
    "else": {
        "prefix": "else",
        "body": [
            "else",
            "{",
            "    $0",
            "}"
        ],
        "description": "Code snippet for else statement"
    },
    "else if": {
        "prefix": "else if",
        "body": [
            "else if ($1)",
            "{",
            "    $0",
            "}"
        ],
        "description": "Code snippet for else-if statement"
    },
    "#ifdef": {
        "prefix": "#ifdef",
        "body": [
            "#ifdef ${DEBUG}",
            "$0",
            "#endif // ${DEBUG}"
        ],
        "description": "Code snippet for #ifdef"
    },
    "#ifndef": {
        "prefix": "#ifndef",
        "body": [
            "#ifndef ${1:1}",
            "$0",
            "#endif // !$1"
        ],
        "description": "Code snippet for #ifndef"
    },
    "#if": {
        "prefix": "#if",
        "body": [
            "#ifdef ${1:0}",
            "$0",
            "#endif // $1"
        ],
        "description": "Code snippet for #if"
    },
    "struct": {
        "prefix": "str",
        "body": [
            "struct ${1:MyStruct}",
            "{",
            "    $0",
            "};"
        ],
        "description": "Code snippet for struct"
    },
    "enum": {
        "prefix": "enum",
        "body": [
            "enum ${1:MyEnum}",
            "{",
            "    $0",
            "};"
        ],
        "description": "Code snippet for enum"
    },
    "union": {
        "prefix": "union",
        "body": [
            "union ${MyUnion}",
            "{",
            "    $0",
            "};"
        ],
        "description": "Code snippet for union"
    },
    "switc": {
        "prefix": "sw",
        "body": [
            "switch($1)",
            "{",
            "    {",
            "    case $2:",
            "        $0",
            "        break;",
            "    default:",
            "        ",
            "        break;",
            "    }",
            "}"
        ],
        "description": "switch(){}"
    },
    "switch2": {
        "prefix": "switch",
        "body": [
            "switch($1){",
            "{",
            "    case $2:",
            "        $0",
            "        break;",
            "    default:",
            "        ",
            "        break;",
            "}"
        ],
        "description": "switch(){}"
    },
    "case": {
        "prefix": "ca",
        "body": [
            "case $1:",
            "    $0",
            "    break;"
        ],
        "description": "case:break;"
    },
    "case2": {
        "prefix": "case",
        "body": [
            "case $1:",
            "    $0",
            "    break;"
        ],
        "description": "case:break;"
    },
    "#inc": {
        "prefix": "#in",
        "body": [
            "#include \"$1\""
        ],
        "description": "Code snippet for #include \" \""
    },
    "#incl": {
        "prefix": "#inc<",
        "body": [
            "#include <$1>"
        ],
        "description": "Code snippet for #include \" \""
    },
    "#def": {
        "prefix": "#def",
        "body": [
            "#define \"$1\" \"$2\" "
        ],
        "description": "Code snippet for #define \" \""
    },
    "add  fun(){}": {
        "prefix": "fun",
        "body": [
            "${1:functionname}(${2:void})",
            "{",
            "    $0",
            "}"
        ],
        "description": "add @funtionname(@arg){}"
    },
    "add fun(){}": {
        "prefix": "fu",
        "body": [
            "${1:functionname}(${2:args})",
            "{",
            "    $0",
            "}"
        ],
        "description": "add @funtionname(@arg){}"
    },
    "template_h": {
        "prefix": "template_h",
        "body": [
            "/*",
            " * Project: ${WORKSPACE_NAME}",
            " * Module: ${1:module_name}",
            " * File: ${TM_FILENAME}",
            " * Created Date: ${CURRENT_YEAR}-${CURRENT_MONTH}-${CURRENT_DATE} ${CURRENT_HOUR}:${CURRENT_MINUTE}:${CURRENT_SECOND}",
            " * Author: ${2:your_name}",
            " * Description: ${3:description}",
            " * -----",
            " * todo: modified ",
            " * -----",
            " * Copyright (c) ${CURRENT_YEAR} -Inc",
            " */",
            "#ifndef ${1:module_name}_${TM_FILENAME_BASE/(.*)/${1:/upcase}/}_H",
            "#define ${1:module_name}_${TM_FILENAME_BASE/(.*)/${1:/upcase}/}_H",
            "",
            "/* ======================================================================================",
            " * includes",
            " */",
            "#include \"base.h\"",
            "",
            "/* ======================================================================================",
            " * extern",
            " */",
            "__vd_extern_c_enter__;",
            "",
            "/* ======================================================================================",
            " * macros",
            " */",
            "",
            "/* ======================================================================================",
            " * types",
            " */",
            "",
            "/* ======================================================================================",
            " * globals",
            " */",
            "",
            "/* ======================================================================================",
            " * declaration",
            " */",
            "",
            "/* ======================================================================================",
            " * extern",
            " */ ",
            "__vd_extern_c_leave__",
            "#endif // ${1:module_name}_${TM_FILENAME_BASE/(.*)/${1:/upcase}/}_H"
        ],
        "description": "add template header"
    },
    "template_modify": {
        "prefix": "template_modify",
        "body": [
            " * Last Modified: ${CURRENT_YEAR}-${CURRENT_MONTH}-${CURRENT_DATE} ${CURRENT_HOUR}:${CURRENT_MINUTE}:${CURRENT_SECOND}",
            " * Modified By: ${1:your_name}",
            " * Comments: ${2:comments}"
        ],
        "description": "add modify info"
    },
    "template_c": {
        "prefix": "template_c",
        "body": [
            "/*",
            " * Project: ${WORKSPACE_NAME}",
            " * Module: ${1:module_name}",
            " * File: ${TM_FILENAME}",
            " * Created Date: ${CURRENT_YEAR}-${CURRENT_MONTH}-${CURRENT_DATE} ${CURRENT_HOUR}:${CURRENT_MINUTE}:${CURRENT_SECOND}",
            " * Author: ${2:your_name}",
            " * Description: ${3:description}",
            " * -----",
            " * todo: modified ",
            " * -----",
            " * Copyright (c) ${CURRENT_YEAR} - Inc",
            " */",
            "",
            "/* ======================================================================================",
            " * log",
            " */",
            "#define LOG_ENABLE_DEBUG (1)",
            "",
            "/* ======================================================================================",
            " * includes",
            " */",
            "#include \"${TM_FILENAME_BASE}.h\"",
            "",
            "/* ======================================================================================",
            " * macros",
            " */",
            "",
            "/* ======================================================================================",
            " * types",
            " */",
            "",
            "/* ======================================================================================",
            " * declaration",
            " */",
            "",
            "/* ======================================================================================",
            " * globals",
            " */",
            "",
            "/* ======================================================================================",
            " * helper",
            " */",
            "",
            "/* ======================================================================================",
            " * private implementation",
            " */",
            "",
            "/* ======================================================================================",
            " * implementation",
            " */",
            ""
        ]
    }
}
`

cpp.json

{
    // Place your snippets for cpp here. Each snippet is defined under a snippet name and has a prefix, body and
    // description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
    // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
    // same ids are connected.
    // Example:
    // "Print to console": {
    //  "prefix": "log",
    //  "body": [
    //      "console.log('$1');",
    //      "$2"
    //  ],
    //  "description": "Log output to console"
    // }
    "main": {
        "prefix": "main",
        "body": [
            "#include <iostream>",
            "",
            "using namespace std;",
            "",
            "vd_int_t main(vd_int_t argc, vd_char_t* argv[]) {",
            "    $0",
            "    return 0;",
            "}"
        ]
    },
    "for": {
        "prefix": "for",
        "body": [
            "for (${int} ${i} = ${1:0}; ${i} < ${2:length}; ++${i}) {",
            "    $3",
            "}"
        ],
        "description": "Code snippet for 'for' loop"
    },
    "forj": {
        "prefix": "forj",
        "body": [
            "for (${int} ${j} = ${1:0}; ${j} < ${2:length}; ++${j}) {",
            "    $3",
            "}"
        ],
        "description": "Code snippet for 'for' loop"
    },
    "fork": {
        "prefix": "fork",
        "body": [
            "for (${size_t} ${k} = ${1:0}; ${k} < ${2:length}; ++${k}) {",
            "    $3",
            "}"
        ],
        "description": "Code snippet for 'for' loop"
    },
    "do": {
        "prefix": "do",
        "body": [
            "do {",
            "    $1",
            "} while($2);"
        ],
        "description": "Code snippet for do...while loop"
    },
    "while": {
        "prefix": "while",
        "body": [
            "while ($1) {",
            "    $2",
            "}"
        ],
        "description": ""
    },
    "foreach": {
        "prefix": "foreach",
        "body": [
            "for(auto &${1:val} : ${2:collection_to_loop}) {",
            "    $0",
            "}"
        ],
        "description": "Code snippet for range-based for loop (c++11) statement"
    },
    "if": {
        "prefix": "if",
        "body": [
            "if ($0) {",
            // "    $2",
            "}"
        ],
        "description": "Code snippet for if statement"
    },
    "else": {
        "prefix": "else",
        "body": [
            "else {",
            "    $1",
            "}"
        ],
        "description": "Code snippet for else statement"
    },
    "else if": {
        "prefix": "else if",
        "body": [
            "else if ($1) {",
            "    $2",
            "}"
        ],
        "description": "Code snippet for else-if statement"
    },
    "enum": {
        "prefix": "enum",
        "body": [
            "enum ${MyEnum} {",
            "    $1",
            "};"
        ],
        "description": "Code snippet for enum"
    },
    "enum class": {
        "prefix": "enum class",
        "body": [
            "enum class ${MyClass} { };"
        ],
        "description": "Code snippet for enum class (c++11)"
    },
    "class": {
        "prefix": "class",
        "body": [
            "class ${MyClass} {",
            "public:",
            "    ${MyClass}();",
            "    ${MyClass}(${MyClass} &&) = default;",
            "    ${MyClass}(const ${MyClass} &) = default;",
            "    ${MyClass} &operator=(${MyClass} &&) = default;",
            "    ${MyClass} &operator=(const ${MyClass} &) = default;",
            "    ~${MyClass}();",
            "",
            "private:",
            "    $1",
            "};",
            "",
            "${MyClass}::${MyClass}() {",
            "}",
            "",
            "${MyClass}::~${MyClass}() {",
            "}"
        ],
        "description": "Code snippet for class"
    },
    "classi": {
        "prefix": "classi",
        "body": [
            "class ${MyClass} {",
            "public:",
            "    ${MyClass}() = default;",
            "    ${MyClass}(${MyClass} &&) = default;",
            "    ${MyClass}(const ${MyClass} &) = default;",
            "    ${MyClass} &operator=(${MyClass} &&) = default;",
            "    ${MyClass} &operator=(const ${MyClass} &) = default;",
            "    ~${MyClass}() = default;",
            "",
            "private:",
            "    $1",
            "};"
        ],
        "description": "Code snippet for class with inline constructor/destructor"
    },
    "interface": {
        "prefix": "interface",
        "body": [
            "__interface I${Interface} {",
            "    $1",
            "};"
        ],
        "description": "Code snippet for interface (Visual C++)"
    },
    "namespace": {
        "prefix": "names",
        "body": [
            "namespace $1 {",
            "    $2",
            "}"
        ]
    },
    "#ifdef": {
        "prefix": "#ifdef",
        "body": [
            "#ifdef ${DEBUG}",
            "$1",
            "#endif // ${DEBUG}"
        ],
        "description": "Code snippet for #ifdef"
    },
    "#ifndef": {
        "prefix": "#ifndef",
        "body": [
            "#ifndef ${1:1}",
            "$2",
            "#endif // !$1"
        ],
        "description": "Code snippet for #ifndef"
    },
    "#if": {
        "prefix": "#if",
        "body": [
            "#ifdef ${1:0}",
            "$2",
            "#endif // $1"
        ],
        "description": "Code snippet for #if"
    },
    "struct": {
        "prefix": "struct",
        "body": [
            "struct ${1:struct_name} {",
            "    $0",
            "};"
        ],
        "description": "Code snippet for struct"
    },
    "switch": {
        "prefix": "switch",
        "body": [
            "switch (${switch_on}) {",
            "default:",
            "    break;$1",
            "}"
        ],
        "description": "Code snippet for switch statement"
    },
    "try": {
        "prefix": "try",
        "body": [
            "try {",
            "    ",
            "} catch (const std::exception&) {",
            "    $1",
            "}"
        ],
        "description": "Code snippet for try catch"
    },
    "union": {
        "prefix": "union",
        "body": [
            "union ${MyUnion} {",
            "    $1",
            "};"
        ],
        "description": "Code snippet for union"
    },
    "cout": {
        "prefix": "cout",
        "body": [
            "std::cout << \"${0} << std::endl;"
        ],
        "description": "Code snippet for printing to std::cout, provided the header is set"
    },
    "coutl": {
        "prefix": "coutl",
        "body": [
            "std::cout << std::endl;"
        ],
        "description": "Code snippet for printing to std::cout, provided the header is set"
    },
    "#inc": {
        "prefix": "#in",
        "body": [
            "#include \"$1\""
        ],
        "description": "Code snippet for #include \" \""
    },
    "#inc<": {
        "prefix": "#inc",
        "body": [
            "#include <$1>"
        ],
        "description": "Code snippet for #include \" \""
    },
    "#def": {
        "prefix": "#def",
        "body": [
            "#define \"$1\" \"$2\" "
        ],
        "description": "Code snippet for #define \" \""
    },
    "template_h": {
        "prefix": "template_h",
        "body": [
            "/*",
            " * Project: ${WORKSPACE_NAME}",
            " * Module: ${1:module_name}",
            " * File: ${TM_FILENAME}",
            " * Created Date: ${CURRENT_YEAR}-${CURRENT_MONTH}-${CURRENT_DATE} ${CURRENT_HOUR}:${CURRENT_MINUTE}:${CURRENT_SECOND}",
            " * Author: ${2:your_name}",
            " * Description: ${3:description}",
            " * -----",
            " * todo: modified ",
            " * -----",
            " * Copyright (c) ${CURRENT_YEAR} - Inc",
            " */",
            "#ifndef ${1:module_name}_${TM_FILENAME_BASE/(.*)/${1:/upcase}/}_H",
            "#define ${1:module_name}_${TM_FILENAME_BASE/(.*)/${1:/upcase}/}_H",
            "",
            "/* ======================================================================================",
            " * includes",
            " */",
            "#include \"base.h\"",
            "",
            "/* ======================================================================================",
            " * extern",
            " */",
            "__vd_extern_c_enter__;",
            "",
            "/* ======================================================================================",
            " * macros",
            " */",
            "",
            "/* ======================================================================================",
            " * types",
            " */",
            "",
            "/* ======================================================================================",
            " * declaration",
            " */",
            "",
            "/* ======================================================================================",
            " * extern",
            " */ ",
            "__vd_extern_c_leave__",
            "#endif // ${1:module_name}_${TM_FILENAME_BASE/(.*)/${1:/upcase}/}_H"
        ],
        "description": "add template header"
    },
    "template_modify": {
        "prefix": "template_modify",
        "body": [
            " * Last Modified: ${CURRENT_YEAR}-${CURRENT_MONTH}-${CURRENT_DATE} ${CURRENT_HOUR}:${CURRENT_MINUTE}:${CURRENT_SECOND}",
            " * Modified By: ${1:your_name}",
            " * Comments: ${2:comments}"
        ],
        "description": "add modify info"
    }
}

模版使用

c源文件,打开对应文件输入,snippet关键字:template_c
在这里插入图片描述在这里插入图片描述
同理,c头文件,打开对应文件输入,snippet关键字:template_h
在这里插入图片描述
若想修改自己的规则,请查看vscode官网,根据对应的规则创建自己的片段

  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值