Skip to content

Code actions for removing dead code #989

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

Merged
merged 4 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -35,6 +35,7 @@
- Complete `%todo`. https://github.com/rescript-lang/rescript-vscode/pull/981
- Add code action for extracting a locally defined module into its own file. https://github.com/rescript-lang/rescript-vscode/pull/983
- Add code action for expanding catch-all patterns. https://github.com/rescript-lang/rescript-vscode/pull/987
- Add code actions for removing unused code (per item and for an entire file), driven by `reanalyze`. https://github.com/rescript-lang/rescript-vscode/pull/989

## 1.50.0

Expand Down
32 changes: 30 additions & 2 deletions client/src/commands/code_analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ let resultsToDiagnostics = (

let codeActionEdit = new WorkspaceEdit();

// In the future, it would be cool to have an additional code action
// here for automatically removing whatever the thing that's dead is.
codeActionEdit.replace(
Uri.parse(item.file),
// Make sure the full line is replaced
Expand All @@ -119,6 +117,36 @@ let resultsToDiagnostics = (
}
}
}

if (item.message.endsWith(" is never used")) {
{
let codeAction = new CodeAction("Remove unused");
codeAction.kind = CodeActionKind.RefactorRewrite;

let codeActionEdit = new WorkspaceEdit();

codeActionEdit.replace(
Uri.parse(item.file),
new Range(
new Position(item.range[0], item.range[1]),
new Position(item.range[2], item.range[3])
),
""
);

codeAction.edit = codeActionEdit;

if (diagnosticsResultCodeActions.has(item.file)) {
diagnosticsResultCodeActions
.get(item.file)
.push({ range: issueLocationRange, codeAction });
} else {
diagnosticsResultCodeActions.set(item.file, [
{ range: issueLocationRange, codeAction },
]);
}
}
}
}
});

Expand Down
32 changes: 29 additions & 3 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import {
Uri,
Range,
Position,
CodeAction,
WorkspaceEdit,
CodeActionKind,
} from "vscode";

import {
Expand All @@ -17,7 +20,7 @@ import {
ServerOptions,
State,
Executable,
TransportKind
TransportKind,
} from "vscode-languageclient/node";

import * as customCommands from "./commands";
Expand Down Expand Up @@ -91,7 +94,11 @@ export function activate(context: ExtensionContext) {
// If the extension is launched in debug mode then the debug server options are used
// Otherwise the run options are used
let serverOptions: ServerOptions = {
run: { module: serverModule, args: ["--node-ipc"], transport: TransportKind.ipc },
run: {
module: serverModule,
args: ["--node-ipc"],
transport: TransportKind.ipc,
},
debug: {
module: serverModule,
args: ["--node-ipc"],
Expand Down Expand Up @@ -189,12 +196,31 @@ export function activate(context: ExtensionContext) {
let availableActions =
diagnosticsResultCodeActions.get(document.uri.fsPath) ?? [];

return availableActions
const allRemoveActionEdits = availableActions.filter(
({ codeAction }) => codeAction.title === "Remove unused"
);

const actions: CodeAction[] = availableActions
.filter(
({ range }) =>
range.contains(rangeOrSelection) || range.isEqual(rangeOrSelection)
)
.map(({ codeAction }) => codeAction);

if (allRemoveActionEdits.length > 0) {
const removeAllCodeAction = new CodeAction("Remove all unused in file");
const edit = new WorkspaceEdit();
allRemoveActionEdits.forEach((subEdit) => {
subEdit.codeAction.edit.entries().forEach(([uri, [textEdit]]) => {
edit.replace(uri, textEdit.range, textEdit.newText);
});
});
removeAllCodeAction.kind = CodeActionKind.RefactorRewrite;
removeAllCodeAction.edit = edit;
actions.push(removeAllCodeAction);
}

return actions;
},
});

Expand Down
Loading