VSCode学习笔记一:添加代码模板

一目了然

  • [1 简述](#1 简述)
  • [2 设置模板](#2 设置模板)
  • [3 Global Snippets file示例](#3 Global Snippets file示例)

1 简述

问:为什么要设置代码模板?

答:编程语言是有个性的,不同语言的演讲风格是不一样的。

旁白:我不懂?!

问:为什么要设置代码模板?

答:同种语言的演讲内容是不一样的,但是演讲稿的框架可以是一样的。

旁白:我懂了?!

问:为什么要设置代码模板?

答:好吧,我承认,每次复制粘贴的操作也是相当费键盘的,我需要快捷的方式在不同格式的语言文件中填充不同的框架内容。

旁白:VSCode说它可以。

2 设置模板

点击VSCode界面左下角的齿轮图标,在弹出界面中点击"User Snippets"选项,如下图所示:

此时会在界面顶部弹出选项,供用户选择需要为哪种语言添加模板,如下图所示:

选择你要添加的模板语言,然后就可以在模板文件中添加自定义的文件模板了,需要注意的是模板文件的格式为"json"格式。

也可以创建一个"Global Snippets file"模板文件,在这个文件中可以为各种语言添加文件模板。

3 Global Snippets file示例

下面的代码展示的是一个"Global Snippets file"示例:

json 复制代码
{
	// Place your global 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"
	// }
    "C function file template": {
        "prefix": "cfile",
        "body": [
            "/**",
            " * CONFIDENTIAL and PROPRIETARY",
            " *   Copyright (c) XXX Automotive System Co.,LTD. All rights reserved.",
            " *",
            " * FILE INFORMATION",
            " *   FILE NAME: ${TM_FILENAME}",
            " *   AUTHOR(S): XXX",
            " *",
            " * BRIEF INFORMATION",
            " *   function file.",
            " */",
            "",
            "/*---------------------------------------------------------------------------",
            " *         Header Files",
            " *---------------------------------------------------------------------------*/",
            "",
            "/*---------------------------------------------------------------------------",
            " *         Local Defines",
            " *---------------------------------------------------------------------------*/",
            "",
            "/*---------------------------------------------------------------------------",
            " *         Local Variables",
            " *---------------------------------------------------------------------------*/",
            "",
            "/*---------------------------------------------------------------------------",
            " *         Global Variables",
            " *---------------------------------------------------------------------------*/",
            "",
            "/*---------------------------------------------------------------------------",
            " *         Local Functions",
            " *---------------------------------------------------------------------------*/",
            "/**",
            " * Abstract:",
            " *   Function.",
            " *",
            " * Parameter:",
            " *   argc: Parameter.",
            " *",
            " * Return:",
            " *   None.",
            " */",
            "",
            "/*---------------------------------------------------------------------------",
            " *         Global Functions",
            " *---------------------------------------------------------------------------*/",
            "/**",
            " * Abstract:",
            " *   Function.",
            " *",
            " * Parameter:",
            " *   argc: Parameter.",
            " *",
            " * Return:",
            " *   None.",
            " */",
            "",
            ""
        ],
        "description": "C function file template."
    },
    "C header file template": {
        "prefix": "hfile",
        "body": [
            "/**",
            " * CONFIDENTIAL and PROPRIETARY",
            " *   Copyright (c) XXX Automotive System Co.,LTD. All rights reserved.",
            " *",
            " * FILE INFORMATION",
            " *   FILE NAME: ${TM_FILENAME}",
            " *   AUTHOR(S): XXX",
            " *",
            " * BRIEF INFORMATION",
            " *   header file.",
            " */",
            "",
            "#ifndef _${TM_FILENAME_BASE/(.*)/${1:/upcase}/}_H_INCLUDED_",
            "#define _${TM_FILENAME_BASE/(.*)/${1:/upcase}/}_H_INCLUDED_",
            "",
            "/*---------------------------------------------------------------------------",
            " *         Header Files",
            " *---------------------------------------------------------------------------*/",
            "",
            "/*---------------------------------------------------------------------------",
            " *         Defines",
            " *---------------------------------------------------------------------------*/",
            "",
            "/*---------------------------------------------------------------------------",
            " *         Types",
            " *---------------------------------------------------------------------------*/",
            "",
            "/*---------------------------------------------------------------------------",
            " *         Exported Functions",
            " *---------------------------------------------------------------------------*/",
            "",
            "",
            "#endif  /* End #ifndef _${TM_FILENAME_BASE/(.*)/${1:/upcase}/}_H_INCLUDED_ */",
            ""
        ],
        "description": "C header file template."
    },
    "GTest function file template": {
        "prefix": "gtest",
        "body": [
            "/**",
            " * CONFIDENTIAL and PROPRIETARY",
            " *   Copyright (c) XXX Automotive System Co.,LTD. All rights reserved.",
            " *",
            " * FILE INFORMATION",
            " *   FILE NAME: ${TM_FILENAME}",
            " *   AUTHOR(S): XXX",
            " *",
            " * BRIEF INFORMATION",
            " *   unit test file.",
            " */",
            "",
            "/*---------------------------------------------------------------------------",
            " *         Header Files",
            " *---------------------------------------------------------------------------*/",
            "#include <stdio.h>"
            "#include <stdlib.h>"
            "#include <gtest/gtest.h>"
            "/* Include C-format header file. */"
            "extern \"C\" {"
            "    #include \"\""
            "}",
            "",
            "/*---------------------------------------------------------------------------",
            " *         Defines",
            " *---------------------------------------------------------------------------*/",
            "",
            "/*---------------------------------------------------------------------------",
            " *         Variables",
            " *---------------------------------------------------------------------------*/",
            "",
            "/*---------------------------------------------------------------------------",
            " *         Functions",
            " *---------------------------------------------------------------------------*/",
            "/**",
            " * Abstract:",
            " *   Function.",
            " *",
            " * Parameter:",
            " *   argc: Parameter.",
            " *",
            " * Return:",
            " *   None.",
            " */",
            ""
        ],
        "description": "GTest function file template."
    },
    "Python file template": {
        "prefix": "pyfile",
        "body": [
            "# CONFIDENTIAL and PROPRIETARY",
            "#   Copyright (c) XXX Automotive System Co.,LTD. All rights reserved.",
            "#",
            "# FILE INFORMATION",
            "#   FILE NAME: ${TM_FILENAME}",
            "#   AUTHOR(S): XXX",
            "#",
            "# BRIEF INFORMATION",
            "#   file information.",
            "",
            "# Import modules or packages.",
            "",
            ""
        ],
        "description": "Python file template."
    },
}

在上述代码中供定义了四种文件模板:

  • cfile: C语言源码文件模板
  • hfile: C语言头文件模板
  • gtest: GTest源码文件模板
  • pyfile: Python文件模板

当为VSCode添加了上述"Global Snippets file"内容后,在空白文件中就可以通过输入提示词(cfile,hfile,gtest,pyfile)然后点击回车按键填充文件了。

相关推荐
深科文库7 分钟前
构建 MCP 服务器:第 4 部分 — 创建工具
python·chatgpt·prompt·aigc·agi·ai-native
witton12 分钟前
美化显示LLDB调试的数据结构
数据结构·python·lldb·美化·debugger·mupdf·pretty printer
nenchoumi31191 小时前
AirSim/Cosys-AirSim 游戏开发(一)XBox 手柄 Windows + python 连接与读取
windows·python·xbox
GoodStudyAndDayDayUp1 小时前
初入 python Django 框架总结
数据库·python·django
星辰大海的精灵1 小时前
基于Dify+MCP实现通过微信发送天气信息给好友
人工智能·后端·python
精灵vector1 小时前
Agent短期记忆的几种持久化存储方式
人工智能·python
北京_宏哥2 小时前
🔥Python零基础从入门到精通详细教程4-数据类型的转换- 上篇
前端·python·面试
乾巫宇宙国监察特使2 小时前
Python的设计模式
python·测试
Hockor2 小时前
写给前端的 Python 教程四(列表/元组)
前端·后端·python
这里有鱼汤2 小时前
熟练掌握MACD这8种形态,让你少走三年弯路(附Python量化代码)| 建议收藏
后端·python