microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
162.04k stars 28.5k forks source link

Execute functions during snippet expansion #10561

Open DustinCampbell opened 8 years ago

DustinCampbell commented 8 years ago

There are several snippets that exist in Visual Studio today that I would like to introduce to the C# extension for VS Code, but require generating dynamic text are executing code to perform other actions during snippet expansion. For example:

From what I can tell, snippets only support text with placeholders today. Is it possible to generate text dynamically or execute commands with arguments during snippet expansion?

jrieken commented 8 years ago

@DustinCampbell There are snippets and completions. The former are static and it would be hard to add dynamic command arguments to it.

However, a completion provider can return a completion of type snippet and we plan to enhance things in August such that completion can have a onDidAccet-command and additional text edits (#6874). The main difference there is the prefix, like cw, ctor etc. You can still react to those, but maybe its wise to also the same amount of luxury for more regular completions

DustinCampbell commented 8 years ago

@jrieken, if a completion provider returns completions of type snippet does those still populate the new Insert Snippet command?

jrieken commented 8 years ago

No and I unsure if we should. The change is easy but we cannot make the same change to the tabCompletion feature - since we need to know sync' if a prefix matches. That would make both features different. This needs some thinking... Maybe some sort of special snippet provider that must statically announce its snippets (at least prefix and title) and is then pinged to make the actual insert.

DustinCampbell commented 8 years ago

Yeah, I'm not sure if it should either, but I'd also hate to have a disjoint snippet experience where, say, the ctor snippet works from the completion list, but not via tab completion or insert snippet.

jrieken commented 8 years ago

@DustinCampbell How this item stack against #6874? Same, more/less important, exclusive?

DustinCampbell commented 8 years ago

6874 is much more important priority-wise.

jrieken commented 8 years ago

check - #6874 will land soon

jrieken commented 8 years ago

We could do something like textmate interpolated shell code but for vscode commands. For instance a snippet would look like this

constructor `commandThatReturnsCtorName`(`commandThatReturnsCtorArgs`) {
  $1
}

When inserting such a snippet we identify the command references and replace them with whatever their execution returned (executeCommand). As arguments we could pass the document, position, and snippet

DustinCampbell commented 8 years ago

That'd be way cool

jrieken commented 7 years ago

@DustinCampbell Would you mind me closing this as a duplicate of https://github.com/Microsoft/vscode/issues/3210? The idea is to advocate the use of CompletionItemProvider returning SnippetStrings instead of smart but static snippets.

The problem with the latter is that they are still static ;-) For instance, you won't be able to say don't propose the ctor snippet inside method-bodies etc, etc. Implementing a completion item provider that returns snippet-completion would allow you all those cool things

DustinCampbell commented 7 years ago

Is the plan to integrate those into the Insert Snippet command? Or, would the list that appears in that command be different from those that can appear in completion?

jrieken commented 7 years ago

Yeah, we could do that. Harder than that is to integrate this with the tab-completion feature...

DustinCampbell commented 7 years ago

Will that happen too? I just want to be sure that users have a consistent experience. Wouldn't it be weird if some snippets work with tab completion, but others only work when completion is triggered?

jens1o commented 7 years ago

Would love to see this in the June release.

FiniteReality commented 7 years ago

I'd love to be able to use this. As a use case, I've got C++ include guards which would be better off if they used the file name (through $TM_FILENAME) but can't since I'd have to replace dots in the filename with underscores.

atian25 commented 6 years ago

use CompletionItem to provide a snippet is hard to set details, any way to render the snippet with some locals programify, so we could set it to details.

DollarAkshay commented 6 years ago

Any updates on inserting current timestamp/datetime for code snippet ?

jens1o commented 6 years ago

@DollarAkshay It's worth it to read the release notes ;) https://code.visualstudio.com/updates/v1_20#_more-snippet-variables

nith271670 commented 5 years ago

Hi,

I want to create a snippet that will give me <tag uuid="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" class="className"> on type of "tag" and the uuid has to generated on expansion of the snippet.

I did get the insert guid snippet but it inserts the snippet through command palette but I want the snippet to be inserted on type of "tag".

Is there a way to do this?

Gruntfuggly commented 5 years ago

Being able to run vscode commands in the backticks would also be really useful.

nith271670 commented 5 years ago

Hi,

I want to create a snippet that will give me <tag uuid="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" class="className"> on type of "tag" and the uuid has to generated on expansion of the snippet.

I did get the insert guid snippet but it inserts the snippet through command palette but I want the snippet to be inserted on type of "tag".

Is there a way to do this?

tecosaur commented 5 years ago

@jrieken What's the chance of texmate code-execution-in-backticks being added any time soon?

I'd like to perform several regex substitutions in a snippet, and I can't see any other way.

WellspringCS commented 5 years ago

So... if I type ctor and get public ClassName(Parameters)... That's still the expected behavior, if not perhaps the desired behavior?

From the two threads I've skimmed, seems the requested feature has not been implemented yet? I thought it was my VSCode editor performing sub-optimally, but now I think I'm just wishing for a feature others requested three years ago.

krispetkov commented 3 years ago

Is there any update on this task?

globalhuman commented 1 year ago

Reading through the numerous linked issues it's not clear to me what status this issue is in regards to the backlog? It has a reasonable amount of support and keen users.

How does this get transitioned to a milestone goal?

Fred-Vatin commented 1 year ago

I would add that having a way to define our own variables available in all snippets in a code-snippets file would help.

starball5 commented 5 months ago

Related on Stack Overflow: How to define and use custom variable for VS Code snippets

Also a use-case in vscode snippet variables: how to get content from output of command line program

maximedupre commented 1 month ago

Is this the feature that would allow this use case: https://github.com/microsoft/vscode-discussions/discussions/1274?

Can VSC extensions still not extend the list of snippet variables?