Visual Code Python 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 编写的较为详细的说明可以参考微软官方的介绍:https://code.visualstudio.com/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}):",
		]
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值