skip to Main Content

I have implemented a LSP for a VSCode extension that provides a series of diagnostics for the opened document(s).

The execution of the function that evaluates the document occurs in the onDidChangeContent event (server side):

documents.onDidChangeContent(async event => {
    await validateTextDocument(event.document)
})

Now the issue comes when the document is quite long and you start typing or making changes rapidly. The validateTextDocument() can take a few seconds for these long documents and since documents.onDidChangeContent() will fire on each keystroke, the computer starts overloading with too many requests.

Is there any way to prevent the client from making any more requests until the server has responded? or even cancel the onDidChangeContent altogether?

Thanks in advance

2

Answers


  1. Chosen as BEST ANSWER

    I'll take this approach as the final solution. I haven't found anything on the LSP client's side that will allow me to limit the number of events fired.

    This is the best solution I've come up with on the server side. Just store all the received events and only execute last one every interval of 1 second discarding the rest. It works more or less OK

    let eventBuffer: TextDocumentChangeEvent<TextDocument>[] = []
    documents.onDidChangeContent(event => eventBuffer.push(event))
    
    setInterval(async () => {
        if (eventBuffer.length > 0) {
            const event = eventBuffer.pop()
            eventBuffer = []
            await validateTextDocument(event!.document)
        }
    }, 1000);
    

    • use the Document link provider vscode.languages.registerDocumentLinkProvider
    • return an empty list of links

    it will only call you when you haven’t typed for a while

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search