Get vscode registerCompletionItemProvider to work in a json file with a `word.` trigger

Viewed 1730

Update

Thanks to json: use default word pattern issue being resolved, vscode's json language server no longer includes the quotes around a word as part of the word, like "someWord" - now that word would be simply someWord.

In my case, as @rioV8 said, I was not explicitly setting the completionItem.range (because I was just going to use the default range). When you do not set your own range vscode uses the range of the word at the cursor in a completion - which used to include the " and that screwed up my completions.

The starting quote " is part of what VSCode considers the current "word". Consequently, the completion items you return don't match the current filter string " and are not displayed.

To fix that, all I needed to do was to explicity set the range like

item.range = new vscode.Range(position, position);

NOW after the linked fix, since the word no longer includes the " I do not (I tested it) need to explcitly set the range and the default range works fine.



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


1 Answers

Based on the answer by Gamma11 about what is a word in JSON, the whole string is considered a word including the " chars.

It works if you adjust the range the completion item should replace, and not look for the current word at the position.

  context.subscriptions.push(vscode.languages.registerCompletionItemProvider (
    { language: 'json', scheme: 'file' },
    // 'json',
    {
      // 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.substring(0, position.character);
        if (!linePrefix.endsWith('"launches.')) {
          return undefined;
        }
        let myitem = (text) => {
          let item = new vscode.CompletionItem(text, vscode.CompletionItemKind.Text);
          item.range = new vscode.Range(position, position);
          return item;
        }
        return [
          myitem('log'),
          myitem('warn'),
          myitem('error'),
        ];
      }
    },
    '.' // trigger
  ));

Edit:

What also works but does not look nice is

return [
  new vscode.CompletionItem('"launches.log"', vscode.CompletionItemKind.Text),
  new vscode.CompletionItem('"launches.warn"', vscode.CompletionItemKind.Text),
  new vscode.CompletionItem('"launches.error"', vscode.CompletionItemKind.Text),
];

Edit:

Just to supply a completion on any . typed I just removed the test (endsWith) of what was in front of the ..

To see if the completion provider is called I place a LogPoint breakpoint on the return with the CompletionItems.

The documentation of the CompletionItem is very terse.

From the doc of CompletionItem:

It is sufficient to create a completion item from just a label. In that case the completion item will replace the word until the cursor with the given label or insertText. Otherwise the given edit is used.

Although they talk about an edit in the main text, the textEdit doc tells you it is deprecated and you need to use insertText and range. But the additionalTextEdits are not deprecated (??)

The range property is not very clear how an inserting and replacing range are used and what effect you can achieve by setting it a certain way.

When omitted, the range of the current word is used as replace-range and as insert-range the start of the current word to the current position is used.

And then part of the problem is that " is part of a word for JSON files. And as Gamma11 has pointed out if you, for some odd reason, add these "'s to the label it works in some cases. Setting the insertText with the same content does not work, probably because the default range is chosen incorrectly.

If you set the range yourself you bypass the strange default behavior.

Because we want to insert new stuff at the position of the cursor just set range to an empty range at the cursor position.

  context.subscriptions.push(vscode.languages.registerCompletionItemProvider (
    // { language: 'json', scheme: 'file' },
    'json',
    {
      // eslint-disable-next-line no-unused-vars
      provideCompletionItems(document, position, token, context) {
        let myitem = (text) => {
          let item = new vscode.CompletionItem(text, vscode.CompletionItemKind.Text);
          item.range = new vscode.Range(position, position);
          return item;
        }
        return [
          myitem('howdy1'),
          myitem('howdy2'),
          myitem('howdy3'),
        ];
      }
    },
    '.' // trigger
  ));
Related