Home > OS >  How to prevent LSP onDidChangeContent event from firing too many times
How to prevent LSP onDidChangeContent event from firing too many times

Time:09-05

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

BEST SOLUTION SO FAR

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);

CodePudding user response:

  • 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

CodePudding user response:

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

  • Related