VS Code自定義C代碼模闆
分享一下我用的VS Code C注釋模闆,使用時隻需要輸入開頭的幾個字母即可建立代碼模闆。
- cfile_template:c檔案分區符
/* include ------------------------------------------------------------------*/
#include <main.h>
/* private define -----------------------------------------------------------*/
/* private variables --------------------------------------------------------*/
/* public function prototype ------------------------------------------------*/
/* private function prototype -----------------------------------------------*/
- project_template:建立工程說明
/**
*Copyright(C),2020 - 2021, Company Tech. Co., Ltd.
*FileName: main.c
*Date: 2020-11-22 23:20:02
*Author: Author
*Version: 1.0
*Path: D:\STC15L\user
*Description:
*/
- hfiletemplate:頭檔案宏
#ifndef __MAIN_H_ //shift+U轉換為大寫
#define __MAIN_H_
typedef struct{
void (_CODE* func)(void);
}Class_t;
extern Class_t Class;
#endif
- function_template:函數注釋和模闆
/**
* @brief Desc
* @param None
* @retval None
* @note None
*/
static void function(void)
{
}
設定步驟:
左下腳設定,選擇使用者代碼片段
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIyVGduV2YfNWawNCM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2cs0TP350MNpXTyklaNBDOsJGcohVYsR2MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL2cjNzMzNxEjMyITMxAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
因為我們還要建立.h檔案的模闆,c.json中的模闆不能用h檔案中,是以要選擇建立全局代碼段檔案。
下面貼出來我用的代碼模闆json文本:
{
// Place your 全局 snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
// is left empty or omitted, the snippet gets applied to all languages. 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": {
// "scope": "javascript,typescript",
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
"New project template": {
"prefix": "project_template",
"body": [
"/**",
" *Copyright(C),2020 - 2021, Company Tech. Co., Ltd.",
" *FileName: ${TM_FILENAME}",
" *Date: ${CURRENT_YEAR}-${CURRENT_MONTH}-${CURRENT_DATE} ${CURRENT_HOUR}:${CURRENT_MINUTE}:${CURRENT_SECOND}",
" *Author: Author",
" *Version: 1.0",
" *Path: ${TM_DIRECTORY}",
" *Description:$1",
"*/"
],
"description": "c function header template"
},
"New .c file template": {
"prefix": "cfile_template",
"body": [
"/* include ------------------------------------------------------------------*/",
"#include ${1:<main.h>}", //可以根據需要更改
"\n\n\n/* private define -----------------------------------------------------------*/",
"\n\n\n/* private variables --------------------------------------------------------*/",
"\n\n\n/* public function prototype ------------------------------------------------*/",
"\n\n\n/* private function prototype -----------------------------------------------*/"
],
"description": "c source file template"
},
"New .h file template": {
"prefix": "hfile_template",
"body": [
"#ifndef __${TM_FILENAME_BASE}_H_ //shift+U轉換為大寫",
"#define __${TM_FILENAME_BASE}_H_", //json變量讀出的小寫不知道怎麼轉換為大寫
"\ntypedef struct{",
"\t${1:void (_CODE* func)(void);}",
"}${2:Class_t};",
"\nextern Class_t Class;",
"\n\n\n\n\n\n\n\n\n\n#endif"
],
"description": "c head file template"
},
"New c function header template": {
"prefix": "function_template",
"body": [
"/**",
" * @brief ${1:Desc}",
" * @param ${2:None}",
" * @retval ${3:None}",
" * @note ${4:None}",
"*/",
"static void ${5:function}(void)",
"{\n\t$6\n}"
],
"description": "c function header template"
}
}
參考:vscode 添加代碼片段(代碼模闆,注釋模闆等)