greenharbour
greenharbour

Reputation: 306

How to programmatically show a CompletionList in VS Code?

I want to be able to show a CompletionList in a specified editor/position to the user programmatically (not based on user typing a trigger character). Is this possible?

Upvotes: 6

Views: 1281

Answers (2)

N. Taylor Mullen
N. Taylor Mullen

Reputation: 18301

Original: As of today (12/12/2018), this is not possible.

Edit: Attempted to delete this post in favor of @Gama11's answer below but cannot delete accepted answers. I'm not 100% positive the editor.action.triggerSuggest is supported but it works.

@Gama11's response below: https://stackoverflow.com/a/53804882/1574622

Upvotes: 0

Gama11
Gama11

Reputation: 34138

Actually, this is possible by executing the "editor.action.triggerSuggest" command. This is the same command that gets executed when you press Ctrl+Space to manually invoke completion.

vscode.commands.executeCommand("editor.action.triggerSuggest");

If you want to control where the popup opens, simply change the active editor + selection beforehand:

var file = vscode.workspace.workspaceFolders[0].uri.fsPath + "/foo.txt";
vscode.workspace.openTextDocument(file).then(document => {
    vscode.window.showTextDocument(document).then(editor => {
        editor.selection = new vscode.Selection(10, 0, 10, 0);
        vscode.commands.executeCommand("editor.action.triggerSuggest");
    });
});

Upvotes: 6

Related Questions