-
Notifications
You must be signed in to change notification settings - Fork 59
Fix "Add missing record fields" code action on v11+ #1088
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2cc4990
58b2d2d
2265806
8ec2efb
314fe3b
df9e9f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -151,7 +151,7 @@ export let findCodeActionsInDiagnosticsMessage = ({ | |
diagnosticMessage.forEach((line, index, array) => { | ||
// Because of how actions work, there can only be one per diagnostic. So, | ||
// halt whenever a code action has been found. | ||
let codeActionEtractors = [ | ||
let codeActionExtractors = [ | ||
simpleTypeMismatches, | ||
didYouMeanAction, | ||
addUndefinedRecordFieldsV10, | ||
|
@@ -162,7 +162,7 @@ export let findCodeActionsInDiagnosticsMessage = ({ | |
wrapInSome, | ||
]; | ||
|
||
for (let extractCodeAction of codeActionEtractors) { | ||
for (let extractCodeAction of codeActionExtractors) { | ||
let didFindAction = false; | ||
|
||
try { | ||
|
@@ -319,12 +319,14 @@ let handleUndefinedRecordFieldsAction = ({ | |
file, | ||
range, | ||
diagnostic, | ||
todoValue | ||
}: { | ||
recordFieldNames: string[]; | ||
codeActions: filesCodeActions; | ||
file: string; | ||
range: p.Range; | ||
diagnostic: p.Diagnostic; | ||
todoValue: string | ||
}) => { | ||
if (recordFieldNames != null) { | ||
codeActions[file] = codeActions[file] || []; | ||
|
@@ -373,16 +375,26 @@ let handleUndefinedRecordFieldsAction = ({ | |
newText += paddingContentRecordField; | ||
} | ||
|
||
newText += `${fieldName}: failwith("TODO"),\n`; | ||
newText += `${fieldName}: ${todoValue},\n`; | ||
}); | ||
|
||
// Let's put the end brace back where it was (we still have it to the direct right of us). | ||
newText += `${paddingContentEndBrace}`; | ||
} else { | ||
// A single line record definition body is a bit easier - we'll just add the new fields on the same line. | ||
newText += ", "; | ||
|
||
// For an empty record (`range.end.character - range.start.character == 2`), | ||
// we don't want to add an initial trailing comma as that would be invalid syntax. | ||
// | ||
// We assume that records that already contain some characters between | ||
// their braces have at least one field and therefore we need to insert | ||
// an initial trailing comma. | ||
if (range.end.character - range.start.character > 2) { | ||
newText += ", "; | ||
} | ||
|
||
newText += recordFieldNames | ||
.map((fieldName) => `${fieldName}: failwith("TODO")`) | ||
.map((fieldName) => `${fieldName}: ${todoValue}`) | ||
.join(", "); | ||
} | ||
|
||
|
@@ -440,6 +452,7 @@ let addUndefinedRecordFieldsV10: codeActionExtractor = ({ | |
diagnostic, | ||
file, | ||
range, | ||
todoValue: `failwith("TODO")` | ||
}); | ||
} | ||
|
||
|
@@ -458,7 +471,7 @@ let addUndefinedRecordFieldsV11: codeActionExtractor = ({ | |
if (line.startsWith("Some required record fields are missing:")) { | ||
let theLine = line; | ||
if (theLine.endsWith(".")) { | ||
theLine = theLine.slice(0, theLine.length - 2); | ||
theLine = theLine.slice(0, theLine.length - 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug fix 1: The last character of the last field was cut off |
||
} | ||
|
||
let recordFieldNames = theLine | ||
|
@@ -486,6 +499,7 @@ let addUndefinedRecordFieldsV11: codeActionExtractor = ({ | |
diagnostic, | ||
file, | ||
range, | ||
todoValue: `%todo` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since |
||
}); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug fix 2: initial comma was inserted when using this code action on empty records, resulting in invalid syntax