Skip to content
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

VSCode: add status bar #6497

Merged
merged 15 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
6 changes: 6 additions & 0 deletions vscode-cairo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@
},
"default": {},
"markdownDescription": "Extra environment variables that will be passed to the `cairo-language-server` executable.\nUseful for passing e.g. `CAIRO_LS_LOG` for debugging."
},
"cairo1.showInStatusBar": {
"type": "boolean",
"default": true,
"description": "Show CairoLS information in the status bar.",
"scope": "window"
}
}
}
Expand Down
1 change: 1 addition & 0 deletions vscode-cairo/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as vscode from "vscode";

interface ConfigProps {
enableLanguageServer: boolean;
showInStatusBar: boolean;
languageServerPath: string;
enableScarb: boolean;
preferScarbLanguageServer: boolean;
Expand Down
6 changes: 5 additions & 1 deletion vscode-cairo/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ export class Context {
log: true,
}),
);
const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 100);

extensionContext.subscriptions.push(log);
extensionContext.subscriptions.push(statusBarItem);

return new Context(extensionContext, log);
return new Context(extensionContext, log, statusBarItem);
}

public readonly config: Config = new Config();

private constructor(
public readonly extension: vscode.ExtensionContext,
public readonly log: RootLogOutputChannel,
public readonly statusBarItem: vscode.StatusBarItem,
) {}
}
9 changes: 9 additions & 0 deletions vscode-cairo/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as vscode from "vscode";
import * as lc from "vscode-languageclient/node";
import { setupLanguageServer } from "./cairols";
import { Context } from "./context";
import { setupStatusBar } from "./statusBar";

let client: lc.LanguageClient | undefined;

Expand All @@ -14,11 +15,19 @@ export async function activate(extensionContext: vscode.ExtensionContext) {
ctx.log.warn("language server is disabled");
ctx.log.warn("note: set `cairo1.enableLanguageServer` to `true` to enable it");
}

if (ctx.config.get("showInStatusBar")) {
await setupStatusBar(ctx, client);
} else {
ctx.log.warn("status bar is disabled");
ctx.log.warn("note: set `cairo1.showInStatusBar` to `true` to enable it");
}
}

export function deactivate(): Thenable<void> | undefined {
if (!client) {
return undefined;
}

return client.stop();
}
5 changes: 5 additions & 0 deletions vscode-cairo/src/scarb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ export class Scarb implements LanguageServerExecutableProvider {
return this.hasCommand("cairo-language-server", ctx);
}

public async getVersion(ctx: Context): Promise<string> {
const output = await this.execWithOutput(["--version"], ctx);
return output.trim();
}

public async cacheClean(ctx: Context): Promise<void> {
await this.execWithOutput(["cache", "clean"], ctx);
}
Expand Down
53 changes: 53 additions & 0 deletions vscode-cairo/src/statusBar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import * as vscode from "vscode";
import { Context } from "./context";
import { Scarb } from "./scarb";
import * as lc from "vscode-languageclient/node";

const CAIRO_STATUS_BAR_COMMAND = "cairo1.statusBar.clicked";

export async function setupStatusBar(ctx: Context, client?: lc.LanguageClient) {
ctx.extension.subscriptions.push(
vscode.workspace.onDidChangeConfiguration((e) => {
if (e.affectsConfiguration("cairo1.showInStatusBar")) {
updateStatusBar(ctx);
}
}),
);

ctx.extension.subscriptions.push(
vscode.commands.registerCommand(CAIRO_STATUS_BAR_COMMAND, () => {
if (client) {
client.outputChannel.show();
} else {
vscode.window.showWarningMessage("Cairo Language Server is not active");
}
}),
);
ctx.statusBarItem.command = CAIRO_STATUS_BAR_COMMAND;

updateStatusBar(ctx);
}

async function updateStatusBar(ctx: Context) {
const config = vscode.workspace.getConfiguration("cairo1");
const showInStatusBar = config.get<boolean>("showInStatusBar", true);

if (showInStatusBar) {
ctx.statusBarItem.text = "Cairo";
ctx.statusBarItem.tooltip = "Cairo Language";

try {
const scarb = await Scarb.find(vscode.workspace.workspaceFolders?.[0], ctx);
if (scarb) {
const version = await scarb.getVersion(ctx);
ctx.statusBarItem.tooltip = `Cairo Language\n${version}`;
}
} catch (error) {
ctx.log.error(`Error getting Scarb version: ${error}`);
}

ctx.statusBarItem.show();
} else {
ctx.statusBarItem.hide();
}
}
Loading