Skip to content

Commit 4198079

Browse files
An-Tucristianoc
authored andcommitted
The search for the path to the bsc is taken out of the formatCode function
1 parent e9232b0 commit 4198079

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

server/src/server.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ let findBuildBinary = (projectRootPath: p.DocumentUri) =>
7070
? utils.findBuildBinaryFromProjectRoot(projectRootPath)
7171
: utils.findBuildBinaryFromConfig(extensionConfiguration.binaryPath);
7272

73+
let findBscBinary = (filePath: p.DocumentUri) =>
74+
extensionConfiguration.binaryPath === null
75+
? utils.findBscBinaryFromProjectRoot(filePath)
76+
: utils.findBscBinaryFromConfig(extensionConfiguration.binaryPath);
77+
7378
interface CreateInterfaceRequestParams {
7479
uri: string;
7580
}
@@ -614,11 +619,8 @@ function format(msg: p.RequestMessage): Array<p.Message> {
614619
} else {
615620
// code will always be defined here, even though technically it can be undefined
616621
let code = getOpenedFileContent(params.textDocument.uri);
617-
let formattedResult = utils.formatCode(
618-
extensionConfiguration.binaryPath,
619-
filePath,
620-
code
621-
);
622+
let bscBinaryPath = findBscBinary(filePath);
623+
let formattedResult = utils.formatCode(bscBinaryPath, filePath, code);
622624
if (formattedResult.kind === "success") {
623625
let max = code.length;
624626
let result: p.TextEdit[] = [

server/src/utils.ts

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export let findProjectRootOfFile = (
4848
// Also, if someone's ever formatting a regular project setup's dependency
4949
// (which is weird but whatever), they'll at least find an upward bs-platform
5050
// from the dependent.
51-
export let findBscNativeOfFile = (
51+
export let findBscBinaryFromProjectRoot = (
5252
source: p.DocumentUri
5353
): null | p.DocumentUri => {
5454
let dir = path.dirname(source);
@@ -66,17 +66,14 @@ export let findBscNativeOfFile = (
6666
// reached the top
6767
return null;
6868
} else {
69-
return findBscNativeOfFile(dir);
69+
return findBscBinaryFromProjectRoot(dir);
7070
}
7171
};
7272

73-
let findBscBinFromConfig = (
74-
pathToBinFromConfig: p.DocumentUri | null
73+
export let findBscBinaryFromConfig = (
74+
pathToBinaryDirFromConfig: p.DocumentUri
7575
): null | p.DocumentUri => {
76-
if (pathToBinFromConfig === null) {
77-
return null;
78-
}
79-
let bscPath = path.join(pathToBinFromConfig, c.bscBinName);
76+
let bscPath = path.join(pathToBinaryDirFromConfig, c.bscBinName);
8077
if (fs.existsSync(bscPath)) {
8178
return bscPath;
8279
}
@@ -124,7 +121,7 @@ type execResult =
124121
};
125122

126123
export let formatCode = (
127-
pathToBinFromConfig: p.DocumentUri | null,
124+
bscPath: p.DocumentUri | null,
128125
filePath: string,
129126
code: string
130127
): execResult => {
@@ -134,15 +131,7 @@ export let formatCode = (
134131
encoding: "utf-8",
135132
});
136133
try {
137-
// Try to find the bsc bin from the binaryPath setting from the configuration.
138-
let bscPath = findBscBinFromConfig(pathToBinFromConfig);
139-
140-
// See comment on findBscNativeDirOfFile for why we need
141-
// to recursively search for bsc.exe upward
142-
bscPath = bscPath == null ? findBscNativeOfFile(filePath) : bscPath;
143-
144-
// Default to using the formatter from the binaryPath setting from the configuration
145-
// or the project formatter.
134+
// It will try to use the user formatting binary.
146135
// If not, use the one we ship with the analysis binary in the extension itself.
147136
if (bscPath != null) {
148137
let result = childProcess.execFileSync(bscPath, [

0 commit comments

Comments
 (0)