M
M
Maxim Morev2020-03-12 04:21:07
Snippets
Maxim Morev, 2020-03-12 04:21:07

Is it possible to insert conditional snippets in VSCode like in Sublime Text?

Good time!
Is it possible in VSCode to insert a snippet from a keybinding only if there is no specific character before the insertion point?

I want typing "{" in CSS/SCSS files to automatically generate the correct structure.

That is, I write:
.block{

I get:

.block {
  | <- курсор
}


There are no problems with this:
5e698c5921ab2675705103.png
...but alas, in SCSS, the parenthesis is used not only as an opening construct, but also for variable interpolation #{$variable}.

Actually the subject - so that the snippet does not work in the case when there is a "#" symbol before the cursor.

In the same Sublime Text works fine:
5e698d1180836867870929.png

I didn’t find something like this for VSCode. Can someone tell me how to?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Morev, 2020-03-12
@SeaInside

In the course of further research, it was determined that this can only be implemented by writing your own extension and calling not editor.action.insertSnippet, but a method of your extension, which will check the previous character and insert the snippet if everything is OK.
In my opinion, writing such small extensions is bad manners, I solved my problem by adding a new snippet, which, when pressed on "ctrl+#", will insert #{$var}, and "shift+3" (#) remains behind the usual octothorpe.
Keybindings, if anyone needs:

{
  "key": "shift+[",
  "when": "editorTextFocus && resourceExtname == .scss || resourceExtname == .css",
  "command": "editor.action.insertSnippet",
  "args": {
    "snippet": " {\n\t${0}\n}"
  }
},
{
  "key": "ctrl+3",
  "when": "editorTextFocus && resourceExtname == .scss || resourceExtname == .css",
  "command": "editor.action.insertSnippet",
  "args": {
     "snippet": "#{\\$${1:var}}"
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question