Repository navigation
TSServer: snippet completions #25207
Description
Activity
I imagine that the snippet type could similar to
SymbolDisplayPart. Just a quick sketch:interface Snippet { parts: SnippetPart[]; } type SnippetPart = SnippetText | SnippetPlaceholder; interface SnippetText { kind: 'text' text: string } interface SnippetPlaceholder { kind: 'placeholder' text: string }
For completions, the snippet could be new optional additional field on
CompletionEntry. That way we don't break clients that do not support snippets.- addedSuggestionAn idea for TypeScriptAn idea for TypeScriptVS Code TrackedThere is a VS Code equivalent to this issueThere is a VS Code equivalent to this issue
on Jun 25, 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:
const three = 1 + 2;
Run extract constant on
2. This could generate a placeholder that is duplicated twice in the file:const «newLocal» = 2 const three = 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)
Typing would update both locations
but is not that what rename does today?
Another possible use case: generate complete method signature when overriding a method in a subclass microsoft/vscode#46038
- addedIn DiscussionNot yet reached consensusNot yet reached consensusDomain: LS: TSServerIssues related to the TSServerIssues related to the TSServer
on Nov 6, 2018 andrewbranch commented
on Aug 26, 2021 MemberMore actionsTS 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.
Yes I'll can close this since these already snuck in :)
Reacted by Andrew Branch

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
adtoaddhere:The inserted snippet would be:
Where the user can tab from the placeholder values
«x»to«y».Use Cases
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