Search code examples
javascripttypescriptvisual-studio-codevscode-extensions

How to Control Visual Studio Code Extension Intellisense?


i want to make my extension

I want to suggest "CompletionList" to users. users can run "editor.action.triggerSuggest"

The process of my extensions as is follows

  1. users write some texts
  2. if he press "completion command",
  3. vscode extension provides Completion
  4. and executeCommand("editor.action.triggerSuggest")

But i encounter some issues. When a specific character is already entered, no matter how much I create CompletionItems, they won't appear unless that specific character is included in the suggestions. For instance, if the cursor is positioned right after the letter 'r' in the word 'for,' the suggestion list won't appear, but if the cursor is one space after 'r,' the suggestion list appears as expected

When the cursor is right behind the text

img

When there is a space

img

if there is a way that solve this issue?


Edit

When I performed a similar task with 'NT,' the Code Completion items appeared with the character 'NT' in CodeCompletion. Therefore, I suspect that in vscode Intellisense, based on the cursor position, it may be looking for completions specifically related to 'NT' since the cursor is right after 'T' in 'NT.' In conclusion, when vscode provides triggerSuggest internally, it seems to be influenced by the preceding characters, offering only triggerSuggest items related to them. Is there a way to address this issue?

When the cursor is right behind the text

img

When there is a space

img


Edit

  const completionTest = vscode.commands.registerCommand("sb.subhotkey", () => {
    // Remove existing completions
    const disposable = vscode.Disposable.from(CompletionProvider);
    disposable.dispose();

    // Sort candidates based on their values in descending order
    candidates.sort((a, b) => b.value - a.value);
    
    // Register new completion provider
    CompletionProvider = vscode.languages.registerCompletionItemProvider(
        "smallbasic",
        {
            provideCompletionItems(): vscode.ProviderResult<
                vscode.CompletionItem[] | vscode.CompletionList
            > {
                const CompletionItems: vscode.CompletionItem[] = [];
                for (const { key, value } of candidates) {
                    const completion = new vscode.CompletionItem(key);
                    const completionDocs = new vscode.MarkdownString(value.toString());
                    completion.documentation = completionDocs;
                    CompletionItems.push(completion);
                }
                return CompletionItems;
            },
        },
        "."
    );
    // Triggest Suggest command
    vscode.commands.executeCommand("editor.action.triggerSuggest");
});

Candidates are structured as follows:

0:{key: '[T, ID]', value: 1444}

1:{key: '[T, (, NT, Expr, T, )]', value: 982}

2:{key: '[T, STR]', value: 697}

3:{key: '[T, NUM]', value: 227}

4:{key: '[T, ID, NT, Idxs]', value: 152}

Solution

  • It is the fuzzy search VSC performs on all shown lists, like the Command Palette.

    You type part of the text you search, characters don't need to next to each other, Capitals of Camel case class name, function name.

    If you want member suggestions your completion provider is triggered by . character and then fuzzy search uses all word characters before the cursor, if there are no characters until . you see the whole list, it gets filtered when you type.

    You need to set the filterText property of the completion item.

    • are there word chars before the cursor: set filterText to word chars + key
    • are there NO word chars before the cursor: set filterText to key