Skip to content

Commit 16d68b7

Browse files
committed
some reorganization
1 parent d535be5 commit 16d68b7

File tree

4 files changed

+54
-43
lines changed

4 files changed

+54
-43
lines changed

server/src/fs_helpers.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import * as Path from "path";
2+
import Fs from "fs";
3+
4+
export let findPathRec = (
5+
currentPath: string,
6+
targetPath: string
7+
): null | string => {
8+
let baseDir = Path.dirname(currentPath);
9+
if (Fs.existsSync(Path.join(baseDir, targetPath))) {
10+
return baseDir;
11+
} else {
12+
if (baseDir === currentPath) {
13+
// reached top
14+
return null;
15+
} else {
16+
return findPathRec(baseDir, targetPath);
17+
}
18+
}
19+
};

server/src/project.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as LSP from "vscode-languageserver-protocol";
2+
import * as FsHelpers from "./fs_helpers";
3+
import * as Const from "./constants";
4+
import * as Path from "path";
5+
6+
// TODO: races here?
7+
// TODO: this doesn't handle file:/// scheme
8+
export let rootByPath = (
9+
source: LSP.DocumentUri
10+
): null | LSP.DocumentUri => {
11+
return FsHelpers.findPathRec(source, Const.bscPartialPath);
12+
};
13+
14+
export let bscPath = (source: LSP.DocumentUri): LSP.DocumentUri => Path.join(source, Const.bscPartialPath);
15+
16+
export let findBscPath = (
17+
source: LSP.DocumentUri
18+
): null | LSP.DocumentUri => {
19+
let rootPath = rootByPath(source);
20+
return rootPath == null ? null : bscPath(rootPath);
21+
}
22+
23+
// the "build root" represents the nearest directory containing a "bsconfig.json" file.
24+
// "bsconfig.json" can be used to locate the nearest build artifacts
25+
export let findBuildRoot = (
26+
source: LSP.DocumentUri
27+
): null | LSP.DocumentUri => {
28+
return FsHelpers.findPathRec(source, Const.bsconfigPartialPath);
29+
};

server/src/server.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import process from "process";
22
import * as p from "vscode-languageserver-protocol";
33
import * as t from "vscode-languageserver-types";
44
import * as j from "vscode-jsonrpc";
5+
import * as Project from "./project";
56
import * as m from "vscode-jsonrpc/lib/messages";
67
import * as v from "vscode-languageserver";
78
import * as path from "path";
@@ -121,7 +122,7 @@ let openedFile = (fileUri: string, fileContent: string) => {
121122

122123
stupidFileContentCache.set(filePath, fileContent);
123124

124-
let buildRootPath = utils.findBuildRootOfFile(filePath);
125+
let buildRootPath = Project.findBuildRoot(filePath);
125126
if (buildRootPath != null) {
126127
if (!projectsFiles.has(buildRootPath)) {
127128
projectsFiles.set(buildRootPath, {
@@ -140,7 +141,7 @@ let openedFile = (fileUri: string, fileContent: string) => {
140141
// because otherwise the diagnostics info we'll display might be stale
141142
let bsbLockPath = path.join(buildRootPath, c.bsbLock);
142143
if (firstOpenFileOfProject && !fs.existsSync(bsbLockPath)) {
143-
let bsbPath = path.join(buildRootPath, c.bsbPartialPath);
144+
let bsbPath = Project.bscPath(buildRootPath);
144145
// TODO: sometime stale .bsb.lock dangling. bsb -w knows .bsb.lock is
145146
// stale. Use that logic
146147
// TODO: close watcher when lang-server shuts down
@@ -178,7 +179,7 @@ let closedFile = (fileUri: string) => {
178179

179180
stupidFileContentCache.delete(filePath);
180181

181-
let buildRootPath = utils.findBuildRootOfFile(filePath);
182+
let buildRootPath = Project.findBuildRoot(filePath);
182183
if (buildRootPath != null) {
183184
let root = projectsFiles.get(buildRootPath);
184185
if (root != null) {
@@ -371,8 +372,8 @@ process.on("message", (msg: m.Message) => {
371372
process.send!(fakeSuccessResponse);
372373
process.send!(response);
373374
} else {
374-
let projectRootPath = utils.findProjectRootOfFile(filePath);
375-
if (projectRootPath == null) {
375+
let bscPath = Project.findBscPath(filePath);
376+
if (bscPath == null) {
376377
let params: p.ShowMessageParams = {
377378
type: p.MessageType.Error,
378379
message: `Cannot find a nearby ${c.bscPartialPath}. It's needed for determining the project's root.`,
@@ -385,7 +386,6 @@ process.on("message", (msg: m.Message) => {
385386
process.send!(fakeSuccessResponse);
386387
process.send!(response);
387388
} else {
388-
let bscPath = path.join(projectRootPath, c.bscPartialPath);
389389
if (!fs.existsSync(bscPath)) {
390390
let params: p.ShowMessageParams = {
391391
type: p.MessageType.Error,

server/src/utils.ts

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,10 @@ import { Range } from "vscode-languageserver-textdocument";
22
import * as c from "./constants";
33
import * as childProcess from "child_process";
44
import * as p from "vscode-languageserver-protocol";
5-
import * as path from "path";
65
import * as t from "vscode-languageserver-types";
76
import * as tmp from "tmp";
87
import fs from "fs";
98

10-
// TODO: races here?
11-
// TODO: this doesn't handle file:/// scheme
12-
export let findProjectRootOfFile = (
13-
source: p.DocumentUri
14-
): null | p.DocumentUri => {
15-
let dir = path.dirname(source);
16-
if (fs.existsSync(path.join(dir, c.bscPartialPath))) {
17-
return dir;
18-
} else {
19-
if (dir === source) {
20-
// reached top
21-
return null;
22-
} else {
23-
return findProjectRootOfFile(dir);
24-
}
25-
}
26-
};
27-
28-
// the "build root" represents the nearest directory containing a "bsconfig.json" file.
29-
// "bsconfig.json" can be used to locate the nearest build artefacts
30-
export let findBuildRootOfFile = (
31-
source: p.DocumentUri
32-
): null | p.DocumentUri => {
33-
let dir = path.dirname(source);
34-
if (fs.existsSync(path.join(dir, c.bsconfigPartialPath))) {
35-
return dir;
36-
} else {
37-
if (dir === source) {
38-
// reached top
39-
return null;
40-
} else {
41-
return findBuildRootOfFile(dir);
42-
}
43-
}
44-
};
45-
469
type execResult =
4710
| {
4811
kind: "success";

0 commit comments

Comments
 (0)