Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TSServer: snippet completions #25207

Closed
mjbvz opened this issue Jun 25, 2018 · 7 comments
Closed

TSServer: snippet completions #25207

mjbvz opened this issue Jun 25, 2018 · 7 comments
Assignees
Labels
Domain: TSServer Issues related to the TSServer In Discussion Not yet reached consensus Suggestion An idea for TypeScript VS Code Tracked There is a VS Code equivalent to this issue

Comments

@mjbvz
Copy link
Contributor

mjbvz commented Jun 25, 2018

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

@mjbvz
Copy link
Contributor Author

mjbvz commented Jun 25, 2018

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.

@mhegazy mhegazy added Suggestion An idea for TypeScript VS Code Tracked There is a VS Code equivalent to this issue labels Jun 25, 2018
@mjbvz
Copy link
Contributor Author

mjbvz commented 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:

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)

@mhegazy
Copy link
Contributor

mhegazy commented Jun 29, 2018

Typing would update both locations

but is not that what rename does today?

@mjbvz
Copy link
Contributor Author

mjbvz commented Jul 19, 2018

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

@bpasero
Copy link
Member

bpasero commented May 9, 2020

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

recording

@andrewbranch
Copy link
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 @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
Copy link
Contributor Author

mjbvz commented Sep 1, 2021

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
Labels
Domain: TSServer Issues related to the TSServer In Discussion Not yet reached consensus Suggestion An idea for TypeScript VS Code Tracked There is a VS Code equivalent to this issue
Projects
None yet
Development

No branches or pull requests

6 participants