Skip to content

Commit

Permalink
VSCode: Minor improvements in LS setup logic (#6253)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkaput committed Aug 26, 2024
1 parent 5ef3a6c commit a39bf32
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
56 changes: 31 additions & 25 deletions vscode-cairo/src/cairols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { Scarb } from "./scarb";
import { isScarbProject } from "./scarbProject";
import { StandaloneLS } from "./standalonels";
import { registerMacroExpandProvider, registerVfsProvider } from "./textDocumentProviders";
import { NotificationType } from "vscode-jsonrpc/lib/common/messages";
import * as cp from "node:child_process";

export interface LanguageServerExecutableProvider {
languageServerExecutable(): lc.Executable;
Expand All @@ -24,7 +22,12 @@ function notifyScarbMissing(ctx: Context) {
}

export async function setupLanguageServer(ctx: Context): Promise<lc.LanguageClient> {
const serverOptions = await getServerOptions(ctx);
// TODO(mkaput): Support multi-root workspaces.
const workspaceFolder = vscode.workspace.workspaceFolders?.[0];

const scarb = await findScarbForWorkspaceFolder(workspaceFolder, ctx);

const serverOptions = await getServerOptions(workspaceFolder, scarb, ctx);

const clientOptions: lc.LanguageClientOptions = {
documentSelector: [
Expand All @@ -40,9 +43,9 @@ export async function setupLanguageServer(ctx: Context): Promise<lc.LanguageClie
clientOptions,
);

// Notify the server when client configuration changes.
// Notify the server when the client configuration changes.
// CairoLS pulls configuration properties it is interested in by itself, so it
// is not needed to attach any details in the notification payload.
// is unnecessary to attach any details in the notification payload.
const weakClient = new WeakRef(client);

ctx.extension.subscriptions.push(
Expand Down Expand Up @@ -85,29 +88,25 @@ export async function setupLanguageServer(ctx: Context): Promise<lc.LanguageClie
});

client.onNotification(
new NotificationType<string>("cairo/corelib-version-mismatch"),
new lc.NotificationType<string>("cairo/corelib-version-mismatch"),
async (errorMessage) => {
ctx.log.error(errorMessage);

const reloadWindow = "Reload window";
const cleanScarbCache = "Clean Scarb's cache";
const cleanScarbCache = "Clean Scarb cache and reload";

const selectedValue = await vscode.window.showErrorMessage(
errorMessage,
reloadWindow,
cleanScarbCache,
);

if (selectedValue === reloadWindow) {
await vscode.commands.executeCommand("workbench.action.reloadWindow");
} else if (selectedValue === cleanScarbCache) {
cp.exec("scarb cache clean", (err, stdout, stderr) => {
ctx.log.trace("`scarb cache clean` stdout: " + stdout);
ctx.log.trace("`scarb cache clean` stderr: " + stderr);
if (err) {
ctx.log.warn("error: " + err);
}
});
switch (selectedValue) {
case reloadWindow:
await vscode.commands.executeCommand("workbench.action.reloadWindow");
break;
case cleanScarbCache:
await scarb?.cacheClean(ctx);
await vscode.commands.executeCommand("workbench.action.reloadWindow");
break;
}
},
);
Expand All @@ -117,24 +116,31 @@ export async function setupLanguageServer(ctx: Context): Promise<lc.LanguageClie
return client;
}

async function getServerOptions(ctx: Context): Promise<lc.ServerOptions> {
// TODO(mkaput): Support multi-root workspaces.
const workspaceFolder = vscode.workspace.workspaceFolders?.[0];

async function findScarbForWorkspaceFolder(
workspaceFolder: vscode.WorkspaceFolder | undefined,
ctx: Context,
): Promise<Scarb | undefined> {
const isScarbEnabled = ctx.config.get("enableScarb", false);
let scarb: Scarb | undefined;
if (!isScarbEnabled) {
ctx.log.warn("Scarb integration is disabled");
ctx.log.warn("note: set `cairo1.enableScarb` to `true` to enable it");
return undefined;
} else {
try {
scarb = await Scarb.find(workspaceFolder, ctx);
return await Scarb.find(workspaceFolder, ctx);
} catch (e) {
ctx.log.error(`${e}`);
ctx.log.error("note: Scarb integration is disabled due to this error");
return undefined;
}
}
}

async function getServerOptions(
workspaceFolder: vscode.WorkspaceFolder | undefined,
scarb: Scarb | undefined,
ctx: Context,
): Promise<lc.ServerOptions> {
let serverExecutableProvider: LanguageServerExecutableProvider | undefined;
try {
serverExecutableProvider = await determineLanguageServerExecutableProvider(
Expand Down
4 changes: 4 additions & 0 deletions vscode-cairo/src/scarb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ export class Scarb implements LanguageServerExecutableProvider {
return this.hasCommand("cairo-language-server", ctx);
}

public async cacheClean(ctx: Context): Promise<void> {
await this.execWithOutput(["cache", "clean"], ctx);
}

private async hasCommand(command: string, ctx: Context): Promise<boolean> {
const output = await this.execWithOutput(["--json", "commands"], ctx);

Expand Down

0 comments on commit a39bf32

Please sign in to comment.