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

设置步骤:

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

  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值