Skip to content

Commit

Permalink
Improve the dx of running the code analyzer some (#995)
Browse files Browse the repository at this point in the history
* improve the dx of running the code analyzer some

* changelog
  • Loading branch information
zth authored May 29, 2024
1 parent 4b555e5 commit 87bf5bd
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 35 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
- Adjust function template snippet return. https://github.com/rescript-lang/rescript-vscode/pull/985
- Don't expand `type t` maker functions in patterns. https://github.com/rescript-lang/rescript-vscode/pull/986
- Use `loc` for identifiers to get more and better completions in certain scenarios with type parameters. https://github.com/rescript-lang/rescript-vscode/pull/993
- Improve the DX of running the code analyzer some. https://github.com/rescript-lang/rescript-vscode/pull/995

#### :rocket: New Feature

Expand Down
20 changes: 8 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,21 @@ Even if the pre-release channel seems too experimental to you, we still suggest

You'll find all ReScript specific settings under the scope `rescript.settings`.

| Setting | Description |
| ------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Prompt to Start Build | If there's no ReScript build running already in the opened project, the extension will prompt you and ask if you want to start a build automatically. You can turn off this automatic prompt via the setting `rescript.settings.askToStartBuild`. |
| ReScript Binary Path | The extension will look for the existence of a `node_modules/.bin/rescript` file and use its directory as the `binaryPath`. If it does not find it at the project root (which is where the nearest `rescript.json` resides), it goes up folders in the filesystem recursively until it either finds it (often the case in monorepos) or hits the top level. To override this lookup process, the path can be configured explicitly using the setting `rescript.settings.binaryPath` |
| ReScript Platform Path | The extension will look for the existence of a `node_modules/rescript` directory and use the subdirectory corresponding to the current platform as the `platformPath`. If it does not find it at the project root (which is where the nearest `rescript.json` resides), it goes up folders in the filesystem recursively until it either finds it (often the case in monorepos) or hits the top level. To override this lookup process, the path can be configured explicitly using the setting `rescript.settings.platformPath` |
| Inlay Hints (experimental) | This allows an editor to place annotations inline with text to display type hints. Enable using `rescript.settings.inlayHints.enable: true` |
| Code Lens (experimental) | This tells the editor to add code lenses to function definitions, showing its full type above the definition. Enable using `rescript.settings.codeLens: true` |
| Signature Help | This tells the editor to show signature help when you're writing function calls. Enable using `rescript.settings.signatureHelp.enabled: true` |
| Autostarting the Code Analyzer | The Code Analyzer needs to be started manually by default. However, you can configure the extension to start the Code Analyzer automatically via the setting `rescript.settings.autoRunCodeAnalysis`. |
| Setting | Description |
| -------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Prompt to Start Build | If there's no ReScript build running already in the opened project, the extension will prompt you and ask if you want to start a build automatically. You can turn off this automatic prompt via the setting `rescript.settings.askToStartBuild`. |
| ReScript Binary Path | The extension will look for the existence of a `node_modules/.bin/rescript` file and use its directory as the `binaryPath`. If it does not find it at the project root (which is where the nearest `rescript.json` resides), it goes up folders in the filesystem recursively until it either finds it (often the case in monorepos) or hits the top level. To override this lookup process, the path can be configured explicitly using the setting `rescript.settings.binaryPath` |
| ReScript Platform Path | The extension will look for the existence of a `node_modules/rescript` directory and use the subdirectory corresponding to the current platform as the `platformPath`. If it does not find it at the project root (which is where the nearest `rescript.json` resides), it goes up folders in the filesystem recursively until it either finds it (often the case in monorepos) or hits the top level. To override this lookup process, the path can be configured explicitly using the setting `rescript.settings.platformPath` |
| Inlay Hints (experimental) | This allows an editor to place annotations inline with text to display type hints. Enable using `rescript.settings.inlayHints.enable: true` |
| Code Lens (experimental) | This tells the editor to add code lenses to function definitions, showing its full type above the definition. Enable using `rescript.settings.codeLens: true` |
| Signature Help | This tells the editor to show signature help when you're writing function calls. Enable using `rescript.settings.signatureHelp.enabled: true` |

**Default settings:**

```jsonc
// Whether you want the extension to prompt for autostarting a ReScript build if a project is opened with no build running
"rescript.settings.askToStartBuild": true,

// Automatically start ReScript's code analysis.
"rescript.settings.autoRunCodeAnalysis": false,

// Path to the directory where cross-platform ReScript binaries are. You can use it if you haven't or don't want to use the installed ReScript from node_modules in your project.
"rescript.settings.binaryPath": null

Expand Down
8 changes: 5 additions & 3 deletions client/src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DiagnosticCollection, OutputChannel } from "vscode";
import { DiagnosticCollection, OutputChannel, StatusBarItem } from "vscode";

import {
DiagnosticsResultCodeActionsMap,
Expand All @@ -14,12 +14,14 @@ export const codeAnalysisWithReanalyze = (
targetDir: string | null,
diagnosticsCollection: DiagnosticCollection,
diagnosticsResultCodeActions: DiagnosticsResultCodeActionsMap,
outputChannel: OutputChannel
outputChannel: OutputChannel,
codeAnalysisRunningStatusBarItem: StatusBarItem
) => {
runCodeAnalysisWithReanalyze(
targetDir,
diagnosticsCollection,
diagnosticsResultCodeActions,
outputChannel
outputChannel,
codeAnalysisRunningStatusBarItem
);
};
34 changes: 33 additions & 1 deletion client/src/commands/code_analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,27 @@ import {
CodeActionKind,
WorkspaceEdit,
OutputChannel,
StatusBarItem,
} from "vscode";
import { analysisProdPath, getAnalysisBinaryPath } from "../utils";

export let statusBarItem = {
setToStopText: (codeAnalysisRunningStatusBarItem: StatusBarItem) => {
codeAnalysisRunningStatusBarItem.text = "$(debug-stop) Stop Code Analyzer";
codeAnalysisRunningStatusBarItem.tooltip = null;
},
setToRunningText: (codeAnalysisRunningStatusBarItem: StatusBarItem) => {
codeAnalysisRunningStatusBarItem.text =
"$(loading~spin) Running code analysis...";
codeAnalysisRunningStatusBarItem.tooltip = null;
},
setToFailed: (codeAnalysisRunningStatusBarItem: StatusBarItem) => {
codeAnalysisRunningStatusBarItem.text = "$(alert) Failed";
codeAnalysisRunningStatusBarItem.tooltip =
"Something went wrong when running the code analysis.";
},
};

export type DiagnosticsResultCodeActionsMap = Map<
string,
{ range: Range; codeAction: CodeAction }[]
Expand Down Expand Up @@ -42,6 +60,7 @@ enum ClassifiedMessage {
let classifyMessage = (msg: string) => {
if (
msg.endsWith(" is never used") ||
msg.endsWith(" is never used and could have side effects") ||
msg.endsWith(" has no side effects and can be removed")
) {
return ClassifiedMessage.Removable;
Expand Down Expand Up @@ -152,6 +171,12 @@ let resultsToDiagnostics = (
""
);

codeAction.command = {
command: "rescript-vscode.clear_diagnostic",
title: "Clear diagnostic",
arguments: [diagnostic],
};

codeAction.edit = codeActionEdit;

if (diagnosticsResultCodeActions.has(item.file)) {
Expand All @@ -177,7 +202,8 @@ export const runCodeAnalysisWithReanalyze = (
targetDir: string | null,
diagnosticsCollection: DiagnosticCollection,
diagnosticsResultCodeActions: DiagnosticsResultCodeActionsMap,
outputChannel: OutputChannel
outputChannel: OutputChannel,
codeAnalysisRunningStatusBarItem: StatusBarItem
) => {
let currentDocument = window.activeTextEditor.document;
let cwd = targetDir ?? path.dirname(currentDocument.uri.fsPath);
Expand All @@ -188,12 +214,15 @@ export const runCodeAnalysisWithReanalyze = (
return;
}

statusBarItem.setToRunningText(codeAnalysisRunningStatusBarItem);

let opts = ["reanalyze", "-json"];
let p = cp.spawn(binaryPath, opts, {
cwd,
});

if (p.stdout == null) {
statusBarItem.setToFailed(codeAnalysisRunningStatusBarItem);
window.showErrorMessage("Something went wrong.");
return;
}
Expand Down Expand Up @@ -252,6 +281,7 @@ export const runCodeAnalysisWithReanalyze = (
if (json == null) {
// If reanalyze failed for some reason we'll clear the diagnostics.
diagnosticsCollection.clear();
statusBarItem.setToFailed(codeAnalysisRunningStatusBarItem);
return;
}

Expand All @@ -272,5 +302,7 @@ export const runCodeAnalysisWithReanalyze = (
diagnosticsMap.forEach((diagnostics, filePath) => {
diagnosticsCollection.set(Uri.parse(filePath), diagnostics);
});

statusBarItem.setToStopText(codeAnalysisRunningStatusBarItem);
});
};
41 changes: 27 additions & 14 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
CodeAction,
WorkspaceEdit,
CodeActionKind,
Diagnostic,
} from "vscode";

import {
Expand All @@ -23,7 +24,10 @@ import {
} from "vscode-languageclient/node";

import * as customCommands from "./commands";
import { DiagnosticsResultCodeActionsMap } from "./commands/code_analysis";
import {
DiagnosticsResultCodeActionsMap,
statusBarItem,
} from "./commands/code_analysis";

let client: LanguageClient;

Expand Down Expand Up @@ -150,7 +154,8 @@ export function activate(context: ExtensionContext) {
inCodeAnalysisState.activatedFromDirectory,
diagnosticsCollection,
diagnosticsResultCodeActions,
outputChannel
outputChannel,
codeAnalysisRunningStatusBarItem
);
}
})
Expand Down Expand Up @@ -228,6 +233,21 @@ export function activate(context: ExtensionContext) {
customCommands.createInterface(client);
});

commands.registerCommand(
"rescript-vscode.clear_diagnostic",
(diagnostic: Diagnostic) => {
const editor = window.activeTextEditor;
if (!editor) {
return;
}

const document = editor.document;
const diagnostics = diagnosticsCollection.get(document.uri);
const newDiagnostics = diagnostics.filter((d) => d !== diagnostic);
diagnosticsCollection.set(document.uri, newDiagnostics);
}
);

commands.registerCommand("rescript-vscode.open_compiled", () => {
customCommands.openCompiled(client);
});
Expand Down Expand Up @@ -272,13 +292,14 @@ export function activate(context: ExtensionContext) {
codeAnalysisRunningStatusBarItem.command =
"rescript-vscode.stop_code_analysis";
codeAnalysisRunningStatusBarItem.show();
codeAnalysisRunningStatusBarItem.text = "$(debug-stop) Stop Code Analyzer";
statusBarItem.setToStopText(codeAnalysisRunningStatusBarItem);

customCommands.codeAnalysisWithReanalyze(
inCodeAnalysisState.activatedFromDirectory,
diagnosticsCollection,
diagnosticsResultCodeActions,
outputChannel
outputChannel,
codeAnalysisRunningStatusBarItem
);
});

Expand Down Expand Up @@ -319,7 +340,8 @@ export function activate(context: ExtensionContext) {
if (
affectsConfiguration("rescript.settings.inlayHints") ||
affectsConfiguration("rescript.settings.codeLens") ||
affectsConfiguration("rescript.settings.signatureHelp")
affectsConfiguration("rescript.settings.signatureHelp") ||
affectsConfiguration("rescript.settings.incrementalTypechecking")
) {
commands.executeCommand("rescript-vscode.restart_language_server");
} else {
Expand All @@ -333,15 +355,6 @@ export function activate(context: ExtensionContext) {
}
})
);

// Autostart code analysis if wanted
if (
workspace
.getConfiguration("rescript.settings")
.get<boolean>("autoRunCodeAnalysis")
) {
commands.executeCommand("rescript-vscode.start_code_analysis");
}
}

export function deactivate(): Thenable<void> | undefined {
Expand Down
5 changes: 0 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,6 @@
"default": true,
"description": "Whether you want the extension to prompt for autostarting a ReScript build if a project is opened with no build running."
},
"rescript.settings.autoRunCodeAnalysis": {
"type": "boolean",
"default": false,
"description": "Automatically start ReScript's code analysis."
},
"rescript.settings.inlayHints.enable": {
"type": "boolean",
"default": false,
Expand Down

0 comments on commit 87bf5bd

Please sign in to comment.