'Get vscode registerCompletionItemProvider to work in a json file with a `word.` trigger
I am using this code to try to register a CompletionProvider in my extension. It is essentially the code from the sample completionProvider sample https://github.com/microsoft/vscode-extension-samples/blob/master/completions-sample/src/extension.ts.
I want it triggered by a . as in "launches." in my extension command in keybindings.json ultimately but it is doing nothing in any json file. Nothing happens, no error.
function activate(context) {
loadLaunchSettings(context);
activeContext = context;
const configCompletionProvider = vscode.languages.registerCompletionItemProvider (
{ language: 'json', scheme: 'file' }, // tried scheme: 'untitled' too
{
// eslint-disable-next-line no-unused-vars
provideCompletionItems(document, position, token, context) {
// get all text until the `position` and check if it reads `"launches."`
const linePrefix = document.lineAt(position).text.substr(0, position.character);
if (!linePrefix.endsWith('\"launches.\"')) { // tried without the escapes too
return undefined;
}
return [
new vscode.CompletionItem('log', vscode.CompletionItemKind.Text),
new vscode.CompletionItem('warn', vscode.CompletionItemKind.Text),
new vscode.CompletionItem('error', vscode.CompletionItemKind.Text),
];
}
},
'.' // trigger
);
context.subscriptions.push(configCompletionProvider);
}
In this code:
{
"key": "alt+f",
"command": "launches." <= provider completion options here
},
I couldn't find anything helpful and thought I followed the sample closely but no completion suggestions either on typing "launches." or using Ctrl+Space to trigger intellisense.
I do have this setting:
"editor.quickSuggestions": {
"comments": true,
"other": true,
"strings": true // <===
},
And I tried various alternatives presented here to a similar problem: Custom Extension for JSON Completion Does Not Work in Double Quotes
Still baffled - works in a javascript file but not a json file. Is there something special I have to do to get a . recognised as a trigger character in a json file (other than listing it in the vscode.languages.registerCompletionItemProvider as below)?
I have stripped it down to:
function activate(context) {
loadLaunchSettings(context);
activeContext = context;
let docFilter = { scheme: 'file', language: 'json' };
const configCompletionProvider = vscode.languages.registerCompletionItemProvider (
// { language: 'json', pattern: './**/test.json' }, // does not work
// 'javascript', // works
// 'json', // does not work
docFilter, // does not work
{
// eslint-disable-next-line no-unused-vars
provideCompletionItems(document, position, token, context) {
return [
new vscode.CompletionItem('howdy1', vscode.CompletionItemKind.Text),
new vscode.CompletionItem('howdy2', vscode.CompletionItemKind.Text),
new vscode.CompletionItem('howdy3', vscode.CompletionItemKind.Text),
];
}
},
"." // trigger
);
context.subscriptions.push(configCompletionProvider);
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
