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

Execute functions during snippet expansion #10561

Open
DustinCampbell opened this Issue Aug 15, 2016 · 18 comments

Comments

Projects
None yet
6 participants
@DustinCampbell
Member

DustinCampbell commented Aug 15, 2016

  • VSCode Version: 1.4
  • OS Version: any

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:

  • ctor -- generate a constructor declaration, automatically filling in the name of the constructor with the enclosing type name.
  • testclass -- generate a test class and adds references to necessary test assemblies.
  • cw -- generate System.Console.WriteLine() but simplify the type name with the current using directives. So, System. is removed if using System; is in scope.

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

This comment has been minimized.

Member

jrieken commented Aug 16, 2016

@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

This comment has been minimized.

Member

DustinCampbell commented Aug 16, 2016

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

@jrieken

This comment has been minimized.

Member

jrieken commented Aug 16, 2016

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

This comment has been minimized.

Member

DustinCampbell commented Aug 16, 2016

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

This comment has been minimized.

Member

jrieken commented Aug 18, 2016

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

@DustinCampbell

This comment has been minimized.

Member

DustinCampbell commented Aug 18, 2016

#6874 is much more important priority-wise.

@jrieken

This comment has been minimized.

Member

jrieken commented Aug 18, 2016

check - #6874 will land soon

@jrieken jrieken added the snippets label Aug 23, 2016

@jrieken

This comment has been minimized.

Member

jrieken commented Aug 23, 2016

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

This comment has been minimized.

Member

DustinCampbell commented Aug 23, 2016

That'd be way cool

@jrieken

This comment has been minimized.

Member

jrieken commented Nov 21, 2016

@DustinCampbell Would you mind me closing this as a duplicate of #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

This comment has been minimized.

Member

DustinCampbell commented Nov 21, 2016

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

This comment has been minimized.

Member

jrieken commented Nov 21, 2016

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

@DustinCampbell

This comment has been minimized.

Member

DustinCampbell commented Nov 21, 2016

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

This comment has been minimized.

Contributor

jens1o commented Jun 2, 2017

Would love to see this in the June release.

@FiniteReality

This comment has been minimized.

FiniteReality commented Jul 2, 2017

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

This comment has been minimized.

atian25 commented Oct 27, 2017

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

This comment has been minimized.

DollarAkshay commented Feb 19, 2018

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

@jens1o

This comment has been minimized.

Contributor

jens1o commented Feb 20, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment