'VSCode custom language extention - Show CompletionItems for variable

I'm working on a custom language extension and I'm having issues with how to show functions for a variable.

I'm importing my language in the form of json, so i've created a typescript-file that i import into my extensions.ts:

export interface CustomIntellisense {
  text: string;
  help: string;
}

const data = [{
  "text": "Void.String",
  "help": "<h1>String String()</h1><p>Default constructor.<code>String something;</code></p>"
},
{
  "text": "String.toInteger",
  "help": "<h1>Integer toInteger()</h1><p>Converts a String to its numeric representation."
},
{
  "text": "Void.Integer",
  "help": "<h1>Integer Integer()</h1><p>Default constructor.</p>"
}];

export let json: CustomIntellisense[] = data;

My idea here is that the elements containing "Void" in text gets created as a variable, while the other element gets added as a method.

const provider1 = vscode.languages.registerCompletionItemProvider({ language: 'myLanguage', scheme: 'file' }, {
        provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken, context: vscode.CompletionContext) {

            let items: vscode.CompletionItem[] = [];
            let re = /\&quot;/gi;

            json.forEach(element => {
                const item = new vscode.CompletionItem(element.text.split('.')[1]);
                item.insertText = new vscode.SnippetString(element.help);
                const markdownDocumentation = new vscode.MarkdownString();
                markdownDocumentation.supportHtml = true;
                markdownDocumentation.appendMarkdown(element.help);
                item.documentation = markdownDocumentation;

                if (element.text.includes('Void.')) { //If text includes Void this should be a variable
                    item.kind = vscode.CompletionItemKind.Variable;
                }
                else {
                    item.kind = vscode.CompletionItemKind.Method;
                }

                items.push(item);
            });
            return items;
        }
    });

The items gets added to the view, but I can't figure out how to 'filter' what is shown. The official example on how to achieve this can be found here: https://github.com/microsoft/vscode-extension-samples/blob/main/completions-sample/src/extension.ts

But this only explains how to filter based on the text/name, and i cant filter this specifically for each variableName i use.. If i somehow could detect what kind of Variable i'm working on I could possibly create a function that fetches if its a String/Int, then parse through my file and add methods in my 2nd CompletionItemProvider. But I havent found any good way of deciding the type of variable..

What i want is this:

If i click ctrl+space i want toInteger() to be the only thing that shows up, but instead it lists up everything all the time: enter image description here

Anyone have a clue how to achieve this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source