Skip to content

Commit 37e7b71

Browse files
authored
Merge pull request #484 from rescript-lang/fix-problems-tab-cleared-accidentally
fix problem tab accidentally being cleared
2 parents 38d58f9 + eb76025 commit 37e7b71

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

server/src/server.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { assert } from "console";
2020
import { fileURLToPath } from "url";
2121
import { ChildProcess } from "child_process";
2222
import { WorkspaceEdit } from "vscode-languageserver";
23+
import { filesDiagnostics } from "./utils";
2324

2425
interface extensionConfiguration {
2526
askToStartBuild: boolean;
@@ -41,6 +42,8 @@ let projectsFiles: Map<
4142
{
4243
openFiles: Set<string>;
4344
filesWithDiagnostics: Set<string>;
45+
filesDiagnostics: filesDiagnostics;
46+
4447
bsbWatcherByEditor: null | ChildProcess;
4548

4649
// This keeps track of whether we've prompted the user to start a build
@@ -79,8 +82,22 @@ let openCompiledFileRequest = new v.RequestType<
7982
void
8083
>("rescript-vscode.open_compiled");
8184

85+
let getCurrentCompilerDiagnosticsForFile = (
86+
fileUri: string
87+
): p.Diagnostic[] => {
88+
let diagnostics: p.Diagnostic[] | null = null;
89+
90+
projectsFiles.forEach((projectFile, _projectRootPath) => {
91+
if (diagnostics == null && projectFile.filesDiagnostics[fileUri] != null) {
92+
diagnostics = projectFile.filesDiagnostics[fileUri].slice();
93+
}
94+
});
95+
96+
return diagnostics ?? [];
97+
};
8298
let sendUpdatedDiagnostics = () => {
83-
projectsFiles.forEach(({ filesWithDiagnostics }, projectRootPath) => {
99+
projectsFiles.forEach((projectFile, projectRootPath) => {
100+
let { filesWithDiagnostics } = projectFile;
84101
let content = fs.readFileSync(
85102
path.join(projectRootPath, c.compilerLogPartialPath),
86103
{ encoding: "utf-8" }
@@ -91,6 +108,7 @@ let sendUpdatedDiagnostics = () => {
91108
codeActions,
92109
} = utils.parseCompilerLogOutput(content);
93110

111+
projectFile.filesDiagnostics = filesAndErrors;
94112
codeActionsFromDiagnostics = codeActions;
95113

96114
// diff
@@ -188,6 +206,7 @@ let openedFile = (fileUri: string, fileContent: string) => {
188206
projectRootState = {
189207
openFiles: new Set(),
190208
filesWithDiagnostics: new Set(),
209+
filesDiagnostics: {},
191210
bsbWatcherByEditor: null,
192211
hasPromptedToStartBuild: /(\/|\\)node_modules(\/|\\)/.test(
193212
projectRootPath
@@ -608,17 +627,22 @@ let updateDiagnosticSyntax = (fileUri: string, fileContent: string) => {
608627
let tmpname = utils.createFileInTempDir(extension);
609628
fs.writeFileSync(tmpname, fileContent, { encoding: "utf-8" });
610629

611-
let items: p.Diagnostic[] | [] = utils.runAnalysisAfterSanityCheck(filePath, [
612-
"diagnosticSyntax",
613-
tmpname,
614-
]);
630+
// We need to account for any existing diagnostics from the compiler for this
631+
// file. If we don't we might accidentally clear the current file's compiler
632+
// diagnostics if there's no syntax diagostics to send. This is because
633+
// publishing an empty diagnostics array is equivalent to saying "clear all
634+
// errors".
635+
let compilerDiagnosticsForFile =
636+
getCurrentCompilerDiagnosticsForFile(fileUri);
637+
let syntaxDiagnosticsForFile: p.Diagnostic[] =
638+
utils.runAnalysisAfterSanityCheck(filePath, ["diagnosticSyntax", tmpname]);
615639

616640
let notification: p.NotificationMessage = {
617641
jsonrpc: c.jsonrpcVersion,
618642
method: "textDocument/publishDiagnostics",
619643
params: {
620644
uri: fileUri,
621-
diagnostics: items,
645+
diagnostics: [...syntaxDiagnosticsForFile, ...compilerDiagnosticsForFile],
622646
},
623647
};
624648

server/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ let parseFileAndRange = (fileAndRange: string) => {
474474
};
475475

476476
// main parsing logic
477-
type filesDiagnostics = {
477+
export type filesDiagnostics = {
478478
[key: string]: p.Diagnostic[];
479479
};
480480
type parsedCompilerLogResult = {

0 commit comments

Comments
 (0)