1

Dynamic VS Code Snippets

Do VS Code snippets support dynamic input values, and if not, is there a way to add some sort of functionality that would allow me to define a snippet whose output is contingent on input received by the user?



For Example:

  • Lets say I added the following to my source code:
        "1/"

  • What I would like is for it to be automatically converted to:
        /frac(1)



Is this possible in VS Code?


  • use the extension `Hypersnips` – rioV8 Aug 26 '22 at 16:32
  • You should give more examples so we can understand better. It would be easy to parse the `ref.name.null` and produce the output you want (you would have to trigger a macro though), Does the `ref` part change? If you want it to automatically produce the output then you might be able to do it with `HyperSnips`. – Mark Aug 26 '22 at 20:11
  • See for example https://stackoverflow.com/a/62562886/836330 – Mark Aug 28 '22 at 19:48

1 Answers1

0

After reading your question a few times over, I found that the way that I initially interpreted what you were asking for was not what you were actually asking.

You are not asking...

"How can one go about authoring a snippet whose output is contingent on text-input received by the user..?"

...rather, you are asking...

"How can one go about adding dynamic text-input support to snippets, such that the end user can create snippets whose output is contingent on the input received from the user..?"

In not so many words...

One asks how to do something, and the other asks, how to add support for doing something (in this context, "that something" is dynamic text-input in VS Code snippets).


The answer would be: "The VS Code API"

You need to use the "VS Code API" to add support for dynamic text-input to snippets because "VS Code's dynamic input variables mechanism" does not extend support to the snippets configuration file. By default dynamic input can only be defined by the user in a VS Code Tasks context whose environment can be worked in using a "launch.json" file located @ "./.vscode/launch.json`.

"VS Code extensions built from the VS Code API" are however, quite proficient at getting (as well as setting & moving) dynamic input from the user then setting it in some arbitrary place (i.e. snippets, keyboard shortcuts, etc...).

I included a bunch of links that cover everything I wrote about. The links included are very helpful, and well written.

There are already extensions (as mentioned in the comments) that can provide support for dynamic snippets input, if you don't want to write one, but sometimes writing an extension can be the best solution, its really on a case by case basis.



Also, how you get the input from the user can be achieved by a myriad of different methods, for example:

The A.P.I. can fetch a dynamic value from...

  • The Quick-input Menu (for example: pressing F1 key)

  • From Inside the Editor (You can even specify specific lines & line-col values)

  • Entity names. (i.e. function names, variable names, parameter names, etc...)

  • You can even develop, design, and render your own menu to get input from via context menu_

-You can let the user define how they want to input the text by contributing your own custom settings

The links in the list just above this text should really help if you decide to add the functionality yourself.

JΛYDΞV
  • 8,532
  • 3
  • 51
  • 77
  • 1
    I want to add more on it, that You can generate snippets from here https://snippet-generator.app/ and add the code into snippets files for your language. – Vijay Hardaha Aug 26 '22 at 19:07
  • If you mean edit the answer, then go ahead. – JΛYDΞV Aug 26 '22 at 19:20
  • 2
    Im assuming you mean add more to the answer. People forget that stack overflow is a community effort. If you have something important to include, that is accurate information, then by all means add it. – JΛYDΞV Aug 26 '22 at 19:21