Skip to content

Commit

Permalink
feat!: Replace grainfind with cliFlags setting (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Jun 3, 2022
1 parent 77d6691 commit 68c1816
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 29 deletions.
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

0 comments on commit 68c1816

Please sign in to comment.