Only show status bar item in relevant files

This commit is contained in:
David Richey 2024-12-03 14:14:53 +00:00
parent c4e040ea8d
commit cf80dddb59
3 changed files with 56 additions and 1 deletions

View file

@ -425,6 +425,41 @@
],
"default": "openLogs",
"markdownDescription": "Action to run when clicking the extension status bar item."
},
"rust-analyzer.statusBar.documentSelector": {
"type": [
"array",
"null"
],
"items": {
"type": "object",
"properties": {
"language": {
"type": [
"string",
"null"
]
},
"pattern": {
"type": [
"string",
"null"
]
}
}
},
"default": [
{
"language": "rust"
},
{
"pattern": "**/Cargo.toml"
},
{
"pattern": "**/Cargo.lock"
}
],
"markdownDescription": "Determines when to show the extension status bar item based on the currently open file. Use `{ \"pattern\": \"**\" }` to always show. Use `null` to never show."
}
}
},

View file

@ -348,6 +348,10 @@ export class Config {
return this.get<string>("statusBar.clickAction");
}
get statusBarDocumentSelector() {
return this.get<vscode.DocumentSelector>("statusBar.documentSelector");
}
get initializeStopped() {
return this.get<boolean>("initializeStopped");
}

View file

@ -88,6 +88,7 @@ export class Ctx implements RustAnalyzerExtensionApi {
private _treeView: vscode.TreeView<Dependency | DependencyFile | DependencyId> | undefined;
private lastStatus: ServerStatusParams | { health: "stopped" } = { health: "stopped" };
private _serverVersion: string;
private statusBarActiveEditorListener: Disposable;
get serverPath(): string | undefined {
return this._serverPath;
@ -119,6 +120,10 @@ export class Ctx implements RustAnalyzerExtensionApi {
this._serverVersion = "<not running>";
this.config = new Config(extCtx.subscriptions);
this.statusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
this.updateStatusBarVisibility(vscode.window.activeTextEditor);
this.statusBarActiveEditorListener = vscode.window.onDidChangeActiveTextEditor((editor) =>
this.updateStatusBarVisibility(editor),
);
if (this.config.testExplorer) {
this.testController = vscode.tests.createTestController(
"rustAnalyzerTestController",
@ -141,6 +146,7 @@ export class Ctx implements RustAnalyzerExtensionApi {
dispose() {
this.config.dispose();
this.statusBar.dispose();
this.statusBarActiveEditorListener.dispose();
this.testController?.dispose();
void this.disposeClient();
this.commandDisposables.forEach((disposable) => disposable.dispose());
@ -404,7 +410,6 @@ export class Ctx implements RustAnalyzerExtensionApi {
let icon = "";
const status = this.lastStatus;
const statusBar = this.statusBar;
statusBar.show();
statusBar.tooltip = new vscode.MarkdownString("", true);
statusBar.tooltip.isTrusted = true;
switch (status.health) {
@ -472,6 +477,17 @@ export class Ctx implements RustAnalyzerExtensionApi {
statusBar.text = `${icon}rust-analyzer`;
}
private updateStatusBarVisibility(editor: vscode.TextEditor | undefined) {
const documentSelector = this.config.statusBarDocumentSelector;
if (documentSelector != null) {
if (editor != null && vscode.languages.match(documentSelector, editor.document) > 0) {
this.statusBar.show();
return;
}
}
this.statusBar.hide();
}
pushExtCleanup(d: Disposable) {
this.extCtx.subscriptions.push(d);
}