文章目录
一、什么是Visual code Snippet(代码片段)?二、如何使用Snippet?3.在哪里可以使用Snippet?4.如何创建自定义的Snippet?1.打开相应语言的Json文件2.编写相应的Snippet片段3.示例解析4. Snippet 选择语句 模板一、什么是Visual code Snippet(代码片段)?
Vsiual code Snippet 简单来讲就是将日常经常用到或重复使用的代码片段整理出一个模板以方便后续使用
二、如何使用Snippet?
在代码行敲入Snippet提示符即可显示相应的代码段或相应的字段,选择相应的代码段即可插入,在相应的光标处写入要添加的表达式,完成后再次按下TAB按键即可跳到下个占位符处
3.在哪里可以使用Snippet?
首先可以到Visual code 里找相关语言的Snippet 插件,其次对于JavaScript, TypeScript, Markdown, and PHP Visuual code里有内置的Snippet可用;
最后如果以上都没有满足你的要求,可以创建自定义的Snippet 片段
4.如何创建自定义的Snippet?
1.打开相应语言的Json文件
注意: Snippet 代码段 可以适用于一种特定的语言也可以设置适用于多个或全部语言,在使用前请确认好使用范围否则可能导致语法错误
同样的的也可以对相应的项目创建Snippet
2.编写相应的Snippet片段
有关Snippet 编写的较为详细的说明可以参考微软官方的介绍:/docs/editor/userdefinedsnippets)
Snippet语法:
另外Snippet 编辑器里给出的简单示例如下:
// Place your snippets for python 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:
// “Print to console”: {
// “prefix”: “log”,
// “body”: [
// “console.log(‘$1’);”,
// “$2”
// ],
// “description”: “Log output to console”
// }
3.示例解析
下面介绍Snippet 主要用到最多的功能及点位符,其他说明可以参考官方介绍
Snippet主要由三部分组成;
名称prefix 提示符body 主体description 描述
"IF...ELSE": {//设置Snippet名称 IF...ELSE,名称后由中括号包围"prefix": "if...else",//设置提示符if...else,由双引号包围"body": [//设置主体,注意body格式,由方括号包围"if ${1:expression1} :",//为if 主体插入占位符,Tab停止位置会按照序号跳转"\t${2:expression2}",//前面的/t为水平制表符,注意每行最后会添加一个逗号"else:","\t${3:expression3}"],//每个结构结束后都要跟个逗号"description": "IF...ELSE...Snippet"//描述语句,可以省略},//每个Snippet片段结束后其后面也要跟个逗号!"IF...ELIF...ELSE": {"prefix": "if...elif","body": ["if ${1:expression1} :","\t${2:expression2}","elif ${3:expression3}:","\t${4:expression4}","else:","\t${5:empression5}"],"description": "if...elif...else Snippet"}
4. Snippet 选择语句
下面介绍下Snippet 的选择语句,单独讲是因为它比较特殊,且应用范围有局限
来看下面两个For循环Snippet示例:
"for_CycleinList":{"prefix": "for","body": ["for ${1:Var} in ${2:Variable}:","\t${3:statement}"]},"for_CycleinRange":{"prefix": "for","body": ["for ${1:Var} in range(${2:LowLim},${3:HighLim})}:","\t${4:statement}"]},
以上两人示例分别代表For 应用的两种情况,第一种为用于迭代数组,第二种用于计数的情况,那么可不可以利用Snippet 占位符可嵌套及选择语句将两个进行合并呢?
参考Snippet语法,这种方式是不可行的,因为Snippet 选择语句里只是写了Text 表示选择语句选项只可以是文本不可以再嵌套占位符
模板
以下是Snippet针对python 的For 语句及if语句的模板,如果需要可以再扩展:
"IF...ELSE": {"prefix": "if...else","body": ["if ${1:expression1} :","\t${2:expression2}","else:","\t${3:expression3}"],"description": "IF...ELSE...Snippet"},"IF...ELIF...ELSE": {"prefix": "if...elif","body": ["if ${1:expression1} :","\t${2:expression2}","elif ${3:expression3}:","\t${4:expression4}","else:","\t${5:empression5}"],"description": "if...elif...else Snippet"},"print%":{"prefix": "Print_%","body": ["print('${1:statement}%${2:formatCharacter}'%(${name}))",],"description": "Desc:Print %"},"print.format":{"prefix": "Print_format","body": ["print('${1:statement1}${2:statement2}${3:statement3}'.format(${4:statement4},${5:statement5},${6:statement6}))",],"description": "Desc:Print_format"},"for_CycleinList":{"prefix": "for","body": ["for ${1:Var} in ${2:Variable}:","\t${3:statement}"]},"for_CycleinRange":{"prefix": "for","body": ["for ${1:Var} in range(${2:LowLim},${3:HighLim})}:","\t${4:statement}"]},"while_Cycle":{"prefix": "while","body": ["#The code is build on $CURRENT_DAY_NAME","while ${1:expression}:", "\t${2:statement}"]},"define function":{"prefix": "def","body": ["def ${1:function name}(${2:para1},${3:para2}):",]}