Skip to content

Commit ef2ee59

Browse files
committed
Auto merge of rust-lang#15446 - Veykril:checkOnSaveToggle, r=Veykril
Add status bar button to toggle check on save state Closes rust-lang/rust-analyzer#15440 cc rust-lang/rust-analyzer#13208
2 parents b771de3 + e76d20e commit ef2ee59

File tree

7 files changed

+69
-5
lines changed

7 files changed

+69
-5
lines changed

editors/code/package.json

+5
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,11 @@
292292
"command": "rust-analyzer.viewMemoryLayout",
293293
"title": "View Memory Layout",
294294
"category": "rust-analyzer"
295+
},
296+
{
297+
"command": "rust-analyzer.toggleCheckOnSave",
298+
"title": "Toggle Check on Save",
299+
"category": "rust-analyzer"
295300
}
296301
],
297302
"keybindings": [

editors/code/src/bootstrap.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export async function bootstrap(
2020

2121
log.info("Using server binary at", path);
2222

23-
if (!isValidExecutable(path)) {
23+
if (!isValidExecutable(path, config.serverExtraEnv)) {
2424
if (config.serverPath) {
2525
throw new Error(`Failed to execute ${path} --version. \`config.server.path\` or \`config.serverPath\` has been set explicitly.\
2626
Consider removing this config or making a valid server binary available at that path.`);

editors/code/src/commands.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1407,3 +1407,10 @@ locate()
14071407
ctx.pushExtCleanup(document);
14081408
};
14091409
}
1410+
1411+
export function toggleCheckOnSave(ctx: Ctx): Cmd {
1412+
return async () => {
1413+
await ctx.config.toggleCheckOnSave();
1414+
ctx.refreshServerStatus();
1415+
};
1416+
}

editors/code/src/config.ts

+33
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,39 @@ export class Config {
216216
),
217217
);
218218
}
219+
get checkOnSave() {
220+
return this.get<boolean>("checkOnSave") ?? false;
221+
}
222+
async toggleCheckOnSave() {
223+
const config = this.cfg.inspect<boolean>("checkOnSave") ?? { key: "checkOnSave" };
224+
let overrideInLanguage;
225+
let target;
226+
let value;
227+
if (
228+
config.workspaceFolderValue !== undefined ||
229+
config.workspaceFolderLanguageValue !== undefined
230+
) {
231+
target = vscode.ConfigurationTarget.WorkspaceFolder;
232+
overrideInLanguage = config.workspaceFolderLanguageValue;
233+
value = config.workspaceFolderValue || config.workspaceFolderLanguageValue;
234+
} else if (
235+
config.workspaceValue !== undefined ||
236+
config.workspaceLanguageValue !== undefined
237+
) {
238+
target = vscode.ConfigurationTarget.Workspace;
239+
overrideInLanguage = config.workspaceLanguageValue;
240+
value = config.workspaceValue || config.workspaceLanguageValue;
241+
} else if (config.globalValue !== undefined || config.globalLanguageValue !== undefined) {
242+
target = vscode.ConfigurationTarget.Global;
243+
overrideInLanguage = config.globalLanguageValue;
244+
value = config.globalValue || config.globalLanguageValue;
245+
} else if (config.defaultValue !== undefined || config.defaultLanguageValue !== undefined) {
246+
overrideInLanguage = config.defaultLanguageValue;
247+
value = config.defaultValue || config.defaultLanguageValue;
248+
}
249+
await this.cfg.update("checkOnSave", !(value || false), target || null, overrideInLanguage);
250+
}
251+
219252
get traceExtension() {
220253
return this.get<boolean>("trace.extension");
221254
}

editors/code/src/ctx.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export class Ctx {
9494
private unlinkedFiles: vscode.Uri[];
9595
private _dependencies: RustDependenciesProvider | undefined;
9696
private _treeView: vscode.TreeView<Dependency | DependencyFile | DependencyId> | undefined;
97+
private lastStatus: ServerStatusParams | { health: "stopped" } = { health: "stopped" };
9798

9899
get client() {
99100
return this._client;
@@ -404,7 +405,15 @@ export class Ctx {
404405
}
405406

406407
setServerStatus(status: ServerStatusParams | { health: "stopped" }) {
408+
this.lastStatus = status;
409+
this.updateStatusBarItem();
410+
}
411+
refreshServerStatus() {
412+
this.updateStatusBarItem();
413+
}
414+
private updateStatusBarItem() {
407415
let icon = "";
416+
const status = this.lastStatus;
408417
const statusBar = this.statusBar;
409418
statusBar.show();
410419
statusBar.tooltip = new vscode.MarkdownString("", true);
@@ -447,13 +456,18 @@ export class Ctx {
447456
"statusBarItem.warningBackground",
448457
);
449458
statusBar.command = "rust-analyzer.startServer";
450-
statusBar.text = `$(stop-circle) rust-analyzer`;
459+
statusBar.text = "$(stop-circle) rust-analyzer";
451460
return;
452461
}
453462
if (statusBar.tooltip.value) {
454463
statusBar.tooltip.appendMarkdown("\n\n---\n\n");
455464
}
456-
statusBar.tooltip.appendMarkdown("\n\n[Open logs](command:rust-analyzer.openLogs)");
465+
statusBar.tooltip.appendMarkdown("\n\n[Open Logs](command:rust-analyzer.openLogs)");
466+
statusBar.tooltip.appendMarkdown(
467+
`\n\n[${
468+
this.config.checkOnSave ? "Disable" : "Enable"
469+
} Check on Save](command:rust-analyzer.toggleCheckOnSave)`,
470+
);
457471
statusBar.tooltip.appendMarkdown(
458472
"\n\n[Reload Workspace](command:rust-analyzer.reloadWorkspace)",
459473
);

editors/code/src/main.ts

+1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ function createCommands(): Record<string, CommandFactory> {
180180
ssr: { enabled: commands.ssr },
181181
serverVersion: { enabled: commands.serverVersion },
182182
viewMemoryLayout: { enabled: commands.viewMemoryLayout },
183+
toggleCheckOnSave: { enabled: commands.toggleCheckOnSave },
183184
// Internal commands which are invoked by the server.
184185
applyActionGroup: { enabled: commands.applyActionGroup },
185186
applySnippetWorkspaceEdit: { enabled: commands.applySnippetWorkspaceEditCommand },

editors/code/src/util.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as vscode from "vscode";
22
import { strict as nativeAssert } from "assert";
33
import { exec, type ExecOptions, spawnSync } from "child_process";
44
import { inspect } from "util";
5+
import type { Env } from "./client";
56

67
export function assert(condition: boolean, explanation: string): asserts condition {
78
try {
@@ -93,10 +94,13 @@ export function isDocumentInWorkspace(document: RustDocument): boolean {
9394
return false;
9495
}
9596

96-
export function isValidExecutable(path: string): boolean {
97+
export function isValidExecutable(path: string, extraEnv: Env): boolean {
9798
log.debug("Checking availability of a binary at", path);
9899

99-
const res = spawnSync(path, ["--version"], { encoding: "utf8" });
100+
const res = spawnSync(path, ["--version"], {
101+
encoding: "utf8",
102+
env: { ...process.env, ...extraEnv },
103+
});
100104

101105
const printOutput = res.error ? log.warn : log.info;
102106
printOutput(path, "--version:", res);

0 commit comments

Comments
 (0)