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!: Replace grainfind with cliFlags setting #113

Merged
merged 2 commits into from
Jun 3, 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
5 changes: 5 additions & 0 deletions editor-extensions/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@
"type": "object",
"title": "Grain Language Server configuration",
"properties": {
"grain.cliFlags": {
"scope": "resource",
"type": "string",
"description": "Space-separated list of flags to pass to the grain CLI"
},
"grain.cliPath": {
"scope": "resource",
"type": "string",
Expand Down
49 changes: 20 additions & 29 deletions editor-extensions/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import {
ServerOptions,
} from "vscode-languageclient/node";

import * as fs from "fs";

import which from "which";

import { GrainDocCompletionProvider } from "./GrainDocCompletionProvider";
Expand All @@ -36,8 +34,6 @@ let languageId = "grain";

let outputChannel = window.createOutputChannel(extensionName, languageId);

let isWindows = /^win32/.test(process.platform);

let fileClients: Map<string, LanguageClient> = new Map();
let workspaceClients: Map<string, LanguageClient> = new Map();

Expand Down Expand Up @@ -69,19 +65,6 @@ function dirpathFromUri(uri: Uri): string {
return dirPath;
}

function filepathFromUri(uri: Uri) {
let filename = uri.fsPath;

// Packaged Grain doesn't understand lowercase drive letters.
// If authority is not empty, then we can skip since this is
// a UNC path.
if (isWindows && uri.authority === "") {
filename = filename[0].toUpperCase() + filename.substring(1);
}

return filename;
}

function globFromUri(uri: Uri, glob: string) {
// globs always need to use `/`
return `${uri.fsPath}${glob}`.replaceAll("\\", "/");
Expand Down Expand Up @@ -115,26 +98,20 @@ function getOuterMostWorkspaceFolder(folder: WorkspaceFolder): WorkspaceFolder {
function getLspCommand(uri: Uri) {
let config = workspace.getConfiguration("grain", uri);

let lspEnabled: boolean = config.get("enableLSP");
let lspEnabled = config.get<boolean>("enableLSP");

if (!lspEnabled) {
return;
}

let command: string = config.get("cliPath") || findGrain();
let command = config.get<string | undefined>("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");

let args = ["lsp"];
let flags = config.get<string | undefined>("cliFlags") || "";

let buildScriptUri = Uri.joinPath(uri, "script/grainfind.js");
let buildScriptPath = filepathFromUri(buildScriptUri);

if (fs.existsSync(buildScriptPath)) {
command = "node";
args = [buildScriptPath, ...args];
}
let args = ["lsp", ...flags.split(" ")];

return [command, args] as const;
}
Expand Down Expand Up @@ -165,7 +142,9 @@ async function startFileClient(uri: Uri) {
clientOptions
);

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

await client.start();

Expand Down Expand Up @@ -225,7 +204,9 @@ async function startWorkspaceClient(workspaceFolder: WorkspaceFolder) {
clientOptions
);

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

await client.start();

Expand Down Expand Up @@ -284,6 +265,11 @@ async function didOpenTextDocument(
await addWorkspaceClient(folder);

configHandler = async (e) => {
if (e.affectsConfiguration("grain.cliFlags", folder.uri)) {
await removeWorkspaceClient(folder);
await addWorkspaceClient(folder);
}

if (e.affectsConfiguration("grain.cliPath", folder.uri)) {
await removeWorkspaceClient(folder);
await addWorkspaceClient(folder);
Expand All @@ -299,6 +285,11 @@ async function didOpenTextDocument(
await addFileClient(uri);

configHandler = async (e) => {
if (e.affectsConfiguration("grain.cliFlags", uri)) {
await removeFileClient(uri);
await addFileClient(uri);
}

if (e.affectsConfiguration("grain.cliPath", uri)) {
await removeFileClient(uri);
await addFileClient(uri);
Expand Down