Skip to content

Tweak project root heuristics #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
some reorganization
  • Loading branch information
BlueHotDog committed Dec 16, 2020
commit ffd4893224466c86befc628bf459da4500a3c449
Binary file added rescript-vscode-0.0.8.vsix
Binary file not shown.
19 changes: 19 additions & 0 deletions server/src/fs_helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as Path from "path";
import Fs from "fs";

export let findPathRec = (
currentPath: string,
targetPath: string
): null | string => {
let baseDir = Path.dirname(currentPath);
if (Fs.existsSync(Path.join(baseDir, targetPath))) {
return baseDir;
} else {
if (baseDir === currentPath) {
// reached top
return null;
} else {
return findPathRec(baseDir, targetPath);
}
}
};
29 changes: 29 additions & 0 deletions server/src/project.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as LSP from "vscode-languageserver-protocol";
import * as FsHelpers from "./fs_helpers";
import * as Const from "./constants";
import * as Path from "path";

// TODO: races here?
// TODO: this doesn't handle file:/// scheme
export let rootByPath = (
source: LSP.DocumentUri
): null | LSP.DocumentUri => {
return FsHelpers.findPathRec(source, Const.bscPartialPath);
};

export let bscPath = (source: LSP.DocumentUri): LSP.DocumentUri => Path.join(source, Const.bscPartialPath);

export let findBscPath = (
source: LSP.DocumentUri
): null | LSP.DocumentUri => {
let rootPath = rootByPath(source);
return rootPath == null ? null : bscPath(rootPath);
}

// the "build root" represents the nearest directory containing a "bsconfig.json" file.
// "bsconfig.json" can be used to locate the nearest build artifacts
export let findBuildRoot = (
source: LSP.DocumentUri
): null | LSP.DocumentUri => {
return FsHelpers.findPathRec(source, Const.bsconfigPartialPath);
};
12 changes: 6 additions & 6 deletions server/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import process from "process";
import * as p from "vscode-languageserver-protocol";
import * as Project from "./project";
import * as m from "vscode-jsonrpc/lib/messages";
import * as v from "vscode-languageserver";
import * as path from "path";
Expand Down Expand Up @@ -122,7 +123,7 @@ let openedFile = (fileUri: string, fileContent: string) => {

stupidFileContentCache.set(filePath, fileContent);

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

stupidFileContentCache.delete(filePath);

let buildRootPath = utils.findBuildRootOfFile(filePath);
let buildRootPath = Project.findBuildRoot(filePath);
if (buildRootPath != null) {
let root = projectsFiles.get(buildRootPath);
if (root != null) {
Expand Down Expand Up @@ -411,8 +412,8 @@ process.on("message", (msg: m.Message) => {
process.send!(fakeSuccessResponse);
process.send!(response);
} else {
let projectRootPath = utils.findProjectRootOfFile(filePath);
if (projectRootPath == null) {
let bscPath = Project.findBscPath(filePath);
if (bscPath == null) {
let params: p.ShowMessageParams = {
type: p.MessageType.Error,
message: `Cannot find a nearby ${c.bscPartialPath}. It's needed for determining the project's root.`,
Expand All @@ -425,7 +426,6 @@ process.on("message", (msg: m.Message) => {
process.send!(fakeSuccessResponse);
process.send!(response);
} else {
let bscPath = path.join(projectRootPath, c.bscPartialPath);
if (!fs.existsSync(bscPath)) {
let params: p.ShowMessageParams = {
type: p.MessageType.Error,
Expand Down
37 changes: 0 additions & 37 deletions server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Range } from "vscode-languageserver-textdocument";
import * as c from "./constants";
import * as childProcess from "child_process";
import * as p from "vscode-languageserver-protocol";
import * as path from "path";
import * as t from "vscode-languageserver-types";
import fs from "fs";
import * as os from "os";
Expand All @@ -16,42 +15,6 @@ export let createFileInTempDir = (extension = "") => {
return path.join(os.tmpdir(), tempFileName);
};

// TODO: races here?
// TODO: this doesn't handle file:/// scheme
export let findProjectRootOfFile = (
source: p.DocumentUri
): null | p.DocumentUri => {
let dir = path.dirname(source);
if (fs.existsSync(path.join(dir, c.bscPartialPath))) {
return dir;
} else {
if (dir === source) {
// reached top
return null;
} else {
return findProjectRootOfFile(dir);
}
}
};

// the "build root" represents the nearest directory containing a "bsconfig.json" file.
// "bsconfig.json" can be used to locate the nearest build artefacts
export let findBuildRootOfFile = (
source: p.DocumentUri
): null | p.DocumentUri => {
let dir = path.dirname(source);
if (fs.existsSync(path.join(dir, c.bsconfigPartialPath))) {
return dir;
} else {
if (dir === source) {
// reached top
return null;
} else {
return findBuildRootOfFile(dir);
}
}
};

type execResult =
| {
kind: "success";
Expand Down
Loading