Skip to content

Commit 4b655e8

Browse files
committed
The path option was added to the rescript.settings
1 parent 4f8d3f9 commit 4b655e8

File tree

4 files changed

+67
-16
lines changed

4 files changed

+67
-16
lines changed

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@
131131
"type": "boolean",
132132
"default": false,
133133
"description": "Automatically start ReScript's code analysis."
134+
},
135+
"rescript.settings.path": {
136+
"type": "string",
137+
"default": "",
138+
"description": "Path to the ReScript binary. You can use it if you haven't or don't want to use the installed ReScript binary from node_modules in your project."
134139
}
135140
}
136141
},

server/src/constants.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@ export let analysisProdPath = path.join(
3232
"rescript-editor-analysis.exe"
3333
);
3434

35+
export const rescriptBinName = "rescript";
36+
37+
export const bsbBinName = "bsb";
38+
3539
// can't use the native bsb/rescript since we might need the watcher -w flag, which is only in the JS wrapper
3640
export let rescriptNodePartialPath = path.join(
3741
"node_modules",
3842
".bin",
39-
"rescript"
43+
rescriptBinName,
4044
);
41-
export let bsbNodePartialPath = path.join("node_modules", ".bin", "bsb");
45+
export let bsbNodePartialPath = path.join("node_modules", ".bin", bsbBinName);
4246

4347
export let bsbLock = ".bsb.lock";
4448
export let bsconfigPartialPath = "bsconfig.json";

server/src/server.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as v from "vscode-languageserver";
44
import * as rpc from "vscode-jsonrpc/node";
55
import * as path from "path";
66
import fs from "fs";
7+
import os from "os";
78
// TODO: check DidChangeWatchedFilesNotification.
89
import {
910
DidOpenTextDocumentNotification,
@@ -23,9 +24,11 @@ import { WorkspaceEdit } from "vscode-languageserver";
2324

2425
interface extensionConfiguration {
2526
askToStartBuild: boolean;
27+
path: string;
2628
}
2729
let extensionConfiguration: extensionConfiguration = {
2830
askToStartBuild: true,
31+
path: "",
2932
};
3033
let pullConfigurationPeriodically: NodeJS.Timeout | null = null;
3134

@@ -209,7 +212,7 @@ let openedFile = (fileUri: string, fileContent: string) => {
209212
// TODO: sometime stale .bsb.lock dangling. bsb -w knows .bsb.lock is
210213
// stale. Use that logic
211214
// TODO: close watcher when lang-server shuts down
212-
if (utils.findNodeBuildOfProjectRoot(projectRootPath) != null) {
215+
if (utils.findNodeBuildOfProjectRoot(projectRootPath, extensionConfiguration.path) != null) {
213216
let payload: clientSentBuildAction = {
214217
title: c.startBuildAction,
215218
projectRootPath: projectRootPath,
@@ -905,6 +908,32 @@ function onMessage(msg: p.Message) {
905908

906909
if (initialConfiguration != null) {
907910
extensionConfiguration = initialConfiguration;
911+
if (extensionConfiguration.path !== "" &&
912+
!path.isAbsolute(extensionConfiguration.path)
913+
) {
914+
// What should happen if the path is relative?
915+
// There is an option to create an absolute path using the params from msg,
916+
// but maybe it isn't the best option.
917+
918+
if (extensionConfiguration.path[0] === "~") {
919+
extensionConfiguration.path = path.join(os.homedir(), extensionConfiguration.path.slice(1))
920+
} else {
921+
let rootFromMgs = null
922+
// @ts-ignore
923+
if (msg.params != null && msg.params.rootUri != null) {
924+
// @ts-ignore
925+
rootFromMgs = fileURLToPath(msg.params.rootUri);
926+
}
927+
// @ts-ignore
928+
if (rootFromMgs == null && msg.params != null && msg.params.rootPath != null) {
929+
// @ts-ignore
930+
rootFromMgs = msg.params.rootPath;
931+
}
932+
extensionConfiguration.path = rootFromMgs == null
933+
? ""
934+
: path.join(rootFromMgs, extensionConfiguration.path);
935+
}
936+
}
908937
}
909938

910939
send(response);
@@ -1013,7 +1042,7 @@ function onMessage(msg: p.Message) {
10131042
// TODO: close watcher when lang-server shuts down. However, by Node's
10141043
// default, these subprocesses are automatically killed when this
10151044
// language-server process exits
1016-
let found = utils.findNodeBuildOfProjectRoot(projectRootPath);
1045+
let found = utils.findNodeBuildOfProjectRoot(projectRootPath, extensionConfiguration.path);
10171046
if (found != null) {
10181047
let bsbProcess = utils.runBuildWatcherUsingValidBuildPath(
10191048
found.buildPath,

server/src/utils.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,30 @@ export let findBscNativeOfFile = (
7272

7373
// TODO: this doesn't handle file:/// scheme
7474
export let findNodeBuildOfProjectRoot = (
75-
projectRootPath: p.DocumentUri
75+
projectRootPath: p.DocumentUri,
76+
pathToBinFromConfig: p.DocumentUri
7677
): null | { buildPath: p.DocumentUri; isReScript: boolean } => {
77-
let rescriptNodePath = path.join(projectRootPath, c.rescriptNodePartialPath);
78-
let bsbNodePath = path.join(projectRootPath, c.bsbNodePartialPath);
78+
if (pathToBinFromConfig !== "") {
79+
if (fs.existsSync(pathToBinFromConfig)) {
80+
if (pathToBinFromConfig.endsWith(c.rescriptBinName)) {
81+
return { buildPath: pathToBinFromConfig, isReScript: true };
82+
}
83+
if (pathToBinFromConfig.endsWith(c.rescriptBinName)) {
84+
return { buildPath: pathToBinFromConfig, isReScript: false };
85+
}
86+
}
87+
return null;
88+
} else {
89+
let rescriptNodePath = path.join(projectRootPath, c.rescriptNodePartialPath);
90+
let bsbNodePath = path.join(projectRootPath, c.bsbNodePartialPath);
7991

80-
if (fs.existsSync(rescriptNodePath)) {
81-
return { buildPath: rescriptNodePath, isReScript: true };
82-
} else if (fs.existsSync(bsbNodePath)) {
83-
return { buildPath: bsbNodePath, isReScript: false };
92+
if (fs.existsSync(rescriptNodePath)) {
93+
return { buildPath: rescriptNodePath, isReScript: true };
94+
} else if (fs.existsSync(bsbNodePath)) {
95+
return { buildPath: bsbNodePath, isReScript: false };
96+
}
97+
return null;
8498
}
85-
return null;
8699
};
87100

88101
type execResult =
@@ -589,7 +602,7 @@ export let parseCompilerLogOutput = (
589602
code: undefined,
590603
severity: t.DiagnosticSeverity.Error,
591604
tag: undefined,
592-
content: [lines[i], lines[i+1]],
605+
content: [lines[i], lines[i + 1]],
593606
});
594607
i++;
595608
} else if (/^ +([0-9]+| +|\.) (|)/.test(line)) {
@@ -603,9 +616,9 @@ export let parseCompilerLogOutput = (
603616
// 10 ┆
604617
} else if (line.startsWith(" ")) {
605618
// part of the actual diagnostics message
606-
parsedDiagnostics[parsedDiagnostics.length - 1].content.push(
607-
line.slice(2)
608-
);
619+
parsedDiagnostics[parsedDiagnostics.length - 1].content.push(
620+
line.slice(2)
621+
);
609622
} else if (line.trim() != "") {
610623
// We'll assume that everything else is also part of the diagnostics too.
611624
// Most of these should have been indented 2 spaces; sadly, some of them

0 commit comments

Comments
 (0)