Skip to content

Commit ffd4893

Browse files
committed
some reorganization
1 parent 332a087 commit ffd4893

File tree

6 files changed

+1451
-43
lines changed

6 files changed

+1451
-43
lines changed

rescript-vscode-0.0.8.vsix

608 KB
Binary file not shown.

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
@@ -1,5 +1,6 @@
11
import process from "process";
22
import * as p from "vscode-languageserver-protocol";
3+
import * as Project from "./project";
34
import * as m from "vscode-jsonrpc/lib/messages";
45
import * as v from "vscode-languageserver";
56
import * as path from "path";
@@ -122,7 +123,7 @@ let openedFile = (fileUri: string, fileContent: string) => {
122123

123124
stupidFileContentCache.set(filePath, fileContent);
124125

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

180181
stupidFileContentCache.delete(filePath);
181182

182-
let buildRootPath = utils.findBuildRootOfFile(filePath);
183+
let buildRootPath = Project.findBuildRoot(filePath);
183184
if (buildRootPath != null) {
184185
let root = projectsFiles.get(buildRootPath);
185186
if (root != null) {
@@ -411,8 +412,8 @@ process.on("message", (msg: m.Message) => {
411412
process.send!(fakeSuccessResponse);
412413
process.send!(response);
413414
} else {
414-
let projectRootPath = utils.findProjectRootOfFile(filePath);
415-
if (projectRootPath == null) {
415+
let bscPath = Project.findBscPath(filePath);
416+
if (bscPath == null) {
416417
let params: p.ShowMessageParams = {
417418
type: p.MessageType.Error,
418419
message: `Cannot find a nearby ${c.bscPartialPath}. It's needed for determining the project's root.`,
@@ -425,7 +426,6 @@ process.on("message", (msg: m.Message) => {
425426
process.send!(fakeSuccessResponse);
426427
process.send!(response);
427428
} else {
428-
let bscPath = path.join(projectRootPath, c.bscPartialPath);
429429
if (!fs.existsSync(bscPath)) {
430430
let params: p.ShowMessageParams = {
431431
type: p.MessageType.Error,

server/src/utils.ts

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ 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 fs from "fs";
87
import * as os from "os";
@@ -16,42 +15,6 @@ export let createFileInTempDir = (extension = "") => {
1615
return path.join(os.tmpdir(), tempFileName);
1716
};
1817

19-
// TODO: races here?
20-
// TODO: this doesn't handle file:/// scheme
21-
export let findProjectRootOfFile = (
22-
source: p.DocumentUri
23-
): null | p.DocumentUri => {
24-
let dir = path.dirname(source);
25-
if (fs.existsSync(path.join(dir, c.bscPartialPath))) {
26-
return dir;
27-
} else {
28-
if (dir === source) {
29-
// reached top
30-
return null;
31-
} else {
32-
return findProjectRootOfFile(dir);
33-
}
34-
}
35-
};
36-
37-
// the "build root" represents the nearest directory containing a "bsconfig.json" file.
38-
// "bsconfig.json" can be used to locate the nearest build artefacts
39-
export let findBuildRootOfFile = (
40-
source: p.DocumentUri
41-
): null | p.DocumentUri => {
42-
let dir = path.dirname(source);
43-
if (fs.existsSync(path.join(dir, c.bsconfigPartialPath))) {
44-
return dir;
45-
} else {
46-
if (dir === source) {
47-
// reached top
48-
return null;
49-
} else {
50-
return findBuildRootOfFile(dir);
51-
}
52-
}
53-
};
54-
5518
type execResult =
5619
| {
5720
kind: "success";

0 commit comments

Comments
 (0)