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] Completion trigger characters #21012

Closed
mjbvz opened this issue Jan 4, 2018 · 0 comments
Closed

[TSServer] Completion trigger characters #21012

mjbvz opened this issue Jan 4, 2018 · 0 comments
Labels
Committed The team has roadmapped this issue Fixed A PR has been merged for this issue Suggestion An idea for TypeScript VS Code Tracked There is a VS Code equivalent to this issue

Comments

@mjbvz
Copy link
Contributor

mjbvz commented Jan 4, 2018

TL;DR: Add the concept of a triggerCharacter for completions. Update the TS Server so that it uses the triggerCharacter to determine if completions should be returned at the requested location or not

Background

VS Code uses trigger characters to automatically show suggestions as a user types. Registering " as a trigger character for example means that suggestions are shown automatically whenever a user types ".

The VS Code TS extension uses these trigger character to determine if we should request completions from TS Server. For the trigger character " for example, the VS Code TS extension has logic so that it only requests completions when you start typing an import path:

import {} from "|

and not when you type out a string #like:

const z = "abc"|

This is because the TS Server still returns completion entires in the second case, and we want to ensure these do not show up unless a user explicitly requests them.

The TS Server currently has no knowledge of what triggered a completions request.

Problem

The VS Code TS extension's logic that determines when to request completions from the TSServer is very simple. It just runs some regular expressions on the current line to check if they resemble a valid auto-suggest context. This works well enough for limiting the " trigger to import paths, but there are other cases where this is too limiting.

Take:

const s: 'abc' | 'def' = "|

TS can provide helpful suggestions here but we do not show them automatically because triggering on every " would show suggestions in too many locations (like for the case "abc"| noted above).

Another example would be microsoft/vscode#40539 which tracks showing jsx tag suggestions automatically when you type <. Again, VS Code cannot just blindly register < as a trigger character, as this would cause suggestions to be shown whenever you type a less-than operator in an expression.

Proposal

Make the TS server trigger character aware. Use the trigger character information and TypeScript's more complete understanding of the current document to determine when to return completions and when not to.

When the trigger character is " for example, the TS server would return completions for import paths like:

import {} from "|

and for literal types like:

const s: 'abc' | 'def' = "|

but not for strings:

const z = "abc"|

A similar check could be done for the triggerCharacter <, which should only return completions when the < is used as a jsx tag opening.

To implement this, add a new optional triggerCharacter field to CompletionsRequestArgs

interface CompletionsRequestArgs extends FileLocationRequestArgs {
    [...]

    triggerCharacter?: string;
}

When a triggerCharacter is provided, use this to determine if completions should be returned or not. If the triggerCharacter is undefined, there would be no change from the existing behavior

@mjbvz mjbvz added the VS Code Tracked There is a VS Code equivalent to this issue label Jan 4, 2018
@mhegazy mhegazy assigned ghost Jan 30, 2018
@mhegazy mhegazy added this to the TypeScript 2.8 milestone Jan 30, 2018
@mhegazy mhegazy added Suggestion An idea for TypeScript Committed The team has roadmapped this issue labels Jan 30, 2018
@mhegazy mhegazy modified the milestones: TypeScript 2.8, TypeScript 2.9 Mar 9, 2018
@ghost ghost closed this as completed in #23491 Apr 20, 2018
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label May 3, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Committed The team has roadmapped this issue Fixed A PR has been merged for this issue 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

2 participants