Skip to content

Commit

Permalink
fix: prefer nearby tsconfig
Browse files Browse the repository at this point in the history
close #1193
  • Loading branch information
johnsoncodehk committed Jul 16, 2022
1 parent 6d58fb5 commit 5a4f2f7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/shared/src/path.ts
Expand Up @@ -20,5 +20,5 @@ export function normalizeUri(uri: string) {

export function isFileInDir(fileName: string, dir: string) {
const relative = upath.relative(dir, fileName);
return relative && !relative.startsWith('..') && !upath.isAbsolute(relative);
return !!relative && !relative.startsWith('..') && !upath.isAbsolute(relative);
}
17 changes: 13 additions & 4 deletions packages/vue-language-server/src/projects.ts
Expand Up @@ -237,7 +237,7 @@ export function createProjects(
const fileName = shared.uriToFsPath(uri);
const rootPaths = [...workspaces.keys()]
.filter(rootPath => shared.isFileInDir(fileName, rootPath))
.sort(sortPaths);
.sort((a, b) => sortPaths(a, b, fileName));

for (const rootPath of rootPaths) {
const workspace = workspaces.get(rootPath);
Expand Down Expand Up @@ -342,7 +342,7 @@ function createWorkspace(
}
}

matches = matches.sort(sortPaths);
matches = matches.sort((a, b) => sortPaths(fileName, a, b));

if (matches.length) {
getParsedCommandLine(matches[0]);
Expand All @@ -367,7 +367,7 @@ function createWorkspace(

const checked = new Set<string>();

for (const rootTsConfig of rootTsConfigs.sort(sortPaths)) {
for (const rootTsConfig of rootTsConfigs.sort((a, b) => sortPaths(fileName, a, b))) {
const project = await projects.fsPathGet(rootTsConfig);
if (project) {

Expand Down Expand Up @@ -455,7 +455,16 @@ function createWorkspace(
}
}

function sortPaths(a: string, b: string) {
function sortPaths(fileName: string, a: string, b: string) {

const inA = shared.isFileInDir(fileName, path.dirname(a));
const inB = shared.isFileInDir(fileName, path.dirname(b));

if (inA !== inB) {
const aWeight = inA ? 1 : 0;
const bWeight = inB ? 1 : 0;
return bWeight - aWeight;
}

const aLength = a.split('/').length;
const bLength = b.split('/').length;
Expand Down

0 comments on commit 5a4f2f7

Please sign in to comment.