typescriptvisual-studio-codelanguage-server-protocol

Language Server Protocol(LSP) for VS Code, document.getText() is not working in onCompletion()


I am trying to create a new custom language, with the code completion.

My purpose is to detect user defined functions and variables in the documents every time the code changes and put it in the list of completion. The code is based on "lsp-sample" code, powered by Microsoft.

But, I am getting an error in the "document.gettext()" function with the error message "'document' is possibly 'undefined'".

Code with error message

Unfortunately, I am not used for TypeScript language, So, why this error occurred in the "onCompletion()" function. Because, it works well in the other function "validateTextDocument()".

Please help me and thanks a lot.

............................


Solution

  • Return if document is undefined:

    const document = documents.get(_textDocumentPosition.textDocument.uri);
    if (document === undefined) {
        return [];
    }
    const pos = _textDocumentPosition.position;