Debounce LSP processing
This commit is contained in:
@@ -21,17 +21,51 @@ import { TextDocument } from "vscode-languageserver-textdocument";
|
|||||||
const connection = createConnection(ProposedFeatures.all);
|
const connection = createConnection(ProposedFeatures.all);
|
||||||
const documents = new TextDocuments(TextDocument);
|
const documents = new TextDocuments(TextDocument);
|
||||||
|
|
||||||
|
// the last is the most important to the user, but we run FIFO
|
||||||
|
// to ensure dependencies are seen in causal order
|
||||||
|
let changes: TextDocument[] = []
|
||||||
|
let running: NodeJS.Timeout | undefined
|
||||||
|
function addChange(doc: TextDocument) {
|
||||||
|
console.log('enqueue', doc.uri)
|
||||||
|
// drop stale pending changes
|
||||||
|
let before = changes.length
|
||||||
|
changes = changes.filter(ch => ch.uri != doc.uri)
|
||||||
|
console.log('DROP', changes.length - before);
|
||||||
|
changes.push(doc)
|
||||||
|
if (!running) running = setTimeout(runChange, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
function runChange() {
|
||||||
|
let doc = changes.shift()
|
||||||
|
if (!doc) {
|
||||||
|
running = undefined
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const uri = doc.uri
|
||||||
|
const diagnostics = LSP_checkFile(doc.uri)
|
||||||
|
connection.sendDiagnostics({uri,diagnostics})
|
||||||
|
console.log('SENT', doc.uri);
|
||||||
|
running = undefined
|
||||||
|
// give more a chance to file in.
|
||||||
|
running = setTimeout(runChange,1)
|
||||||
|
}
|
||||||
|
|
||||||
documents.onDidChangeContent(async (change) => {
|
documents.onDidChangeContent(async (change) => {
|
||||||
console.log('DIDCHANGE', change.document.uri)
|
console.log('DIDCHANGE', change.document.uri)
|
||||||
const uri = change.document.uri;
|
const uri = change.document.uri;
|
||||||
const text = change.document.getText();
|
const text = change.document.getText();
|
||||||
LSP_updateFile(uri, text);
|
LSP_updateFile(uri, text);
|
||||||
const diagnostics = LSP_checkFile(uri);
|
// we defer the check to let all of the changes file in.
|
||||||
console.log(`Got ${JSON.stringify(diagnostics, null, ' ')}`)
|
addChange(change.document);
|
||||||
connection.sendDiagnostics({ uri, diagnostics })
|
// const diagnostics = LSP_checkFile(uri);
|
||||||
|
// console.log(`Got ${JSON.stringify(diagnostics, null, ' ')}`)
|
||||||
|
// connection.sendDiagnostics({ uri, diagnostics })
|
||||||
});
|
});
|
||||||
|
|
||||||
connection.onHover((params): Hover | null => {
|
connection.onHover((params): Hover | null => {
|
||||||
|
// wait until quiesced (REVIEW after query-based)
|
||||||
|
if (running) return null
|
||||||
|
|
||||||
const uri = params.textDocument.uri;
|
const uri = params.textDocument.uri;
|
||||||
const pos = params.position;
|
const pos = params.position;
|
||||||
console.log('HOVER', uri, pos)
|
console.log('HOVER', uri, pos)
|
||||||
|
|||||||
Reference in New Issue
Block a user