VS Code自定义C代码模板

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:     Mumingliang
  *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)
{
    
    
	
}

设置步骤:

左下脚设置,选择用户代码片段
在这里插入图片描述
因为我们还要创建.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:     Mumingliang",
		   "  *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 添加代码片段(代码模板,注释模板等)

猜你喜欢

转载自blog.csdn.net/weixin_42663377/article/details/109968174