镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

TSServer: snippet completions #25207

Description

@mjbvz

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:

function add(x, y) { return x + y; }

ad|

The inserted snippet would be:

function add(x, y) { return x + y; }

add(«x», «y»)

Where the user can tab from the placeholder values «x» to «y».

Use Cases

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

Activity

  1. mjbvz commented on Jun 25, 2018

    @mjbvz
    Author

    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.

  2. mjbvz commented on Jun 28, 2018

    @mjbvz
    Author

    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)

  3. mhegazy commented on Jun 29, 2018

    @mhegazy
    Contributor

    Typing would update both locations

    but is not that what rename does today?

  4. mjbvz commented on Jul 19, 2018

    @mjbvz
    Author

    Another possible use case: generate complete method signature when overriding a method in a subclass microsoft/vscode#46038

  5. bpasero commented on May 9, 2020

    @bpasero

    Inspiration from Eclipse. Notice how the one tab stop for the method name is even linked!

    recording

  6. andrewbranch commented on Aug 26, 2021

    @andrewbranch
    Member

    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.

  7. mjbvz commented on Sep 1, 2021

    @mjbvz
    Author

    Yes I'll can close this since these already snuck in :)

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

Metadata

Metadata

Labels

Domain: LS: TSServerIssues related to the TSServerIn DiscussionNot yet reached consensusSuggestionAn idea for TypeScriptVS Code TrackedThere is a VS Code equivalent to this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions