question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Execute functions during snippet expansion

See original GitHub issue
  • 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?

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:44
  • Comments:26 (15 by maintainers)

github_iconTop GitHub Comments

9reactions
jriekencommented, 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

5reactions
WellspringCScommented, Jul 7, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Execute function in vscode snippet - Stack Overflow
Is it possible to run a function during expansion of snippet? javascript.json - vscode snippet file: function add() { return 2 ...
Read more >
Code Snippet Functions - Visual Studio (Windows)
There are three functions available to use with C# code snippets. Functions are specified in the Function element of the code snippet.
Read more >
Snippets in Visual Studio Code
Snippets in Visual Studio Code. Code snippets are templates that make it easier to enter repeating code patterns, such as loops or conditional-statements....
Read more >
Expanding snippets
You can use YASnippet to expand snippets in different ways: When yas-minor-mode is active: Type the snippet's trigger key then calling yas-expand (bound...
Read more >
Run function after specific snippet - Emacs Stack Exchange
If the snippet worked, then there will be text in the region. We expect the snippet to either work or fail gracefully, not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found