Auto merge of #15446 - Veykril:checkOnSaveToggle, r=Veykril

Add status bar button to toggle check on save state

Closes https://github.com/rust-lang/rust-analyzer/issues/15440
cc https://github.com/rust-lang/rust-analyzer/issues/13208
This commit is contained in:
bors 2023-08-15 11:38:44 +00:00
commit ef2ee59631
7 changed files with 69 additions and 5 deletions

View File

@ -292,6 +292,11 @@
"command": "rust-analyzer.viewMemoryLayout", "command": "rust-analyzer.viewMemoryLayout",
"title": "View Memory Layout", "title": "View Memory Layout",
"category": "rust-analyzer" "category": "rust-analyzer"
},
{
"command": "rust-analyzer.toggleCheckOnSave",
"title": "Toggle Check on Save",
"category": "rust-analyzer"
} }
], ],
"keybindings": [ "keybindings": [

View File

@ -20,7 +20,7 @@ export async function bootstrap(
log.info("Using server binary at", path); log.info("Using server binary at", path);
if (!isValidExecutable(path)) { if (!isValidExecutable(path, config.serverExtraEnv)) {
if (config.serverPath) { if (config.serverPath) {
throw new Error(`Failed to execute ${path} --version. \`config.server.path\` or \`config.serverPath\` has been set explicitly.\ throw new Error(`Failed to execute ${path} --version. \`config.server.path\` or \`config.serverPath\` has been set explicitly.\
Consider removing this config or making a valid server binary available at that path.`); Consider removing this config or making a valid server binary available at that path.`);

View File

@ -1407,3 +1407,10 @@ locate()
ctx.pushExtCleanup(document); ctx.pushExtCleanup(document);
}; };
} }
export function toggleCheckOnSave(ctx: Ctx): Cmd {
return async () => {
await ctx.config.toggleCheckOnSave();
ctx.refreshServerStatus();
};
}

View File

@ -216,6 +216,39 @@ export class Config {
), ),
); );
} }
get checkOnSave() {
return this.get<boolean>("checkOnSave") ?? false;
}
async toggleCheckOnSave() {
const config = this.cfg.inspect<boolean>("checkOnSave") ?? { key: "checkOnSave" };
let overrideInLanguage;
let target;
let value;
if (
config.workspaceFolderValue !== undefined ||
config.workspaceFolderLanguageValue !== undefined
) {
target = vscode.ConfigurationTarget.WorkspaceFolder;
overrideInLanguage = config.workspaceFolderLanguageValue;
value = config.workspaceFolderValue || config.workspaceFolderLanguageValue;
} else if (
config.workspaceValue !== undefined ||
config.workspaceLanguageValue !== undefined
) {
target = vscode.ConfigurationTarget.Workspace;
overrideInLanguage = config.workspaceLanguageValue;
value = config.workspaceValue || config.workspaceLanguageValue;
} else if (config.globalValue !== undefined || config.globalLanguageValue !== undefined) {
target = vscode.ConfigurationTarget.Global;
overrideInLanguage = config.globalLanguageValue;
value = config.globalValue || config.globalLanguageValue;
} else if (config.defaultValue !== undefined || config.defaultLanguageValue !== undefined) {
overrideInLanguage = config.defaultLanguageValue;
value = config.defaultValue || config.defaultLanguageValue;
}
await this.cfg.update("checkOnSave", !(value || false), target || null, overrideInLanguage);
}
get traceExtension() { get traceExtension() {
return this.get<boolean>("trace.extension"); return this.get<boolean>("trace.extension");
} }

View File

@ -94,6 +94,7 @@ export class Ctx {
private unlinkedFiles: vscode.Uri[]; private unlinkedFiles: vscode.Uri[];
private _dependencies: RustDependenciesProvider | undefined; private _dependencies: RustDependenciesProvider | undefined;
private _treeView: vscode.TreeView<Dependency | DependencyFile | DependencyId> | undefined; private _treeView: vscode.TreeView<Dependency | DependencyFile | DependencyId> | undefined;
private lastStatus: ServerStatusParams | { health: "stopped" } = { health: "stopped" };
get client() { get client() {
return this._client; return this._client;
@ -404,7 +405,15 @@ export class Ctx {
} }
setServerStatus(status: ServerStatusParams | { health: "stopped" }) { setServerStatus(status: ServerStatusParams | { health: "stopped" }) {
this.lastStatus = status;
this.updateStatusBarItem();
}
refreshServerStatus() {
this.updateStatusBarItem();
}
private updateStatusBarItem() {
let icon = ""; let icon = "";
const status = this.lastStatus;
const statusBar = this.statusBar; const statusBar = this.statusBar;
statusBar.show(); statusBar.show();
statusBar.tooltip = new vscode.MarkdownString("", true); statusBar.tooltip = new vscode.MarkdownString("", true);
@ -447,13 +456,18 @@ export class Ctx {
"statusBarItem.warningBackground", "statusBarItem.warningBackground",
); );
statusBar.command = "rust-analyzer.startServer"; statusBar.command = "rust-analyzer.startServer";
statusBar.text = `$(stop-circle) rust-analyzer`; statusBar.text = "$(stop-circle) rust-analyzer";
return; return;
} }
if (statusBar.tooltip.value) { if (statusBar.tooltip.value) {
statusBar.tooltip.appendMarkdown("\n\n---\n\n"); statusBar.tooltip.appendMarkdown("\n\n---\n\n");
} }
statusBar.tooltip.appendMarkdown("\n\n[Open logs](command:rust-analyzer.openLogs)"); statusBar.tooltip.appendMarkdown("\n\n[Open Logs](command:rust-analyzer.openLogs)");
statusBar.tooltip.appendMarkdown(
`\n\n[${
this.config.checkOnSave ? "Disable" : "Enable"
} Check on Save](command:rust-analyzer.toggleCheckOnSave)`,
);
statusBar.tooltip.appendMarkdown( statusBar.tooltip.appendMarkdown(
"\n\n[Reload Workspace](command:rust-analyzer.reloadWorkspace)", "\n\n[Reload Workspace](command:rust-analyzer.reloadWorkspace)",
); );

View File

@ -180,6 +180,7 @@ function createCommands(): Record<string, CommandFactory> {
ssr: { enabled: commands.ssr }, ssr: { enabled: commands.ssr },
serverVersion: { enabled: commands.serverVersion }, serverVersion: { enabled: commands.serverVersion },
viewMemoryLayout: { enabled: commands.viewMemoryLayout }, viewMemoryLayout: { enabled: commands.viewMemoryLayout },
toggleCheckOnSave: { enabled: commands.toggleCheckOnSave },
// Internal commands which are invoked by the server. // Internal commands which are invoked by the server.
applyActionGroup: { enabled: commands.applyActionGroup }, applyActionGroup: { enabled: commands.applyActionGroup },
applySnippetWorkspaceEdit: { enabled: commands.applySnippetWorkspaceEditCommand }, applySnippetWorkspaceEdit: { enabled: commands.applySnippetWorkspaceEditCommand },

View File

@ -2,6 +2,7 @@ import * as vscode from "vscode";
import { strict as nativeAssert } from "assert"; import { strict as nativeAssert } from "assert";
import { exec, type ExecOptions, spawnSync } from "child_process"; import { exec, type ExecOptions, spawnSync } from "child_process";
import { inspect } from "util"; import { inspect } from "util";
import type { Env } from "./client";
export function assert(condition: boolean, explanation: string): asserts condition { export function assert(condition: boolean, explanation: string): asserts condition {
try { try {
@ -93,10 +94,13 @@ export function isDocumentInWorkspace(document: RustDocument): boolean {
return false; return false;
} }
export function isValidExecutable(path: string): boolean { export function isValidExecutable(path: string, extraEnv: Env): boolean {
log.debug("Checking availability of a binary at", path); log.debug("Checking availability of a binary at", path);
const res = spawnSync(path, ["--version"], { encoding: "utf8" }); const res = spawnSync(path, ["--version"], {
encoding: "utf8",
env: { ...process.env, ...extraEnv },
});
const printOutput = res.error ? log.warn : log.info; const printOutput = res.error ? log.warn : log.info;
printOutput(path, "--version:", res); printOutput(path, "--version:", res);