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

feat: Search for a variety of grain CLIs in PATH #111

Merged
merged 1 commit into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 17 additions & 7 deletions editor-extensions/vscode/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions editor-extensions/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@
"grain.cliPath": {
"scope": "resource",
"type": "string",
"default": "grain",
"description": "Path to the grain cli"
"description": "Absolute path to the grain CLI (detected in PATH if not specified)"
},
"grain.enableLSP": {
"scope": "resource",
Expand Down Expand Up @@ -114,12 +113,14 @@
"deploy": "vsce publish && ovsx publish"
},
"dependencies": {
"vscode-languageclient": "^8.0.1"
"vscode-languageclient": "^8.0.1",
"which": "^2.0.2"
},
"devDependencies": {
"@types/mocha": "^9.1.1",
"@types/node": "^12.12.0",
"@types/vscode": "1.67.0",
"@types/which": "^2.0.1",
"@typescript-eslint/parser": "^5.25.0",
"@vscode/test-electron": "^2.1.3",
"esbuild": "^0.14.42",
Expand Down
45 changes: 30 additions & 15 deletions editor-extensions/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import {

import * as fs from "fs";

import which from "which";

import { GrainDocCompletionProvider } from "./GrainDocCompletionProvider";

let extensionName = "Grain Language Server";
Expand All @@ -39,6 +41,26 @@ let isWindows = /^win32/.test(process.platform);
let fileClients: Map<string, LanguageClient> = new Map();
let workspaceClients: Map<string, LanguageClient> = new Map();

const grainBinaries = [
"grain",
"grain-mac-x64",
"grain-linux-x64",
"grain-win-x64",
];

function findGrain() {
for (const bin of grainBinaries) {
try {
const grain = which.sync(bin);
// If it didn't throw, we found a grain binary
return grain;
} catch (err) {
// Not found
}
}
throw new Error("Unable to locate any Grain binary. Did you install it?");
}

function dirpathFromUri(uri: Uri): string {
let dirPath = uri.toString();
if (!dirPath.endsWith("/")) {
Expand Down Expand Up @@ -99,22 +121,11 @@ function getLspCommand(uri: Uri) {
return;
}

let executablePath: string = config.get("cliPath") || "grain";
let command: string = config.get("cliPath") || findGrain();
// For some reason, if you specify a capitalized EXE extension for our pkg binary,
// it crashes the LSP so we just lowercase any .EXE ending in the command
command = command.replace(/\.EXE$/, ".exe");

// Not sure if this can technically change between VSCode restarts. Even if it does,
// it is likely to be swapped with PowerShell, which understands the `.cmd` executables.
let needsCMD =
isWindows && process.env.ComSpec && /cmd.exe$/.test(process.env.ComSpec);

if (
needsCMD &&
!executablePath.endsWith(".cmd") &&
!executablePath.endsWith(".exe")
) {
executablePath += ".cmd";
}

let command = executablePath;
let args = ["lsp"];

let buildScriptUri = Uri.joinPath(uri, "script/grainfind.js");
Expand Down Expand Up @@ -154,6 +165,8 @@ async function startFileClient(uri: Uri) {
clientOptions
);

client.info(`Starting LSP client using executable: ${command}`);

await client.start();

let grainDocCompletionProvider = new GrainDocCompletionProvider(client);
Expand Down Expand Up @@ -212,6 +225,8 @@ async function startWorkspaceClient(workspaceFolder: WorkspaceFolder) {
clientOptions
);

client.info(`Starting LSP client using executable: ${command}`);

await client.start();

let grainDocCompletionProvider = new GrainDocCompletionProvider(client);
Expand Down
3 changes: 2 additions & 1 deletion editor-extensions/vscode/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"lib": ["ES2022"],
"outDir": "out",
"rootDir": "src",
"sourceMap": true
"sourceMap": true,
"esModuleInterop": true
},
"include": ["src/*"],
"exclude": ["node_modules"]
Expand Down