Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support snippet variables #4956

Closed
martinring opened this issue Apr 5, 2016 · 21 comments
Closed

Support snippet variables #4956

martinring opened this issue Apr 5, 2016 · 21 comments
Assignees
Labels
editor-contrib Editor collection of extras feature-request Request for new features or functionality snippets
Milestone

Comments

@martinring
Copy link

martinring commented Apr 5, 2016

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.


  • VSCode Version: 0.10.11
  • OS Version: Fedora 23

Steps to Reproduce:

  1. Create user snippet using variable TM_SELECTED_TEXT
  2. Use snippet
    -> Does not insert the selected text in place of $TM_SELECTED_TEXT
@alexdima alexdima self-assigned this Apr 7, 2016
@martinring
Copy link
Author

Any chance this is going to be considered? :)

@alexdima
Copy link
Member

@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.

@alexdima alexdima added the feature-request Request for new features or functionality label Apr 27, 2016
@martinring
Copy link
Author

@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.

@jreljac
Copy link

jreljac commented Sep 30, 2016

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)

@alexdima alexdima added the editor-contrib Editor collection of extras label Oct 27, 2016
@alexdima alexdima added this to the Backlog milestone Oct 27, 2016
@alexdima alexdima removed their assignment Oct 27, 2016
@jrieken
Copy link
Member

jrieken commented Nov 3, 2016

desired variables: selected text, filename, date

@jrieken jrieken changed the title snippets: TM_SELECTED_TEXT does not work Support snippet variables Nov 3, 2016
@jrieken
Copy link
Member

jrieken commented Nov 3, 2016

@jrieken jrieken self-assigned this Nov 21, 2016
@jrieken jrieken modified the milestones: November 2016, Backlog Nov 21, 2016
jrieken added a commit that referenced this issue Nov 21, 2016
jrieken added a commit that referenced this issue Nov 21, 2016
jrieken added a commit that referenced this issue Nov 21, 2016
@jrieken
Copy link
Member

jrieken commented Nov 21, 2016

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.

@egamma egamma mentioned this issue Dec 20, 2016
56 tasks
@jbeaudoin11
Copy link

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.

@misantronic
Copy link

+1 for getting the filename without extension.

@ianlyons
Copy link

ianlyons commented Feb 7, 2017

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...

@jbeaudoin11
Copy link

jbeaudoin11 commented Feb 7, 2017

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.

@jrieken
Copy link
Member

jrieken commented Feb 8, 2017

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.

@Ashedragon
Copy link

Any update on being able to retrieve the file name without the extension?

@ScallyGames
Copy link

Is Date as listed in #4956 (comment) still a thing to come?

@jrieken
Copy link
Member

jrieken commented Mar 24, 2017

@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?

@ScallyGames
Copy link

@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.

@borislemke
Copy link

borislemke commented May 18, 2017

@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
 */

@jrieken
Copy link
Member

jrieken commented May 19, 2017

@borislemke Is that a webstrom specific syntax, or where is that defined?

@borislemke
Copy link

@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$

@bobmagicii
Copy link

bobmagicii commented Jun 14, 2017

+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)$

@penproductions
Copy link

I will add a vote for this as well. Having to add notes with dates by hand is wasting time.

@vscodebot vscodebot bot locked and limited conversation to collaborators Nov 18, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
editor-contrib Editor collection of extras feature-request Request for new features or functionality snippets
Projects
None yet
Development

No branches or pull requests