Search code examples
typescriptvscode-extensionslanguage-server-protocol

Intellisense in custom vscode Extension not working


I have copied the setup from the microsoft lsp-sample.The handling for the Completion is in the server.

connection.onCompletion(
    (_textDocumentPosition: TextDocumentPositionParams): CompletionItem[] | undefined => {
        const document = documents.get(_textDocumentPosition.textDocument.uri);
        const linePrefix = document?.getText({
            start: { line: _textDocumentPosition.position.line, character: 0 },
            end: _textDocumentPosition.position,
        });

        if(!linePrefix?.startsWith('Record.')){
            const yamlComplete: CompletionItem[] = getCompletionFromYaml();
            console.log(yamlComplete);
            return yamlComplete;
        }
        
        const output: CompletionItem[] = [];
        let i = 1;
        for(const currKey of tableData){
            const keys = Object.keys(currKey);
            output.push({
                label: keys[0],
                kind: CompletionItemKind.Field,
                data: i,
                documentation: 'test'
            });
            i++;
        }
        console.log(output);
        return output;
        }
); 

The returns are correct and in the correct format:

This is from the yamlComplete
[
  { label: 'record_get', kind: 2, data: 1 },
  { label: 'record_find', kind: 2, data: 2 }
]

It works if the the Prefix isnt Record.. If that is true i expect vscode to give me the follwoing Items as autocomplete:

This is the output
[
  { label: 'test_id', kind: 5, data: 1, documentation: 'test' },
  { label: 'event_id', kind: 5, data: 2, documentation: 'test' }
]

However I just get no suggestions at all. I believe that is because vscode is looking in the line and searching for a suggestion that has Record. in it.

So the expected behavior is when i type Record. i should get test_id and event_id as suggestions for the Items of that Object.

If i rewrite the Label of the onCompletion() to: label: Record.${keys[0]}`` it kinda works but that doenst seem like a clean and correct solution to me.

Thank you in advance :)


Solution

  • Solution to the Problem:

    output.push({
                label: keys[0],
                kind: CompletionItemKind.Field,
                filterText: `Record.${keys[0]}`,
                insertText: `Record.${keys[0]}`
            });
    

    That worked for us!