The documentation on snippets says:
The snippet syntax follows the TextMate snippet syntax with the exception of 'regular expression replacements', 'interpolated shell code' and 'transformations', which are not supported.
Which implicitly says that VSCode does support variables.
The TextMate documentation on snippet variables says:
You can insert the value of a variable by prefixing the name of the variable with $. All the normal dynamic variables are supported, the most useful probably being TM_SELECTED_TEXT.
Though it does not work, nor does vscode seem to provide an alternative.
Steps to Reproduce:
Any chance this is going to be considered? :)
@martinring I agree that our documentation does not explicitly mention that variables are not supported, but AFAIK they were never implemented. So this issue is a feature request.
@alexandrudima ok thank you for the clarification. surrounding snippets are quite a common feature of editors. visual studio supports them as does textmate for example. I might look into adding that.
I'll add in my vote. Being able to highlight text, run a snippet and have that highlighted text replace $TM_SELECTED_TEXT in the snippet is super useful. (Both Textmate and Sublime support this)
desired variables: selected text, filename, date
We now support SELECTION, TM_SELECTED_TEXT, TM_CURRENT_LINE, TM_CURRENT_WORD, TM_LINE_INDEX, TM_LINE_NUMBER, TM_FILENAME, TM_DIRECTORY, TM_FILEPATH as defined here: http://docs.sublimetext.info/en/latest/extensibility/snippets.html#environment-variables.
I would like to have a TM_FILENAME w/o the extension so i could, for exemple, create a snippet for auto naming a class base on the filename.
+1 for getting the filename without extension.
Probably worth noting that (at least on the Sublime docs page) they do support this:
You can also use environment variables with substitutions:
# In file MyModule.js:
Transformation: ${TM_FILENAME/(\w+)\.js/\1/g}
# Output:
Transformation: MyModule
Seems like it might be unnecessary to add it as a full-fledged feature...
Are we able to use regex ? If yes how ? Cause I'm not able to make it work..
With regex support yes it would be good enough. It would be nice if we could make custom variable like TM_FILENAME or simple shortcuts.
We are tracking the transformations request in #6920. It's a little more complex because of how the regex-engines in TextMate and VS Code/JS work. We are likely not shooting for compatibility in evaluating them, but just reuse the syntax.
Any update on being able to retrieve the file name without the extension?
Is Date as listed in https://github.com/Microsoft/vscode/issues/4956#issuecomment-258175578 still a thing to come?
@Aides359 How isdate spec'd? I couldn't figure that out. Is it full UTC date string, locale data string, year, month, day and also time?
@jrieken hm.. good point.
In lack of this feature I currently use vscode-insertdatestring (which is quite a hassle since I have to execute two commands per snippet) and they solve this issue by setting a property in the user settings with a sensible default.
Otherwise (not sure if that is possible syntax-wise) a solution might be to specify it using a parameter somewhat like ${date|'YYYY-MM-DD'} again with some sensible default.
@Aides359 Yes, we need this. I'm currently TRYING to fully replace my workflow from WebStorm to VS Code for all the good reasons but always end up coming back to WS for heavy duty coding sessions. I use Live Templates(a term for snippets in WS) a lot with WS.
As an example, creating todo notes by expanding the todo keyword:
"body": [
"/**",
" * TODO($1): $2",
" * @date - ${date|'YYYY-MM-DD'}",
" * @time - ${date|'HH:mm'}",
" */$0"
]
Should become:
/**
* TODO(firstCursor): secondCursor
* @date - 2017-05-19
* @time - 00:29
*/
@borislemke Is that a webstrom specific syntax, or where is that defined?
@jrieken that is the proposal I would like VS Code to implement. The official WS syntax for the same template is:
/**
* TODO($VAR1$): $VAR2$
* @date - $DATE$
* @time - $TIME$
*/$END$
+1 for date, unless the format is not configurable cause i'd just rage over anything not YYYY-MM-DD.
$DATE(format)$ could be interesting.
$DATE(YYYY-MM-DD)$
I will add a vote for this as well. Having to add notes with dates by hand is wasting time.
Most helpful comment
I would like to have a TM_FILENAME w/o the extension so i could, for exemple, create a snippet for auto naming a class base on the filename.