Skip to content

Commit f27237a

Browse files
committedJan 6, 2021
Remove the need to detect bsconfig for formatting
1 parent d75c664 commit f27237a

File tree

2 files changed

+40
-55
lines changed

2 files changed

+40
-55
lines changed
 

‎server/src/server.ts

+35-50
Original file line numberDiff line numberDiff line change
@@ -417,11 +417,15 @@ process.on("message", (msg: m.Message) => {
417417
process.send!(fakeSuccessResponse);
418418
process.send!(response);
419419
} else {
420-
let projectRootPath = utils.findProjectRootOfFile(filePath);
421-
if (projectRootPath == null) {
420+
// See comment on findBscExeDirOfFile for why we need
421+
// to recursively search for bsc.exe upward
422+
let bscExeDir = utils.findBscExeDirOfFile(
423+
filePath
424+
);
425+
if (bscExeDir === null) {
422426
let params: p.ShowMessageParams = {
423427
type: p.MessageType.Error,
424-
message: `Cannot find a nearby ${c.bsconfigPartialPath}. It's needed for determining the project's root.`,
428+
message: `Cannot find a nearby ${c.bscExePartialPath}. It's needed for formatting.`,
425429
};
426430
let response: m.NotificationMessage = {
427431
jsonrpc: c.jsonrpcVersion,
@@ -431,58 +435,39 @@ process.on("message", (msg: m.Message) => {
431435
process.send!(fakeSuccessResponse);
432436
process.send!(response);
433437
} else {
434-
// See comment on findBscExeDirUpwardFromProjectRoot for why we need
435-
// to recursively search for bsc.exe upward
436-
let bscExeDir = utils.findBscExeDirUpwardFromProjectRoot(
437-
projectRootPath
438+
let resolvedBscPath = path.join(bscExeDir, c.bscExePartialPath);
439+
// code will always be defined here, even though technically it can be undefined
440+
let code = getOpenedFileContent(params.textDocument.uri);
441+
let formattedResult = utils.formatUsingValidBscPath(
442+
code,
443+
resolvedBscPath,
444+
extension === c.resiExt
438445
);
439-
if (bscExeDir === null) {
440-
let params: p.ShowMessageParams = {
441-
type: p.MessageType.Error,
442-
message: `Cannot find a nearby ${c.bscExePartialPath}. It's needed for formatting.`,
443-
};
444-
let response: m.NotificationMessage = {
446+
if (formattedResult.kind === "success") {
447+
let result: p.TextEdit[] = [
448+
{
449+
range: {
450+
start: { line: 0, character: 0 },
451+
end: {
452+
line: Number.MAX_VALUE,
453+
character: Number.MAX_VALUE,
454+
},
455+
},
456+
newText: formattedResult.result,
457+
},
458+
];
459+
let response: m.ResponseMessage = {
445460
jsonrpc: c.jsonrpcVersion,
446-
method: "window/showMessage",
447-
params: params,
461+
id: msg.id,
462+
result: result,
448463
};
449-
process.send!(fakeSuccessResponse);
450464
process.send!(response);
451465
} else {
452-
let resolvedBscPath = path.join(bscExeDir, c.bscExePartialPath);
453-
// code will always be defined here, even though technically it can be undefined
454-
let code = getOpenedFileContent(params.textDocument.uri);
455-
let formattedResult = utils.formatUsingValidBscPath(
456-
code,
457-
resolvedBscPath,
458-
extension === c.resiExt
459-
);
460-
if (formattedResult.kind === "success") {
461-
let result: p.TextEdit[] = [
462-
{
463-
range: {
464-
start: { line: 0, character: 0 },
465-
end: {
466-
line: Number.MAX_VALUE,
467-
character: Number.MAX_VALUE,
468-
},
469-
},
470-
newText: formattedResult.result,
471-
},
472-
];
473-
let response: m.ResponseMessage = {
474-
jsonrpc: c.jsonrpcVersion,
475-
id: msg.id,
476-
result: result,
477-
};
478-
process.send!(response);
479-
} else {
480-
// let the diagnostics logic display the updated syntax errors,
481-
// from the build.
482-
// Again, not sending the actual errors. See fakeSuccessResponse
483-
// above for explanation
484-
process.send!(fakeSuccessResponse);
485-
}
466+
// let the diagnostics logic display the updated syntax errors,
467+
// from the build.
468+
// Again, not sending the actual errors. See fakeSuccessResponse
469+
// above for explanation
470+
process.send!(fakeSuccessResponse);
486471
}
487472
}
488473
}

‎server/src/utils.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,19 @@ export let findProjectRootOfFile = (
4444
// Also, if someone's ever formatting a regular project setup's dependency
4545
// (which is weird but whatever), they'll at least find an upward bs-platform
4646
// from the dependent.
47-
export let findBscExeDirUpwardFromProjectRoot = (
48-
dir: p.DocumentUri
47+
export let findBscExeDirOfFile = (
48+
source: p.DocumentUri
4949
): null | p.DocumentUri => {
50+
let dir = path.dirname(source);
5051
let bscPath = path.join(dir, c.bscExePartialPath);
5152
if (fs.existsSync(bscPath)) {
5253
return dir;
5354
} else {
54-
let parentDir = path.dirname(dir);
55-
if (parentDir === dir) {
55+
if (dir === source) {
5656
// reached the top
5757
return null;
5858
} else {
59-
return findBscExeDirUpwardFromProjectRoot(parentDir);
59+
return findBscExeDirOfFile(dir);
6060
}
6161
}
6262
};

0 commit comments

Comments
 (0)