Skip to content

Commit

Permalink
Add status bar button to toggle check on save state
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Aug 15, 2023
1 parent 33f9250 commit e76d20e
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 3 deletions.
5 changes: 5 additions & 0 deletions editors/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
7 changes: 7 additions & 0 deletions editors/code/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1407,3 +1407,10 @@ locate()
ctx.pushExtCleanup(document);
};
}

export function toggleCheckOnSave(ctx: Ctx): Cmd {
return async () => {
await ctx.config.toggleCheckOnSave();
ctx.refreshServerStatus();
};
}
33 changes: 33 additions & 0 deletions editors/code/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
18 changes: 16 additions & 2 deletions editors/code/src/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)",
);
Expand Down
1 change: 1 addition & 0 deletions editors/code/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
2 changes: 1 addition & 1 deletion editors/code/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e76d20e

Please sign in to comment.