Reputation: 382
Is there a way to have vscode insert different snippets for the same key combination depending on the current code context? In Coldfusion you can write your code in "tag" or "script" context.
For example, this is the script syntax:
<cfscript>
a = b + 1;
writeDump(a);
</cfscript>
and same thing in the tag syntax:
<cfset a = b + 1>
<cfdump var="#a#">
I want to be able to set up a keybinding that would insert a "writeDump(...)" when I'm coding in the cfscript context, and a "<cfdump var="...">
" when I'm coding in the tag context. Is this possible?
Upvotes: 0
Views: 174
Reputation: 182311
There are context or when
clauses in snippets and keybindings but not the kind that you need. [You could write an extension that sets a custom context that could then be used in a snippet or keybinding but that is a more complicated approach.]
You could create a keybinding that inserts a choice snippet. Keybinding:
{
"key": "alt+w", // whatever keybinding you want
"command": "editor.action.insertSnippet",
"args": {
// "snippet": "${1|writeDump(),<cfdump var=\"##\">|}" // simpler version
// uses the clipboard for your variable and 2 choice variables
"snippet": "${1|writeDump(,<cfdump var=\"#|}${CLIPBOARD}${2|);,#\">|}" // uses the clipboard for your variable
},
// "when": "editorLangId == coldfusion" // whatever the coldfusion language ID is?
}
${1|writeDump(,<cfdump var=\"#|}
on the first tabstop you will get a coice of writeDump(
or <cfdump var="#
.
Then the clipboard contents, your a
variable would be inserted.
Then tab
again and you will be given another choice to complete either );
or #\">
.
Upvotes: 1
Reputation: 2186
I use Lintalist (free; Windows; portable) for all my snippets. It's not VSCode-specific or "context aware", but it works with any program and allows me to quickly search snippets and paste either primary or alternate content. Using this app has enabled me to easily acclimate myself to any IDE because my snippets are independent of the editor and the experience remains consistent.
Upvotes: 1