You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Provide a generic, cross editor snippet type that can be used to express complex text insertion operations. At a minimum, these snippets should include placeholder values and indicate where the cursor should end up.
For example, snippets could fill in function call arguments with placeholders. If the user completes ad to add here:
functionadd(x,y){returnx+y;}ad|
The inserted snippet would be:
functionadd(x,y){returnx+y;}add(«x»,«y»)
Where the user can tab from the placeholder values «x» to «y».
Use Cases
Method completions (currently implemented manually by VSCode under useCodeSnippetsOnMethodSuggest)
Why is this needed
Snippets speed up coding and provide additional hints while writing code. VS Code already tries to convert TS's existing response types to snippets but fails to handle many edge cases. In order to improve reliability and support more advanced used cases such as microsoft/vscode#48562, I believe that TS needs to start generating these snippets directly
Another possible use case that we discussed: refactorings such as extract constant. We currently implement this by triggering a rename after the extraction but a snippet could be a better fit. Example:
constthree=1+2;
Run extract constant on 2. This could generate a placeholder that is duplicated twice in the file:
const«newLocal»=2constthree=1+«newLocal»;
Typing would update both locations
(Note that VS Code's api would not support this currently since the placeholders are at unconnected locations in the file)
TS added support for snippet completions in VS Code with #43149 using snippet text syntax supported by VS Code and LSP. I don’t think any other protocol changes are needed, but correct me if I’m missing something Matt Bierner (@mjbvz). I think the task now is just to implement one or two of the most compelling use cases for snippets. Planning to start with #38891 and investigate #40700.
Activity
mjbvz commentedon Jun 25, 2018
I imagine that the snippet type could similar to
SymbolDisplayPart. Just a quick sketch:For completions, the snippet could be new optional additional field on
CompletionEntry. That way we don't break clients that do not support snippets.mjbvz commentedon Jun 28, 2018
Another possible use case that we discussed: refactorings such as extract constant. We currently implement this by triggering a rename after the extraction but a snippet could be a better fit. Example:
Run extract constant on
2. This could generate a placeholder that is duplicated twice in the file:Typing would update both locations
(Note that VS Code's api would not support this currently since the placeholders are at unconnected locations in the file)
mhegazy commentedon Jun 29, 2018
but is not that what rename does today?
mjbvz commentedon Jul 19, 2018
Another possible use case: generate complete method signature when overriding a method in a subclass microsoft/vscode#46038
bpasero commentedon May 9, 2020
Inspiration from Eclipse. Notice how the one tab stop for the method name is even linked!
andrewbranch commentedon Aug 26, 2021
TS added support for snippet completions in VS Code with #43149 using snippet text syntax supported by VS Code and LSP. I don’t think any other protocol changes are needed, but correct me if I’m missing something Matt Bierner (@mjbvz). I think the task now is just to implement one or two of the most compelling use cases for snippets. Planning to start with #38891 and investigate #40700.
mjbvz commentedon Sep 1, 2021
Yes I'll can close this since these already snuck in :)