Skip to content

Commit 2265806

Browse files
committed
Use file content cache for detecting empty record
1 parent 58b2d2d commit 2265806

File tree

6 files changed

+50
-4
lines changed

6 files changed

+50
-4
lines changed

package-lock.json

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,5 +259,8 @@
259259
"esbuild": "^0.20.1",
260260
"semver": "^7.3.7",
261261
"typescript": "^4.7.3"
262+
},
263+
"dependencies": {
264+
"vscode-languageserver-textdocument": "^1.0.12"
262265
}
263266
}

server/src/codeActions.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// actions available in the extension, but they are derived via the analysis
33
// OCaml binary.
44
import * as p from "vscode-languageserver-protocol";
5+
import { TextDocument } from "vscode-languageserver-textdocument";
56
import * as utils from "./utils";
67
import { fileURLToPath } from "url";
78

@@ -16,6 +17,7 @@ interface findCodeActionsConfig {
1617
diagnosticMessage: string[];
1718
file: string;
1819
range: p.Range;
20+
fileContentCache: Map<string, string>;
1921
addFoundActionsHere: filesCodeActions;
2022
}
2123

@@ -146,6 +148,7 @@ export let findCodeActionsInDiagnosticsMessage = ({
146148
diagnosticMessage,
147149
file,
148150
range,
151+
fileContentCache,
149152
addFoundActionsHere: codeActions,
150153
}: findCodeActionsConfig) => {
151154
diagnosticMessage.forEach((line, index, array) => {
@@ -174,6 +177,7 @@ export let findCodeActionsInDiagnosticsMessage = ({
174177
index,
175178
line,
176179
range,
180+
fileContentCache,
177181
});
178182
} catch (e) {
179183
console.error(e);
@@ -194,6 +198,7 @@ interface codeActionExtractorConfig {
194198
range: p.Range;
195199
diagnostic: p.Diagnostic;
196200
codeActions: filesCodeActions;
201+
fileContentCache: Map<string, string>;
197202
}
198203

199204
type codeActionExtractor = (config: codeActionExtractorConfig) => boolean;
@@ -319,12 +324,14 @@ let handleUndefinedRecordFieldsAction = ({
319324
file,
320325
range,
321326
diagnostic,
327+
fileContentCache
322328
}: {
323329
recordFieldNames: string[];
324330
codeActions: filesCodeActions;
325331
file: string;
326332
range: p.Range;
327333
diagnostic: p.Diagnostic;
334+
fileContentCache: Map<string, string>;
328335
}) => {
329336
if (recordFieldNames != null) {
330337
codeActions[file] = codeActions[file] || [];
@@ -379,8 +386,19 @@ let handleUndefinedRecordFieldsAction = ({
379386
// Let's put the end brace back where it was (we still have it to the direct right of us).
380387
newText += `${paddingContentEndBrace}`;
381388
} else {
389+
let insertLeadingComma = true
390+
const fileContent = fileContentCache.get(file.replace("file://", ""))
391+
if (fileContent) {
392+
const textDocument = TextDocument.create(file, "text", 0, fileContent)
393+
if (textDocument.getText(range) == "{}") {
394+
insertLeadingComma = false
395+
}
396+
}
397+
382398
// A single line record definition body is a bit easier - we'll just add the new fields on the same line.
383-
newText += ", ";
399+
if (insertLeadingComma) {
400+
newText += ", ";
401+
}
384402
newText += recordFieldNames
385403
.map((fieldName) => `${fieldName}: failwith("TODO")`)
386404
.join(", ");
@@ -421,6 +439,7 @@ let addUndefinedRecordFieldsV10: codeActionExtractor = ({
421439
index,
422440
line,
423441
range,
442+
fileContentCache,
424443
}) => {
425444
if (line.startsWith("Some record fields are undefined:")) {
426445
let recordFieldNames = line
@@ -440,6 +459,7 @@ let addUndefinedRecordFieldsV10: codeActionExtractor = ({
440459
diagnostic,
441460
file,
442461
range,
462+
fileContentCache,
443463
});
444464
}
445465

@@ -454,6 +474,7 @@ let addUndefinedRecordFieldsV11: codeActionExtractor = ({
454474
index,
455475
line,
456476
range,
477+
fileContentCache
457478
}) => {
458479
if (line.startsWith("Some required record fields are missing:")) {
459480
let theLine = line;
@@ -486,6 +507,7 @@ let addUndefinedRecordFieldsV11: codeActionExtractor = ({
486507
diagnostic,
487508
file,
488509
range,
510+
fileContentCache,
489511
});
490512
}
491513

server/src/incrementalCompilation.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,8 +644,13 @@ async function compileContents(
644644
}
645645
// Reset compilation status as this compilation finished
646646
entry.compilation = null;
647+
648+
const fileContentCache = new Map();
649+
fileContentCache.set(entry.file.incrementalFilePath, fileContent)
650+
647651
const { result, codeActions } = utils.parseCompilerLogOutput(
648-
`${stderr}\n#Done()`
652+
`${stderr}\n#Done()`,
653+
fileContentCache
649654
);
650655

651656
const actions = Object.values(codeActions)[0] ?? [];

server/src/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ let sendUpdatedDiagnostics = () => {
102102
result: filesAndErrors,
103103
codeActions,
104104
linesWithParseErrors,
105-
} = utils.parseCompilerLogOutput(content);
105+
} = utils.parseCompilerLogOutput(content, stupidFileContentCache);
106106

107107
if (linesWithParseErrors.length > 0) {
108108
let params: p.ShowMessageParams = {

server/src/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,8 @@ type parsedCompilerLogResult = {
509509
linesWithParseErrors: string[];
510510
};
511511
export let parseCompilerLogOutput = (
512-
content: string
512+
content: string,
513+
fileContentCache: Map<string, string>
513514
): parsedCompilerLogResult => {
514515
type parsedDiagnostic = {
515516
code: number | undefined;
@@ -680,6 +681,7 @@ export let parseCompilerLogOutput = (
680681
diagnosticMessage,
681682
file,
682683
range,
684+
fileContentCache,
683685
});
684686

685687
result[file].push(diagnostic);

0 commit comments

Comments
 (0)