Skip to content

Commit 87bf5bd

Browse files
authored
Improve the dx of running the code analyzer some (#995)
* improve the dx of running the code analyzer some * changelog
1 parent 4b555e5 commit 87bf5bd

File tree

6 files changed

+74
-35
lines changed

6 files changed

+74
-35
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
- Adjust function template snippet return. https://github.com/rescript-lang/rescript-vscode/pull/985
3030
- Don't expand `type t` maker functions in patterns. https://github.com/rescript-lang/rescript-vscode/pull/986
3131
- 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
32+
- Improve the DX of running the code analyzer some. https://github.com/rescript-lang/rescript-vscode/pull/995
3233

3334
#### :rocket: New Feature
3435

README.md

+8-12
Original file line numberDiff line numberDiff line change
@@ -97,25 +97,21 @@ Even if the pre-release channel seems too experimental to you, we still suggest
9797

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

100-
| Setting | Description |
101-
| ------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
102-
| 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`. |
103-
| 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` |
104-
| 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` |
105-
| Inlay Hints (experimental) | This allows an editor to place annotations inline with text to display type hints. Enable using `rescript.settings.inlayHints.enable: true` |
106-
| 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` |
107-
| Signature Help | This tells the editor to show signature help when you're writing function calls. Enable using `rescript.settings.signatureHelp.enabled: true` |
108-
| 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`. |
100+
| Setting | Description |
101+
| -------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
102+
| 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`. |
103+
| 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` |
104+
| 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` |
105+
| Inlay Hints (experimental) | This allows an editor to place annotations inline with text to display type hints. Enable using `rescript.settings.inlayHints.enable: true` |
106+
| 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` |
107+
| Signature Help | This tells the editor to show signature help when you're writing function calls. Enable using `rescript.settings.signatureHelp.enabled: true` |
109108

110109
**Default settings:**
111110

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

116-
// Automatically start ReScript's code analysis.
117-
"rescript.settings.autoRunCodeAnalysis": false,
118-
119115
// 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.
120116
"rescript.settings.binaryPath": null
121117

client/src/commands.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DiagnosticCollection, OutputChannel } from "vscode";
1+
import { DiagnosticCollection, OutputChannel, StatusBarItem } from "vscode";
22

33
import {
44
DiagnosticsResultCodeActionsMap,
@@ -14,12 +14,14 @@ export const codeAnalysisWithReanalyze = (
1414
targetDir: string | null,
1515
diagnosticsCollection: DiagnosticCollection,
1616
diagnosticsResultCodeActions: DiagnosticsResultCodeActionsMap,
17-
outputChannel: OutputChannel
17+
outputChannel: OutputChannel,
18+
codeAnalysisRunningStatusBarItem: StatusBarItem
1819
) => {
1920
runCodeAnalysisWithReanalyze(
2021
targetDir,
2122
diagnosticsCollection,
2223
diagnosticsResultCodeActions,
23-
outputChannel
24+
outputChannel,
25+
codeAnalysisRunningStatusBarItem
2426
);
2527
};

client/src/commands/code_analysis.ts

+33-1
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,27 @@ import {
1212
CodeActionKind,
1313
WorkspaceEdit,
1414
OutputChannel,
15+
StatusBarItem,
1516
} from "vscode";
1617
import { analysisProdPath, getAnalysisBinaryPath } from "../utils";
1718

19+
export let statusBarItem = {
20+
setToStopText: (codeAnalysisRunningStatusBarItem: StatusBarItem) => {
21+
codeAnalysisRunningStatusBarItem.text = "$(debug-stop) Stop Code Analyzer";
22+
codeAnalysisRunningStatusBarItem.tooltip = null;
23+
},
24+
setToRunningText: (codeAnalysisRunningStatusBarItem: StatusBarItem) => {
25+
codeAnalysisRunningStatusBarItem.text =
26+
"$(loading~spin) Running code analysis...";
27+
codeAnalysisRunningStatusBarItem.tooltip = null;
28+
},
29+
setToFailed: (codeAnalysisRunningStatusBarItem: StatusBarItem) => {
30+
codeAnalysisRunningStatusBarItem.text = "$(alert) Failed";
31+
codeAnalysisRunningStatusBarItem.tooltip =
32+
"Something went wrong when running the code analysis.";
33+
},
34+
};
35+
1836
export type DiagnosticsResultCodeActionsMap = Map<
1937
string,
2038
{ range: Range; codeAction: CodeAction }[]
@@ -42,6 +60,7 @@ enum ClassifiedMessage {
4260
let classifyMessage = (msg: string) => {
4361
if (
4462
msg.endsWith(" is never used") ||
63+
msg.endsWith(" is never used and could have side effects") ||
4564
msg.endsWith(" has no side effects and can be removed")
4665
) {
4766
return ClassifiedMessage.Removable;
@@ -152,6 +171,12 @@ let resultsToDiagnostics = (
152171
""
153172
);
154173

174+
codeAction.command = {
175+
command: "rescript-vscode.clear_diagnostic",
176+
title: "Clear diagnostic",
177+
arguments: [diagnostic],
178+
};
179+
155180
codeAction.edit = codeActionEdit;
156181

157182
if (diagnosticsResultCodeActions.has(item.file)) {
@@ -177,7 +202,8 @@ export const runCodeAnalysisWithReanalyze = (
177202
targetDir: string | null,
178203
diagnosticsCollection: DiagnosticCollection,
179204
diagnosticsResultCodeActions: DiagnosticsResultCodeActionsMap,
180-
outputChannel: OutputChannel
205+
outputChannel: OutputChannel,
206+
codeAnalysisRunningStatusBarItem: StatusBarItem
181207
) => {
182208
let currentDocument = window.activeTextEditor.document;
183209
let cwd = targetDir ?? path.dirname(currentDocument.uri.fsPath);
@@ -188,12 +214,15 @@ export const runCodeAnalysisWithReanalyze = (
188214
return;
189215
}
190216

217+
statusBarItem.setToRunningText(codeAnalysisRunningStatusBarItem);
218+
191219
let opts = ["reanalyze", "-json"];
192220
let p = cp.spawn(binaryPath, opts, {
193221
cwd,
194222
});
195223

196224
if (p.stdout == null) {
225+
statusBarItem.setToFailed(codeAnalysisRunningStatusBarItem);
197226
window.showErrorMessage("Something went wrong.");
198227
return;
199228
}
@@ -252,6 +281,7 @@ export const runCodeAnalysisWithReanalyze = (
252281
if (json == null) {
253282
// If reanalyze failed for some reason we'll clear the diagnostics.
254283
diagnosticsCollection.clear();
284+
statusBarItem.setToFailed(codeAnalysisRunningStatusBarItem);
255285
return;
256286
}
257287

@@ -272,5 +302,7 @@ export const runCodeAnalysisWithReanalyze = (
272302
diagnosticsMap.forEach((diagnostics, filePath) => {
273303
diagnosticsCollection.set(Uri.parse(filePath), diagnostics);
274304
});
305+
306+
statusBarItem.setToStopText(codeAnalysisRunningStatusBarItem);
275307
});
276308
};

client/src/extension.ts

+27-14
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
CodeAction,
1313
WorkspaceEdit,
1414
CodeActionKind,
15+
Diagnostic,
1516
} from "vscode";
1617

1718
import {
@@ -23,7 +24,10 @@ import {
2324
} from "vscode-languageclient/node";
2425

2526
import * as customCommands from "./commands";
26-
import { DiagnosticsResultCodeActionsMap } from "./commands/code_analysis";
27+
import {
28+
DiagnosticsResultCodeActionsMap,
29+
statusBarItem,
30+
} from "./commands/code_analysis";
2731

2832
let client: LanguageClient;
2933

@@ -150,7 +154,8 @@ export function activate(context: ExtensionContext) {
150154
inCodeAnalysisState.activatedFromDirectory,
151155
diagnosticsCollection,
152156
diagnosticsResultCodeActions,
153-
outputChannel
157+
outputChannel,
158+
codeAnalysisRunningStatusBarItem
154159
);
155160
}
156161
})
@@ -228,6 +233,21 @@ export function activate(context: ExtensionContext) {
228233
customCommands.createInterface(client);
229234
});
230235

236+
commands.registerCommand(
237+
"rescript-vscode.clear_diagnostic",
238+
(diagnostic: Diagnostic) => {
239+
const editor = window.activeTextEditor;
240+
if (!editor) {
241+
return;
242+
}
243+
244+
const document = editor.document;
245+
const diagnostics = diagnosticsCollection.get(document.uri);
246+
const newDiagnostics = diagnostics.filter((d) => d !== diagnostic);
247+
diagnosticsCollection.set(document.uri, newDiagnostics);
248+
}
249+
);
250+
231251
commands.registerCommand("rescript-vscode.open_compiled", () => {
232252
customCommands.openCompiled(client);
233253
});
@@ -272,13 +292,14 @@ export function activate(context: ExtensionContext) {
272292
codeAnalysisRunningStatusBarItem.command =
273293
"rescript-vscode.stop_code_analysis";
274294
codeAnalysisRunningStatusBarItem.show();
275-
codeAnalysisRunningStatusBarItem.text = "$(debug-stop) Stop Code Analyzer";
295+
statusBarItem.setToStopText(codeAnalysisRunningStatusBarItem);
276296

277297
customCommands.codeAnalysisWithReanalyze(
278298
inCodeAnalysisState.activatedFromDirectory,
279299
diagnosticsCollection,
280300
diagnosticsResultCodeActions,
281-
outputChannel
301+
outputChannel,
302+
codeAnalysisRunningStatusBarItem
282303
);
283304
});
284305

@@ -319,7 +340,8 @@ export function activate(context: ExtensionContext) {
319340
if (
320341
affectsConfiguration("rescript.settings.inlayHints") ||
321342
affectsConfiguration("rescript.settings.codeLens") ||
322-
affectsConfiguration("rescript.settings.signatureHelp")
343+
affectsConfiguration("rescript.settings.signatureHelp") ||
344+
affectsConfiguration("rescript.settings.incrementalTypechecking")
323345
) {
324346
commands.executeCommand("rescript-vscode.restart_language_server");
325347
} else {
@@ -333,15 +355,6 @@ export function activate(context: ExtensionContext) {
333355
}
334356
})
335357
);
336-
337-
// Autostart code analysis if wanted
338-
if (
339-
workspace
340-
.getConfiguration("rescript.settings")
341-
.get<boolean>("autoRunCodeAnalysis")
342-
) {
343-
commands.executeCommand("rescript-vscode.start_code_analysis");
344-
}
345358
}
346359

347360
export function deactivate(): Thenable<void> | undefined {

package.json

-5
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,6 @@
148148
"default": true,
149149
"description": "Whether you want the extension to prompt for autostarting a ReScript build if a project is opened with no build running."
150150
},
151-
"rescript.settings.autoRunCodeAnalysis": {
152-
"type": "boolean",
153-
"default": false,
154-
"description": "Automatically start ReScript's code analysis."
155-
},
156151
"rescript.settings.inlayHints.enable": {
157152
"type": "boolean",
158153
"default": false,

0 commit comments

Comments
 (0)