Skip to content

Commit

Permalink
VSCode: Move Scarb searching logic to the Scarb class
Browse files Browse the repository at this point in the history
commit-id:c0872618
  • Loading branch information
mkaput committed Feb 12, 2024
1 parent 00a55bd commit fc16ac0
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 31 deletions.
42 changes: 11 additions & 31 deletions vscode-cairo/src/cairols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ import { SemanticTokensFeature } from "vscode-languageclient/lib/common/semantic
import * as lc from "vscode-languageclient/node";
import { Context } from "./context";
import { Scarb } from "./scarb";
import {
checkTool,
findToolAtWithExtension,
findToolInAsdf,
findToolInPath,
} from "./toolchain";
import { checkTool, findToolAtWithExtension } from "./toolchain";

/**
* Tries to find the development version of the language server executable,
Expand Down Expand Up @@ -62,22 +57,6 @@ async function findLanguageServerExecutable(ctx: Context) {
return findDevLanguageServerAt(root);
}

async function findScarbExecutablePath(
ctx: Context,
): Promise<string | undefined> {
// Check config for scarb path.
const configPath = ctx.config.get("scarbPath");
if (configPath) {
return await checkTool(configPath);
}

// Check PATH env var for scarb path.
const envPath = await findToolInPath("scarb");
if (envPath) return envPath;

return findToolInAsdf("scarb");
}

function notifyScarbMissing(ctx: Context) {
const errorMessage =
"This is a Scarb project, but could not find Scarb executable on this machine. " +
Expand Down Expand Up @@ -204,22 +183,23 @@ export async function setupLanguageServer(
}

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

const isScarbEnabled = ctx.config.get("enableScarb", false);
const scarbPath = await findScarbExecutablePath(ctx);
const configLanguageServerPath = ctx.config.get("languageServerPath");

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");
} else if (scarbPath == undefined) {
ctx.log.error("could not find Scarb executable on this machine");
} else {
ctx.log.debug(`using Scarb: ${scarbPath}`);
}

let scarb: Scarb | undefined;
if (isScarbEnabled && scarbPath != undefined) {
scarb = new Scarb(scarbPath, vscode.workspace.workspaceFolders?.[0]);
try {
scarb = await Scarb.find(workspaceFolder, ctx);
} catch (e) {
ctx.log.error(`${e}`);
ctx.log.error("note: Scarb integration is disabled due to this error");
}
}

const serverType = await getServerType(
Expand Down
46 changes: 46 additions & 0 deletions vscode-cairo/src/scarb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { spawn } from "child_process";
import * as vscode from "vscode";
import * as lc from "vscode-languageclient/node";
import type { Context } from "./context";
import { checkTool, findToolInAsdf, findToolInPath } from "./toolchain";

let globalExecId = 0;

Expand All @@ -18,6 +19,51 @@ export class Scarb {
public readonly workspaceFolder?: vscode.WorkspaceFolder | undefined,
) {}

/**
* Tries to find Scarb on the host in context of the given workspace folder.
*/
public static async find(
workspaceFolder: vscode.WorkspaceFolder | undefined,
ctx: Context,
): Promise<Scarb> {
const path = await findPath();
return new Scarb(path, workspaceFolder);

async function findPath(): Promise<string> {
// TODO(mkaput): Config should probably be scoped to workspace folder.
// Check config for scarb path.
let configPath = ctx.config.get("scarbPath");
if (configPath) {
configPath = await checkTool(configPath);
if (configPath) {
ctx.log.debug(`using Scarb from config: ${configPath}`);
return configPath;
} else {
throw new Error(
`configured Scarb path does not exist: ${configPath}`,
);
}
}

// Check PATH env var for scarb path.
const envPath = await findToolInPath("scarb");
if (envPath) {
ctx.log.debug(`using Scarb from PATH: ${envPath}`);
return envPath;
}

// Check if scarb is installed via asdf. Usually it's already in PATH,
// but some users happened not to have it there.
const asdfPath = await findToolInAsdf("scarb");
if (asdfPath) {
ctx.log.debug(`using Scarb from asdf: ${asdfPath}`);
return asdfPath;
}

throw new Error("could not find Scarb executable on this machine");
}
}

public languageServerExecutable(): lc.Executable {
const exec: lc.Executable = {
command: this.path,
Expand Down

0 comments on commit fc16ac0

Please sign in to comment.