Skip to content

Commit

Permalink
extension: only show the CUE status bar item when editing CUE
Browse files Browse the repository at this point in the history
In VSCode window there is only one status bar. The status bar shows,
amongst other things, the file type of the active tab. There is only one
active tab. Correspondingly, we should only show the CUE status bar when
the active tab is CUE.

Also borrow some logic from vscode-go regarding the priority of the
status bar item, and fix up the return type of Extension.updateStatus.

Signed-off-by: Paul Jolly <[email protected]>
Change-Id: Ifb44141c14aadcc3cf6ab2a7cf4b9a4efe993c8e
Reviewed-on: https://review.gerrithub.io/c/cue-lang/vscode-cue/+/1206562
Reviewed-by: Daniel Martí <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
  • Loading branch information
myitcv committed Jan 2, 2025
1 parent d217389 commit d8ec058
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ export class Extension {
// :zap: icon in case the LSP is running.
private statusBarItem: vscode.StatusBarItem;

// showStatusBarItem encapsulates the logic determining whether to show
// statusBarItem or not.
private showStatusBarItem: boolean = false;

constructor(
ctx: vscode.ExtensionContext,
output: vscode.LogOutputChannel,
Expand All @@ -116,7 +120,20 @@ export class Extension {
// Not visible to users via the command palette
this.registerCommand(copyStatusVersionToClipboardCmd, this.copyStatusVersionToClipboard);

this.statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right);
this.statusBarItem = vscode.window.createStatusBarItem(
'CUE',
vscode.StatusBarAlignment.Right,

// Borrowed from vscode-go, place the item right after the language
// status item
// https://github.com/microsoft/vscode-python/issues/18040#issuecomment-992567670.
100.09999
);

// Capture the current editor state, and
let activeTextEditorListener = vscode.window.onDidChangeActiveTextEditor(this.activeTextEditorChanged);
this.ctx.subscriptions.push(activeTextEditorListener);
this.updateStatusBarVisibilityFromEditor(vscode.window.activeTextEditor);

// TODO(myitcv): in the early days of 'cue lsp', it might be worthwhile
// adding a command that toggles the enabled-ness of the LSP in the active
Expand Down Expand Up @@ -333,7 +350,11 @@ export class Extension {

// updateStatus ensures that the status bar item reflects the current state
// of the extension.
updateStatus = (): Promise<void> => {
updateStatus = (): void => {
if (!this.showStatusBarItem) {
this.statusBarItem.hide();
return;
}
let version = this.cueVersion;
let tooltip = 'Click to copy version';
let command = this.commandID(copyStatusVersionToClipboardCmd);
Expand All @@ -350,7 +371,6 @@ export class Extension {
this.statusBarItem.tooltip = tooltip;
this.statusBarItem.command = command;
this.statusBarItem.show();
return Promise.resolve();
};

// copyStatusVersionToClipboard is the target of the
Expand Down Expand Up @@ -580,6 +600,23 @@ export class Extension {
}
return Promise.resolve(resolvedCommand!);
};

// activeTextEditorChanged handles a change in text editor (appears to
// include active tab) in VSCode. In VSCode window there is only one status
// bar. The status bar shows, amongst other things, the file type of the
// active tab. There is only one active tab. Correspondingly, we should only
// show the CUE status bar when the active tab is CUE.
activeTextEditorChanged = async (editor: vscode.TextEditor | undefined): Promise<void> => {
this.updateStatusBarVisibilityFromEditor(editor);
this.updateStatus();
};

// updateStatusBarVisibilityFromEditor determines whether the editor's active
// document is a CUE file or not, and updates the state of the status bar
// visibility accordingly.
updateStatusBarVisibilityFromEditor = (editor: vscode.TextEditor | undefined): void => {
this.showStatusBarItem = editor?.document.languageId.toLowerCase() === 'cue';
};
}

// CueConfiguration corresponds to the type of the configuration of the vscode-cue
Expand Down

0 comments on commit d8ec058

Please sign in to comment.