Vscode自动为文件及函数添加注释及分割线

安装插件

安装插件“Doxygen Documentation Generator”,用来生成注释。
安装插件”C/C++ Snippets”,用来生成文件头、代码块分割线等

Doxygen插件设置

点击“文件”—>”首选项”—>”设置”—>”拓展”—>”Doxygen Docume…”—>”在settings.json中编辑”,在打开的settings.json文件中粘贴以下内容

    "doxdocgen.file.fileTemplate": "@file{indent:12} {name}",
    "doxdocgen.file.versionTag": "@version{indent:12} 1.0",
    "doxdocgen.file.copyrightTag": [
        "@copyright{indent:12} All Rights Reserved, DIY Company "
    ],
    "doxdocgen.file.customTag": [
        "",
        "Change Logs",
        "Author            Date            Version         Note       ",
        "{author}      {date}      1.0             xxxxx       "
    ],
    "doxdocgen.file.fileOrder": [
        "file",
        "brief",
        "version",
        "author",
        "date",
        "empty",
        "copyright",
        "custom",
    ],
    "doxdocgen.generic.authorName": "Your name",
    "doxdocgen.generic.authorTag": "@author{indent:12} {author}",
    "doxdocgen.generic.briefTemplate": "@brief{indent:12} {text}",
    "doxdocgen.generic.dateTemplate": "@date{indent:12} {date}",
    "doxdocgen.generic.paramTemplate": "@param{indent:12} {param} ",
    "doxdocgen.generic.returnTemplate": "@return{indent:12} {type} ",
    "doxdocgen.generic.order": [
        "brief",
        "param",
        "return",
        "version",
        "author",
        "date",
        "custom",
    ],
    "doxdocgen.generic.customTags": [
        "",
        "Change Logs",
        "Author            Date            Version         Note       ",
        "{author}      {date}      1.0             xxxxx       "
    ],
    "doxdocgen.c.firstLine": "/*************************************************************************",
    "doxdocgen.c.lastLine": "*************************************************************************/",
    

C/C++ Snippets插件设置

按下快捷键“Ctrl+Shift+P”,在弹出的窗口中输入snip,点击筛选出来的“首选项:配置用户代码片段”

点击“新建全局代码片段文件”,输入要新建的文件名,如“stm32“,并按回车键确认。
全选,替换为以下内容

{
	// 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"
	// }
	"MainPage Table": {
		"prefix": ".table",
		"body": [
			"",
			"/**@mainpage",
			"  *<table>",
			"  *<tr><th>项目名称 <td>",
			"  *<tr><th>设计负责人 <td>",
			"  *<tr><th>版权 <td>xxxx科技有限公司",
			"  </table>",
			"",
			"  @section ",
			"  * ",
			"",
			"  @section ",
			"  * ",
			"",
			"  @section ",
			"  * ",
			"  */",
			"",
		],
		"description": "MainPage Table"
	},
	"Avoid Repetition": {
		"prefix": ".hinit",
		"body": [
			"\n#ifndef ${1:${TM_FILENAME/(.*)\\.h$/${1:/upcase}_H/i}}\n#define $1\n\n${2:#ifdef __cplusplus\nextern \"C\" {\n#endif}",
			"",
			"/*--------------------------------------------- *\n * Exported includes                            *\n *----------------------------------------------*/",
			"",
			"/*--------------------------------------------- *\n * Exported types                               *\n *----------------------------------------------*/",
			"",
			"/*--------------------------------------------- *\n * Exported constants                           *\n *----------------------------------------------*/",
			"",
			"/*--------------------------------------------- *\n * Exported macro                               *\n *----------------------------------------------*/",
			"",
			"/*--------------------------------------------- *\n * Exported variables                           *\n *----------------------------------------------*/",
			"",
			"/*--------------------------------------------- *\n * Exported function prototypes                 *\n *----------------------------------------------*/",
			"",
			"#ifdef __cplusplus",
			"}",
			"#endif",
			"",
			"#endif  /* $1 */\n"
		],
		"description": "Avoid Repetition"
	},
	"Code Block Separation Line": {
		"prefix": ".line",
		"body": [
			"",
			"/*--------------------------------------------- *\n * Private includes                             *\n *----------------------------------------------*/",
			"",
			"/*--------------------------------------------- *\n * Private types                               *\n *----------------------------------------------*/",
			"",
			"/*--------------------------------------------- *\n * Private constants                           *\n *----------------------------------------------*/",
			"",
			"/*--------------------------------------------- *\n * Private macro                               *\n *----------------------------------------------*/",
			"",
			"/*--------------------------------------------- *\n * Private variables                           *\n *----------------------------------------------*/",
			"",
			"/*--------------------------------------------- *\n * Private function prototypes                 *\n *----------------------------------------------*/",
			"",
		],
		"description": "Avoid Repetition"
	}
}

使用

添加文件头
打开一个文件,例如“test.h“。
在文件最上方第一行输入“/**“并回车,将自动生成文件头注释,如下图所示。
在这里插入图片描述

添加.h文件防重复编译代码
输入“.hinit“并回车,将自动生成防重复编译相关代码、CPP兼容相关代码和程序块分割线,如下图所示。
在这里插入图片描述

添加函数注释
在“function“代码段内声明一个函数后,在函数名上方输入”/**“并回车,将自动生成函数注释,如下图所示。添加数据类型的注释方法与此相同。
在这里插入图片描述

补充

通过Astyle 插件 格式化 C/C++ 代码,可与上面自动生成注释配合使用,设置方法类似,将配置代码放入settings文件即可。
在这里插入图片描述

/** Astyle config */
  "editor.mouseWheelZoom": true, //鼠标放大的
  "editor.rulers": [
    80
  ], //设置标尺
  "astyle.additional_languages": [
    "c",
    "cpp",
  ],
  "astyle.cmd_options": [
    // 预定义风格 -----------------------------------------------------------
    "--style=ansi", //ANSI 风格格式和缩进
    //"--style=kr",               //Kernighan&Ritchie 风格格式和缩进
    // "--style=linux", //Linux 风格格式和缩进
    // "--style=gnu",              //GNU 风格格式和缩进
    //"--style=java",             //Java 风格格式和缩进
    "--indent=spaces=4", //缩进4个空格
    "--indent-preproc-block",
    "--pad-oper", //操作符两端插入一个空格
    "--pad-header",
    "--unpad-paren",
    "--suffix=none",
    "--align-pointer=name",
    "--lineend=linux",
    "--convert-tabs", //TAB转换为空格
    "--verbose",
    // "--delete-empty-lines", //删除多余空行
    //"--pad-paren-in",         //括号内部加入空格
    "--unpad-paren", //移除括号两端多余空格
  ],
  "astyle.executable": "C:\\AStyle\\bin\\AStyle.exe",
  //"files.encoding":"GB2312",//文件编码的,以防中文乱码
  "files.encoding": "utf8",
  "[c]": {
    "editor.defaultFormatter": "chiehyu.vscode-astyle"
  },
  "[cpp]": {
    "editor.defaultFormatter": "chiehyu.vscode-astyle"
  },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值