Skip to content

Commit f4c968a

Browse files
authored
Merge pull request rust-lang#18592 from darichey/status-bar-visibility
vscode: Only show status bar item in relevant files
2 parents f555fc4 + 97feb03 commit f4c968a

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

src/tools/rust-analyzer/editors/code/package.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,41 @@
425425
],
426426
"default": "openLogs",
427427
"markdownDescription": "Action to run when clicking the extension status bar item."
428+
},
429+
"rust-analyzer.statusBar.documentSelector": {
430+
"type": [
431+
"array",
432+
"null"
433+
],
434+
"items": {
435+
"type": "object",
436+
"properties": {
437+
"language": {
438+
"type": [
439+
"string",
440+
"null"
441+
]
442+
},
443+
"pattern": {
444+
"type": [
445+
"string",
446+
"null"
447+
]
448+
}
449+
}
450+
},
451+
"default": [
452+
{
453+
"language": "rust"
454+
},
455+
{
456+
"pattern": "**/Cargo.toml"
457+
},
458+
{
459+
"pattern": "**/Cargo.lock"
460+
}
461+
],
462+
"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."
428463
}
429464
}
430465
},

src/tools/rust-analyzer/editors/code/src/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,10 @@ export class Config {
348348
return this.get<string>("statusBar.clickAction");
349349
}
350350

351+
get statusBarDocumentSelector() {
352+
return this.get<vscode.DocumentSelector>("statusBar.documentSelector");
353+
}
354+
351355
get initializeStopped() {
352356
return this.get<boolean>("initializeStopped");
353357
}

src/tools/rust-analyzer/editors/code/src/ctx.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export class Ctx implements RustAnalyzerExtensionApi {
8888
private _treeView: vscode.TreeView<Dependency | DependencyFile | DependencyId> | undefined;
8989
private lastStatus: ServerStatusParams | { health: "stopped" } = { health: "stopped" };
9090
private _serverVersion: string;
91+
private statusBarActiveEditorListener: Disposable;
9192

9293
get serverPath(): string | undefined {
9394
return this._serverPath;
@@ -119,6 +120,10 @@ export class Ctx implements RustAnalyzerExtensionApi {
119120
this._serverVersion = "<not running>";
120121
this.config = new Config(extCtx.subscriptions);
121122
this.statusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
123+
this.updateStatusBarVisibility(vscode.window.activeTextEditor);
124+
this.statusBarActiveEditorListener = vscode.window.onDidChangeActiveTextEditor((editor) =>
125+
this.updateStatusBarVisibility(editor),
126+
);
122127
if (this.config.testExplorer) {
123128
this.testController = vscode.tests.createTestController(
124129
"rustAnalyzerTestController",
@@ -141,6 +146,7 @@ export class Ctx implements RustAnalyzerExtensionApi {
141146
dispose() {
142147
this.config.dispose();
143148
this.statusBar.dispose();
149+
this.statusBarActiveEditorListener.dispose();
144150
this.testController?.dispose();
145151
void this.disposeClient();
146152
this.commandDisposables.forEach((disposable) => disposable.dispose());
@@ -404,7 +410,6 @@ export class Ctx implements RustAnalyzerExtensionApi {
404410
let icon = "";
405411
const status = this.lastStatus;
406412
const statusBar = this.statusBar;
407-
statusBar.show();
408413
statusBar.tooltip = new vscode.MarkdownString("", true);
409414
statusBar.tooltip.isTrusted = true;
410415
switch (status.health) {
@@ -472,6 +477,17 @@ export class Ctx implements RustAnalyzerExtensionApi {
472477
statusBar.text = `${icon}rust-analyzer`;
473478
}
474479

480+
private updateStatusBarVisibility(editor: vscode.TextEditor | undefined) {
481+
const documentSelector = this.config.statusBarDocumentSelector;
482+
if (documentSelector != null) {
483+
if (editor != null && vscode.languages.match(documentSelector, editor.document) > 0) {
484+
this.statusBar.show();
485+
return;
486+
}
487+
}
488+
this.statusBar.hide();
489+
}
490+
475491
pushExtCleanup(d: Disposable) {
476492
this.extCtx.subscriptions.push(d);
477493
}

0 commit comments

Comments
 (0)