Skip to content

Add status bar button to toggle check on save state #15446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion editors/code/src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function bootstrap(

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

if (!isValidExecutable(path)) {
if (!isValidExecutable(path, config.serverExtraEnv)) {
if (config.serverPath) {
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.`);
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
8 changes: 6 additions & 2 deletions editors/code/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +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 type { Env } from "./client";

export function assert(condition: boolean, explanation: string): asserts condition {
try {
Expand Down Expand Up @@ -93,10 +94,13 @@ export function isDocumentInWorkspace(document: RustDocument): boolean {
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);

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;
printOutput(path, "--version:", res);
Expand Down