Skip to content

Commit 8108098

Browse files
committed
The binaryPath setting was added to the formatCode function
1 parent 4deba1e commit 8108098

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

server/src/constants.ts

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

35+
export let bscBinName = "bsc";
36+
3537
// can't use the native bsb/rescript since we might need the watcher -w flag, which is only in the JS wrapper
3638
export let rescriptNodePartialPath = path.join(
3739
"node_modules",

server/src/server.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,11 @@ function format(msg: p.RequestMessage): Array<p.Message> {
596596
} else {
597597
// code will always be defined here, even though technically it can be undefined
598598
let code = getOpenedFileContent(params.textDocument.uri);
599-
let formattedResult = utils.formatCode(filePath, code);
599+
let formattedResult = utils.formatCode(
600+
extensionConfiguration.binaryPath,
601+
filePath,
602+
code
603+
);
600604
if (formattedResult.kind === "success") {
601605
let max = code.length;
602606
let result: p.TextEdit[] = [

server/src/utils.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@ export let findBscNativeOfFile = (
7070
}
7171
};
7272

73+
let findBscBinFromConfig = (
74+
pathToBinFromConfig: p.DocumentUri | null
75+
): null | p.DocumentUri => {
76+
if (pathToBinFromConfig === null) {
77+
return null;
78+
}
79+
let bscPath = path.join(pathToBinFromConfig, c.bscBinName);
80+
if (fs.existsSync(bscPath)) {
81+
return bscPath;
82+
}
83+
return null;
84+
};
85+
7386
// TODO: this doesn't handle file:/// scheme
7487
export let findNodeBuildOfProjectRoot = (
7588
projectRootPath: p.DocumentUri
@@ -94,21 +107,30 @@ type execResult =
94107
kind: "error";
95108
error: string;
96109
};
97-
export let formatCode = (filePath: string, code: string): execResult => {
110+
111+
export let formatCode = (
112+
pathToBinFromConfig: p.DocumentUri | null,
113+
filePath: string,
114+
code: string
115+
): execResult => {
98116
let extension = path.extname(filePath);
99117
let formatTempFileFullPath = createFileInTempDir(extension);
100118
fs.writeFileSync(formatTempFileFullPath, code, {
101119
encoding: "utf-8",
102120
});
103121
try {
122+
// Try to find the bsc bin from the binaryPath setting from the configuration.
123+
let bscPath = findBscBinFromConfig(pathToBinFromConfig);
124+
104125
// See comment on findBscNativeDirOfFile for why we need
105126
// to recursively search for bsc.exe upward
106-
let bscNativePath = findBscNativeOfFile(filePath);
127+
bscPath = bscPath == null ? findBscNativeOfFile(filePath) : bscPath;
107128

108-
// Default to using the project formatter. If not, use the one we ship with
109-
// the analysis binary in the extension itself.
110-
if (bscNativePath != null) {
111-
let result = childProcess.execFileSync(bscNativePath, [
129+
// Default to using the formatter from the binaryPath setting from the configuration
130+
// or the project formatter.
131+
// If not, use the one we ship with the analysis binary in the extension itself.
132+
if (bscPath != null) {
133+
let result = childProcess.execFileSync(bscPath, [
112134
"-color",
113135
"never",
114136
"-format",

0 commit comments

Comments
 (0)