VScode中自定義用戶代碼片段(C user snippet)自動生成C語言頭文件排除重複包含 原

環境

  • 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(代碼段),附詳細配置

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章