Visual Studio Code allows creating snippets, separated for various language syntaxes. But is it possible to specify not only language, but also extension of a particular file for created snippet?
For example:
I want to create snippet specifically for c#
.csproj
file. This file has xml
syntax, and I could create snippet as an xml
snippet, but I don't want to see this snippet in other xml
files except .csproj
.
For snippets you use the scope
parameter:
"FrontMatter": {
"scope": "csharp", // put your language identifier here
"prefix": "frontmatter",
"body": [
"...",
]
},
If that scope isn't specific enough, use a keybinding instead which allows for more specificity.
scope
cannot take content like editorTextFocus && !editorReadonly && resourceExtname =~ /\\.csproj/
.
----------- for keybindings you can use the below -----------
"when": "editorTextFocus && !editorReadonly && resourceExtname =~ /\\.csproj/ && editorLangId == csharp"
The =~
operator allows the use of regular expressions on the right-hand side, so you could do somethinh like
resourceExtname =~ /\\.(html|css|scss)
to restrict to those three extensions. It seems odd to me that you do have to specifically add the .
prior to the extension itself, but it seems you do.
A keybinding which refers to a snippet by name
- the snippet itself is defined in one of your snippet files follows:
{
"key": "alt+l",
"command": "editor.action.insertSnippet",
"args": {
"name": "My groovy comment style"
},
"when": "editorTextFocus && !editorReadonly && resourceExtname =~ /\\.csproj/ && editorLangId == csharp"
},
or with the actual snippet body right in the keybinding:
{
"key": "alt+m",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus && !editorReadonly && resourceExtname =~ /\\.csproj/ && editorLangId == csharp",
"args": {
"snippet": "${TM_SELECTED_TEXT/([a-z])([A-Z]+(?=[A-Z]))|([A-Z][a-z])/$1 ${2:/downcase}${3:/downcase}/g}"
}
},