使用VSCode创建自定义注释及代码段的方法

发布时间:2024年01月18日

使用VSCode创建自定义注释及代码段的方法

1.打开VSCode活动栏中的管理面版

2.点击配置用户用户代码片段。如下图所示
在这里插入图片描述

3.点击新建全局代码片段文件

4.输入代码段文件名,如需注释c语言的代码,则文件命名为c.json

5.以c语言为例,建立的文件默认模板如下:

// "Print to console": {

  //  "prefix": "log",

  //  "body": [

  //    "console.log('$1');",

  //    "$2"

  //  ],

  //  "description": "Log output to console"

  // }

其中"Print to console"为添加代码段的说明。

"prefix"为调用这个代码段的快捷输入,如这里为log,则在VSCode编辑c语言的文本区输入log则可以快速插入"body"里的内容

“body”为代码段的内容,代码每行之间以”,“号分割

”description“为代码段的描述

6.创建一个main文件的头部注释模板(包含佛祖保佑,永无bug):

"Create a new c main file": {
        "prefix": "..file.main",
        
        "body":
        [
            "/*********************************** Copyright (c) ************************************",
            "                                                                                                         ",
            "                                        _ooOoo_                                                           ",
            "                                       o8888888o                                                          ",
            "                                       88\" . \"88                                                          ",
            "                                       (| -_- |)                                                          ",
            "                                       O\\  =  /O                                                          ",
            "                                    ____/`---'\\____                                                       ",
            "                                  .'  \\\\\\\\|     |\\\\\\\\  `.                                                     ",
            "                                 /  \\\\\\\\|||  :  |||//  \\                                                    ",
            "                                /  _||||| -:- |||||-  \\                                                   ",
            "                                |   | \\\\\\\\\\\\  -  /// |   |                                                   ",
            "                                | \\_|  ''\\---/''  |   |                                                   ",
            "                                \\  .-\\__  `-`  ___/-. /                                                   ",
            "                              ___`. .'  /--.--\\  `. . __                                                  ",
            "                           .\"\" '<  `.___\\_<|>_/___.'  >'\"\".                                               ",
            "                           | | :  `- \\`.;`\\ _ /`;.`/ - ` : | |                                            ",
            "                          \\  \\ `-.   \\_ __\\ /__ _/   .-` /  /                                             ",
            "                     ======`-.____`-.___\\_____/___.-`____.-'======                                        ",
            "                                        `=---='                                                           ",
            "                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                         ",
            "                                佛祖保佑        永无BUG                                                    ",
            "                                                                                                          ",
            "**                   It's all a circle,Everything gets a return.                                         ",
            "**---------------------------------File Info-------------------------------------------",
            "** @file:               ${TM_FILENAME}",
            "** @author:             $1${author.body}",
            "** @date:               $CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE",
            "** @version:            V0.0",
            "** @brief:              $2",
            "**                                                                                                        ",
            "**-------------------------------------------------------------------------------------",
            "** @modified:           $3                                                              ",
            "** @date:               $CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE                                         ",
            "** @version:            V0.0                                                                              ",
            "** @description:        $4",
            "** @note:               $5",
            "**-------------------------------------------------------------------------------------",
            "** @copyright:          $6",
            "**************************************************************************************/",
        ],

        "description": "a mainnote mode"
    },
 

其中, T M F I L E N A M E 为当前 c 源文件的文件名,这里如果为 m a i n . c , 则 {TM_FILENAME}为当前c源文件的文件名,这里如果为main.c,则 TMF?ILENAME为当前c源文件的文件名,这里如果为main.c,{TM_FILENAME}会被替换为main.c

$1, $2为制表符的切换标志,通过按下tab键,可从$1的位置快捷跳到$2的位置

$CURRENT_YEAR为当前年份

$CURRENT_MONTH为当前月份

$CURRENT_DATE为当前日期

7.创建一个源文件的头部注释模板

"Create a new c source file": {
        "prefix": "..file.c",
        "isFileTemplate": true,
		"body":
        [
            "/******************************** Copyright (c) ***************************************",
            "**",                  
            "**",                  
            "**",                  
            "**---------------------------------File Info-------------------------------------------",
            "** @file:               ${TM_FILENAME}                                                                     ",
            "** @author:             $1${author.body}                                                              ",
            "** @date:               $CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE                                         ",
            "** @version:            V0.0                                                                              ",
            "** @brief:              $2",
            "**                      ",
            "**-------------------------------------------------------------------------------------",
            "** @modified:           $3                                                             ",
            "** @date:               $CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE                                         ",
            "** @version:            V0.0                                                                              ",
            "** @description:        $2",
            "** @note:               $3",
            "**-------------------------------------------------------------------------------------",
            "** @copyright:          $4",
            "**************************************************************************************/",
        ],

        "description": "a file.c mode"
    },

8.创建一个头文件的头部注释模板

"Create a new c header file": {
        "prefix": "..file.h",
        "isFileTemplate": true,
		"body":
        [
            "/******************************** Copyright (c) ***************************************",
            "**",                  
            "**",                  
            "**",                  
            "**---------------------------------File Info-------------------------------------------",
            "** @file:               ${TM_FILENAME}                                                                           ",
            "** @author:             $2${author.body}                                                             ",
            "** @date:               $CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE                                         ",
            "** @version:            V0.0                                                                              ",
            "** @brief:              $3",
            "**                      ",
            "**-------------------------------------------------------------------------------------",
            "** @modified:           $4                                                              ",
            "** @date:               $CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE                                         ",
            "** @version:            V0.0                                                                              ",
            "** @description:        $5",
            "** @note:               $6",
            "**-------------------------------------------------------------------------------------",
            "** @copyright:          $7",
            "**************************************************************************************/",
            "#ifndef __${1:${TM_FILENAME/(.*)\\.h$/${1:/upcase}_H/i}}",
            "#define __$1",
            "\n\n\n\n",
            "/***************************************************************************************",
            "**  接口数据申明", 
            "**************************************************************************************/\n",
            "\n\n",
            "/***************************************************************************************",
            "**  接口函数申明", 
            "**************************************************************************************/\n",
            "\n\n",
            "#endif\t // __$1 \n",
            "/***************************************************************************************",
            "**  End Of File", 
            "**  在烧写的时候是FLASH中的被占用的空间为:      Code + RO-data + RW-data",
            "**  程序运行的时候,芯片内部RAM使用的空间为:    RW-data + ZI-data",
            "**************************************************************************************/\n",
        ],

        "description": "a file.h mode"
    },

9.创建一个新函数模板

"Create a new func": {
        "prefix": "..func.new",
        
		"body":
        [
            "void ${TM_FILENAME_BASE/(.*)/${1:/upcase}/}_Func(void)",
            "{\n\n\n",
            "}",
        ],

        "description": "a func new mode"
    },

10.创建一个结构体模板

 "Define a struct": {
        "prefix": "..struct",
        "body": "typedef struct \n{\n\t$2\n\t$3\n} ${1:name_t};"
    },

11.创建一个枚举模板

"Define a enum": {
        "prefix": "..enum",
        "body": "typedef enum \n\t{\n\t$2\n\t$3\n} ${1:name_t};"
    },

12.创建一个作者声明模板

"author.body": {
        "prefix": "..author.body",
        "body": "${author.body}",
    },

13.创建一个函数注释模板

"Print to func note": {
        "prefix": "..func.note",
        
		"body":
        [
            "/**************************************************************************************",
            "** @name:              ${TM_SELECTED_TEXT}",
            "** @brief:             $2",
            "** @param:             $3",
            "** @retval:            $4",
            "** @author:            $5${author.body}",
            "** @date:              $CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE",
            "** @version:           V0.0",
            "---------------------------------------------------------------------------------------",
            "** @modified:          $6",
            "** @date:              $CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE",
            "** @version:           V0.0",
            "** @description:       $7",
            "** @note:              $8",
            "**************************************************************************************/",
        ],

        "description": "a funcnote mode"
    },

14.创建一个for循环模板

"For loop": {
        "prefix": "..for",
        "body": [
            "for (${1:int i} = 0; ${1:i} < ${2:count}; ${1:i}${3:++})",
            "{\n\t$4\n}"
        ]
    },

15.创建一个do…while循环模板

 "Do while loop": {
        "prefix": "..do",
        "body": "do {\n\t$0\n} while($1);"
    },

16.创建一个while循环模板

 "While loop": {
        "prefix": "..while",
        "body": "while ($1)\n{\n\t$2\n}"
    },

17.创建一个switch分支选择模板

    "Switch case": {
        "prefix": "..switch",
        "body": [
            "switch ($1) {",
            "case $2:\n\t$3\n\tbreak;\n",
            "default:\n\tbreak;\n}$0"
        ]
    },

18.创建一个printf打印模板

  "printf()": {
        "prefix": "..printf",
        "body": "printf(\"${1:%s }\\n\", $2);$3"
    },
文章来源:https://blog.csdn.net/fancyang/article/details/135675648
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。