您的位置:首页 > 编程语言 > C语言/C++

VScode中自定义用户代码片段(C user snippet)自动生成C语言头文件排除重复包含

2018-08-05 15:41 3287 查看

环境

WINDOWS 2016

VSCode v1.25.1

C语言用户代码片段SNIPPET配置c.json内容

{
// Place your snippets for c here. Each snippet is defined under a snippet name and has a prefix, body and
// description. 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:

"C header file": {
"prefix": "#ifndef",
"body": [
"#ifndef __$1_h__",
"#define __$1_h__",
"",
"#ifdef __$1_h__GLOBAL",
"\t#define __$1_h__EXTERN ",
"#else",
"\t#define __$1_h__EXTERN extern",
"#endif",
"",
"$2",
"#endif // __$1_h__",
"",

],
"description": "C header file define"
}
}


结果

输入#ifndef后按TAB

再输入 AUTO_GEN_SNIPPET , 再按TAB ,

输入 VOID hello(void) 就生成如下了:

#ifndef __AUTO_GEN_SNIPPET_h__
#define __AUTO_GEN_SNIPPET_h__

#ifdef __AUTO_GEN_SNIPPET_h__GLOBAL
#define __AUTO_GEN_SNIPPET_h__EXTERN
#else
#define __AUTO_GEN_SNIPPET_h__EXTERN extern
#endif

VOID HELLO(void);

#endif // __AUTO_GEN_SNIPPET_h__


注意如果是头文件在VSCode中(右下角)要选择语言语言为C,不能为C++或其它,否则没有自动完成提示功能。

C.JSON修改保存后立即生效,不用重新启动VSC.

自动生成一个更复杂的头

自动提取.h头文件名,并把文件名中的-或.改为_

snippet c.json

{
"C head common": {
"prefix": "#ifndef",
"body": [
"/***********************************************************************",
" * @file $TM_FILENAME",
"     ${1:${TM_FILENAME_BASE/(.*)/${1:/upcase}/i}}",
" * @brief  $2 header file",
" * @history",
" * Date       Version Author    description",
" * ========== ======= ========= =======================================",
" * $CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE V1.0    ${3|Wukong.SUN,Bajie.ZHU,Wuji.SA,Sanzhang.TANG,Baoyu.JIA|}   Create",
" *",
" * @Copyright (C)  $CURRENT_YEAR  .cdWFVCEL. all right reserved",
"***********************************************************************/",
"#ifndef __${1/[.-]/_/g}_h__",
"#define __${1/[.-]/_/g}_h__",
"",
"#ifdef __${1/[.-]/_/g}_h__GLOBAL",
"\t#define __${1/[.-]/_/g}_h__EXTERN ",
"#else",
"\t#define __${1/[.-]/_/g}_h__EXTERN extern",
"#endif",
"",
"$4",
"#endif // __${1/[.-]/_/g}_h__",
"",

],
"description": "C header file common define"
},

}


结果

/***********************************************************************
* @file auto-gen.header.h
AUTO-GEN.HEADER
* @brief   header file
* @history
* Date       Version Author    description
* ========== ======= ========= =======================================
* 2018-08-05 V1.0    Wukong.SUN   Create
*
* @Copyright (C)  2018  .cdWFVCEL. all right reserved
***********************************************************************/
#ifndef __AUTO_GEN_HEADER_h__
#define __AUTO_GEN_HEADER_h__

#ifdef __AUTO_GEN_HEADER_h__GLOBAL
#define __AUTO_GEN_HEADER_h__EXTERN
#else
#define __AUTO_GEN_HEADER_h__EXTERN extern
#endif

#endif // __AUTO_GEN_HEADER_h__


配置参考

https://www.cnblogs.com/a14907/p/6180244.html : [VS Code]跟我一起在Visual Studio Code 添加自定义snippet(代码段),附详细配置
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息