Skip to content

Commit d541596

Browse files
committed
Diagnosis proper diffing
1 parent e92a4ea commit d541596

File tree

1 file changed

+39
-20
lines changed

1 file changed

+39
-20
lines changed

server/src/server.ts

+39-20
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { DidOpenTextDocumentNotification, DidChangeTextDocumentNotification, Did
1212
import * as tmp from 'tmp';
1313
import { Range } from 'vscode-languageserver-textdocument';
1414
import { uriToFsPath, URI } from 'vscode-uri';
15+
// import * as chokidar from 'chokidar'
1516

1617
// See https://microsoft.github.io/language-server-protocol/specification Abstract Message
1718
// version is fixed to 2.0
@@ -29,8 +30,8 @@ let shutdownRequestAlreadyReceived = false;
2930
let diagnosticTimer: null | NodeJS.Timeout = null;
3031

3132
// congrats. A simple UI problem is now a distributed system problem
32-
let stupidFileContentCache: { [key: string]: string } = {
33-
}
33+
let stupidFileContentCache: { [key: string]: string } = {}
34+
let previousDiagnosticFiles: Set<string> = new Set()
3435

3536
let findDirOfFileNearFile = (fileToFind: p.DocumentUri, source: p.DocumentUri): null | p.DocumentUri => {
3637
let dir = path.dirname(source)
@@ -231,7 +232,8 @@ FAILED: src/test.cmj src/test.cmi
231232
return ret
232233
}
233234

234-
let startWatchingCompilerLog = (root: p.DocumentUri, process: NodeJS.Process) => {
235+
let startWatchingCompilerLog = (process: NodeJS.Process) => {
236+
// chokidar.watch()
235237
// TOOD: setTimeout instead
236238
let id = setInterval(() => {
237239
let openFiles = Object.keys(stupidFileContentCache);
@@ -246,19 +248,20 @@ let startWatchingCompilerLog = (root: p.DocumentUri, process: NodeJS.Process) =>
246248

247249
let files: { [key: string]: t.Diagnostic[] } = {}
248250

249-
let res = Array.from(compilerLogDirs)
250-
.forEach(compilerLogDir => {
251-
let compilerLogPath = path.join(compilerLogDir, compilerLogPartialPath);
252-
let content = fs.readFileSync(compilerLogPath, { encoding: 'utf-8' });
253-
let filesAndErrors = parseCompilerLogOutput(content, ":")
254-
Object.keys(filesAndErrors).forEach(file => {
255-
// assumption: there's no existing files[file] entry
256-
// this is true; see the lines above. A file can only belong to one .compiler.log root
257-
files[file] = filesAndErrors[file]
258-
})
259-
});
260-
261-
Object.keys(files).forEach(file => {
251+
compilerLogDirs.forEach(compilerLogDir => {
252+
let compilerLogPath = path.join(compilerLogDir, compilerLogPartialPath);
253+
let content = fs.readFileSync(compilerLogPath, { encoding: 'utf-8' });
254+
let filesAndErrors = parseCompilerLogOutput(content, ":")
255+
Object.keys(filesAndErrors).forEach(file => {
256+
// assumption: there's no existing files[file] entry
257+
// this is true; see the lines above. A file can only belong to one .compiler.log root
258+
files[file] = filesAndErrors[file]
259+
})
260+
});
261+
262+
// Send new diagnostic, wipe old ones
263+
let filePaths = Object.keys(files)
264+
filePaths.forEach(file => {
262265
let params: p.PublishDiagnosticsParams = {
263266
uri: file,
264267
// there's a new optional version param from https://github.com/microsoft/language-server-protocol/issues/201
@@ -271,7 +274,24 @@ let startWatchingCompilerLog = (root: p.DocumentUri, process: NodeJS.Process) =>
271274
params: params,
272275
};
273276
process.send!(notification);
277+
278+
// this file's taken care of already now. Remove from old diagnostic files
279+
previousDiagnosticFiles.delete(file)
280+
})
281+
// wipe the errors from the files that are no longer erroring
282+
previousDiagnosticFiles.forEach(remainingPreviousFile => {
283+
let params: p.PublishDiagnosticsParams = {
284+
uri: remainingPreviousFile,
285+
diagnostics: [],
286+
}
287+
let notification: m.NotificationMessage = {
288+
jsonrpc: jsonrpcVersion,
289+
method: 'textDocument/publishDiagnostics',
290+
params: params,
291+
};
292+
process.send!(notification);
274293
})
294+
previousDiagnosticFiles = new Set(filePaths)
275295
}, 1000);
276296

277297
return id;
@@ -332,12 +352,13 @@ process.on('message', (a: (m.RequestMessage | m.NotificationMessage)) => {
332352
process.send!(response);
333353
} else if (aa.method === 'initialize') {
334354
let param: p.InitializeParams = aa.params
355+
// TODO: sigh what's deprecated now?
335356
let root = param.rootUri
336357
if (root == null) {
337358
// TODO: handle single file
338359
console.log("not handling single file")
339360
} else {
340-
diagnosticTimer = startWatchingCompilerLog(root, process)
361+
diagnosticTimer = startWatchingCompilerLog(process)
341362
}
342363
// send the list of things we support
343364
let result: p.InitializeResult = {
@@ -439,12 +460,10 @@ process.on('message', (a: (m.RequestMessage | m.NotificationMessage)) => {
439460
};
440461
process.send!(response);
441462

442-
// TODO: make sure the diagnosis diffing takes this into account
463+
// TODO: make sure the diagnostic diffing takes this into account
443464
if (!compilerLogPresentAndNotEmpty(filePath)) {
444465
let params2: p.PublishDiagnosticsParams = {
445466
uri: params.textDocument.uri,
446-
// there's a new optional version param from https://github.com/microsoft/language-server-protocol/issues/201
447-
// not using it for now, sigh
448467
diagnostics: [],
449468
}
450469
let notification: m.NotificationMessage = {

0 commit comments

Comments
 (0)