Home > Blockchain >  VSCode Extension: How can you tell if an Intellisense suggestion has been clicked?
VSCode Extension: How can you tell if an Intellisense suggestion has been clicked?

Time:08-21

While working on suggestions inside of my VSCode extension, I've been unable to tell if the user has clicked my suggestion.

For example down below, this works to show log, warn or error if the user types "console." however if the click "warn", for example, is there a way to fire a function or know that they clicked it?

export function activate(context: vscode.ExtensionContext) { 
const provider2 = vscode.languages.registerCompletionItemProvider(
    'plaintext',
    {
        provideCompletionItems(document: vscode.TextDocument, position: vscode.Position) {

            // get all text until the `position` and check if it reads `console.`
            // and if so then complete if `log`, `warn`, and `error`
            const linePrefix = document.lineAt(position).text.substr(0, position.character);
            if (!linePrefix.endsWith('console.')) {
                return undefined;
            }

            return [
                new vscode.CompletionItem('log', vscode.CompletionItemKind.Method),
                new vscode.CompletionItem('warn', vscode.CompletionItemKind.Method),
                new vscode.CompletionItem('error', vscode.CompletionItemKind.Method),
            ];
        }
    },
    '.' // triggered whenever a '.' is being typed
);

context.subscriptions.push(provider2);

CodePudding user response:

Lets say your completionItem i s called item in the function where you are actually constructing the completionItem. You can add a command property to any or all items that will be fired upon selection of that item from the suggestion list.

let newCommand = {};

// call command 'selectDigitInCompletion' defined in extension.js
// rename it to what you want
newCommand.command = "find-and-transform.selectDigitInCompletion";

// some descriptive title
newCommand.title = "Select the digit 'n' in completionItem";

// whatever arguments you put here will be available when the command is called, key is the completion text in my use
newCommand.arguments = [key, replaceRange];  

item.command = newCommand;

Then return your array of completionItems. Register the "find-and-transform.selectDigitInCompletion" somewhere. This command selects part of the completion key n that was just added and needs to be replaced by the user with some digit.

let selectDigitInCompletion = vscode.commands.registerCommand('find-and-transform.selectDigitInCompletion', async (completionText, completionRange) => {

// do what you want when that completion item was selected, or check `completionText` to see which was selected.
// you can modify what is inserted here

  // args = [completionText, Range] from makeCompletionItem function
  // if completionText startsWith '\\U$n' or '${n'
  let keyLength;
  if (completionText?.startsWith("${n")) keyLength = 3;
  else if (completionText?.search(/^\\\\[UuLl]\$n/m) === 0) keyLength = 5;
  else if (completionText?.search(/^\$\{getTextLines:n/m) === 0) keyLength = 16;
  else return;

  if (completionRange?.start) {
    const digitStart = new vscode.Position(completionRange.start.line, completionRange.start.character   keyLength - 1);
    const digitEnd = new vscode.Position(completionRange.start.line, completionRange.start.character   keyLength);
    vscode.window.activeTextEditor.selection = new vscode.Selection(digitStart, digitEnd);
  }
});

context.subscriptions.push(selectDigitInCompletion);
  • Related