Add status bar button to toggle check on save state

This commit is contained in:
Lukas Wirth 2023-08-12 08:25:51 +02:00
parent 33f9250d21
commit e76d20e072
6 changed files with 63 additions and 3 deletions

View File

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

View File

@ -1407,3 +1407,10 @@ locate()
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() {
return this.get<boolean>("trace.extension");
}

View File

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

View File

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

View File

@ -2,7 +2,7 @@ import * as vscode from "vscode";
import { strict as nativeAssert } from "assert";
import { exec, type ExecOptions, spawnSync } from "child_process";
import { inspect } from "util";
import { Env } from "./client";
import type { Env } from "./client";
export function assert(condition: boolean, explanation: string): asserts condition {
try {