按文件名或扩展名过滤片段,例如 *.spec.ts 和 *.spec.js?
Filter a snippet by filename or extension, like *.spec.ts and *.spec.js?
我想添加几个片段以在创建 javascript/typescript 单元测试时使用,但我找不到任何方法将片段的全局范围设置为 *.spec.ts
或 *.spec.js
这可能吗? In the documentation they say that the scope is bases on the language identifier 但我刚刚看到一种方法可以为那里的每种语言添加另一个扩展。
您可以对片段执行此操作。在你的 keybindings.json:
{
"key": "shift+alt+2",
"command": "editor.action.insertSnippet",
"when": "resourceFilename =~ /\.spec\.[tj]s$/",
// with the snippet text directly in the keybinding
"args": {
"snippet": "console.log()[=10=]"
}
},
或此键绑定:
{
"key": "shift+alt+2",
"command": "editor.action.insertSnippet",
"when": "resourceFilename =~ /\.spec\.[tj]s$/",
"args": {
"name": "unit tests"
}
}
在代码片段文件中使用此代码片段:
"unit tests": {
// "prefix": "", // not used here
"body": [
"console.log()[=12=]",
],
限制代码段范围的关键是这个 when
子句:
"when": "resourceFilename =~ /\.spec\.[tj]s$/",
作为正则表达式,它会查找以 .spec.ts
或 .spec.js
结尾的文件名(请注意,您需要在句点之前进行两次转义)。因此,使用 resourceFileName
并构建一个查看其末尾的正则表达式。
现在您选择的键绑定仅在 *.spec.ts
或 *.spec.js
文件中有效。
见a when clause acting as a regular expression, in keybindings documentation:
key-value when clause operator
There is a key-value pair operator for when
clauses. The expression
key =~ value
treats the right hand side as a regular expression to
match against the left hand side. For example, to contribute context
menu items for all Docker files, one could use:
"when": "resourceFilename =~ /docker/"
由于这个问题,我找到了这个:resourceExtname with two dots not working
我想添加几个片段以在创建 javascript/typescript 单元测试时使用,但我找不到任何方法将片段的全局范围设置为 *.spec.ts
或 *.spec.js
这可能吗? In the documentation they say that the scope is bases on the language identifier 但我刚刚看到一种方法可以为那里的每种语言添加另一个扩展。
您可以对片段执行此操作。在你的 keybindings.json:
{
"key": "shift+alt+2",
"command": "editor.action.insertSnippet",
"when": "resourceFilename =~ /\.spec\.[tj]s$/",
// with the snippet text directly in the keybinding
"args": {
"snippet": "console.log()[=10=]"
}
},
或此键绑定:
{
"key": "shift+alt+2",
"command": "editor.action.insertSnippet",
"when": "resourceFilename =~ /\.spec\.[tj]s$/",
"args": {
"name": "unit tests"
}
}
在代码片段文件中使用此代码片段:
"unit tests": {
// "prefix": "", // not used here
"body": [
"console.log()[=12=]",
],
限制代码段范围的关键是这个 when
子句:
"when": "resourceFilename =~ /\.spec\.[tj]s$/",
作为正则表达式,它会查找以 .spec.ts
或 .spec.js
结尾的文件名(请注意,您需要在句点之前进行两次转义)。因此,使用 resourceFileName
并构建一个查看其末尾的正则表达式。
现在您选择的键绑定仅在 *.spec.ts
或 *.spec.js
文件中有效。
见a when clause acting as a regular expression, in keybindings documentation:
key-value when clause operator
There is a key-value pair operator for
when
clauses. The expressionkey =~ value
treats the right hand side as a regular expression to match against the left hand side. For example, to contribute context menu items for all Docker files, one could use:
"when": "resourceFilename =~ /docker/"
由于这个问题,我找到了这个:resourceExtname with two dots not working