Skip to content

Commit ebcdbc5

Browse files
authored
Fix "Add missing record fields" code action on v11+ (#1088)
* Don't cut off last character of last record field mentioned in missing fields error * Typo * Use file content cache for detecting empty record * Revert "Use file content cache for detecting empty record" This reverts commit 2265806. * Use range character difference to decide whether to insert initial trailing comma * Use %todo instead of failwith on v11+
1 parent aca332d commit ebcdbc5

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

server/src/codeActions.ts

+20-6
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ export let findCodeActionsInDiagnosticsMessage = ({
151151
diagnosticMessage.forEach((line, index, array) => {
152152
// Because of how actions work, there can only be one per diagnostic. So,
153153
// halt whenever a code action has been found.
154-
let codeActionEtractors = [
154+
let codeActionExtractors = [
155155
simpleTypeMismatches,
156156
didYouMeanAction,
157157
addUndefinedRecordFieldsV10,
@@ -162,7 +162,7 @@ export let findCodeActionsInDiagnosticsMessage = ({
162162
wrapInSome,
163163
];
164164

165-
for (let extractCodeAction of codeActionEtractors) {
165+
for (let extractCodeAction of codeActionExtractors) {
166166
let didFindAction = false;
167167

168168
try {
@@ -319,12 +319,14 @@ let handleUndefinedRecordFieldsAction = ({
319319
file,
320320
range,
321321
diagnostic,
322+
todoValue
322323
}: {
323324
recordFieldNames: string[];
324325
codeActions: filesCodeActions;
325326
file: string;
326327
range: p.Range;
327328
diagnostic: p.Diagnostic;
329+
todoValue: string
328330
}) => {
329331
if (recordFieldNames != null) {
330332
codeActions[file] = codeActions[file] || [];
@@ -373,16 +375,26 @@ let handleUndefinedRecordFieldsAction = ({
373375
newText += paddingContentRecordField;
374376
}
375377

376-
newText += `${fieldName}: failwith("TODO"),\n`;
378+
newText += `${fieldName}: ${todoValue},\n`;
377379
});
378380

379381
// Let's put the end brace back where it was (we still have it to the direct right of us).
380382
newText += `${paddingContentEndBrace}`;
381383
} else {
382384
// A single line record definition body is a bit easier - we'll just add the new fields on the same line.
383-
newText += ", ";
385+
386+
// For an empty record (`range.end.character - range.start.character == 2`),
387+
// we don't want to add an initial trailing comma as that would be invalid syntax.
388+
//
389+
// We assume that records that already contain some characters between
390+
// their braces have at least one field and therefore we need to insert
391+
// an initial trailing comma.
392+
if (range.end.character - range.start.character > 2) {
393+
newText += ", ";
394+
}
395+
384396
newText += recordFieldNames
385-
.map((fieldName) => `${fieldName}: failwith("TODO")`)
397+
.map((fieldName) => `${fieldName}: ${todoValue}`)
386398
.join(", ");
387399
}
388400

@@ -440,6 +452,7 @@ let addUndefinedRecordFieldsV10: codeActionExtractor = ({
440452
diagnostic,
441453
file,
442454
range,
455+
todoValue: `failwith("TODO")`
443456
});
444457
}
445458

@@ -458,7 +471,7 @@ let addUndefinedRecordFieldsV11: codeActionExtractor = ({
458471
if (line.startsWith("Some required record fields are missing:")) {
459472
let theLine = line;
460473
if (theLine.endsWith(".")) {
461-
theLine = theLine.slice(0, theLine.length - 2);
474+
theLine = theLine.slice(0, theLine.length - 1);
462475
}
463476

464477
let recordFieldNames = theLine
@@ -486,6 +499,7 @@ let addUndefinedRecordFieldsV11: codeActionExtractor = ({
486499
diagnostic,
487500
file,
488501
range,
502+
todoValue: `%todo`
489503
});
490504
}
491505

0 commit comments

Comments
 (0)