Skip to content
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

Code action: Wrap in Some #806

Merged
merged 2 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

- Add support for syntax highlighting in `%raw` and `%ffi` extension points. https://github.com/rescript-lang/rescript-vscode/pull/774
- Add completion to top level decorators. https://github.com/rescript-lang/rescript-vscode/pull/799
- Add code action for wrapping patterns where option is expected with `Some`. https://github.com/rescript-lang/rescript-vscode/pull/806

#### :nail_care: Polish

Expand Down
70 changes: 70 additions & 0 deletions server/src/codeActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export let findCodeActionsInDiagnosticsMessage = ({
simpleConversion,
applyUncurried,
simpleAddMissingCases,
wrapInSome,
];

for (let extractCodeAction of codeActionEtractors) {
Expand Down Expand Up @@ -240,6 +241,75 @@ let didYouMeanAction: codeActionExtractor = ({
return false;
};

// This action offers to wrap patterns that aren't option in Some.
let wrapInSome: codeActionExtractor = ({
codeActions,
diagnostic,
file,
line,
range,
array,
index,
}) => {
if (line.startsWith("This pattern matches values of type")) {
let regex = /This pattern matches values of type (.*)$/;

let match = line.match(regex);

if (match === null) {
return false;
}

let [_, type] = match;

if (!type.startsWith("option<")) {
// Look for the expected type
let restOfMessage = array.slice(index + 1);
let lineIndexWithType = restOfMessage.findIndex((l) =>
l
.trim()
.startsWith("but a pattern was expected which matches values of type")
);

if (lineIndexWithType === -1) return false;
// The type is either on this line or the next
let [_, typ = ""] = restOfMessage[lineIndexWithType].split(
"but a pattern was expected which matches values of type"
);

if (typ.trim() === "") {
// Type is on the next line
typ = (restOfMessage[lineIndexWithType + 1] ?? "").trim();
}

if (typ.trim().startsWith("option<")) {
codeActions[file] = codeActions[file] || [];

let codeAction: p.CodeAction = {
title: `Wrap in option Some`,
edit: {
changes: {
[file]: wrapRangeInText(range, `Some(`, `)`),
},
},
diagnostics: [diagnostic],
kind: p.CodeActionKind.QuickFix,
isPreferred: true,
};

codeActions[file].push({
range,
codeAction,
});

return true;
}
}
}

return false;
};

// This action handles when the compiler errors on certain fields of a record
// being undefined. We then offers an action that inserts all of the record
// fields, with an `assert false` dummy value. `assert false` is so applying the
Expand Down