diff options
Diffstat (limited to 'packages/lsp/src/root.ts')
| -rw-r--r-- | packages/lsp/src/root.ts | 60 |
1 files changed, 30 insertions, 30 deletions
diff --git a/packages/lsp/src/root.ts b/packages/lsp/src/root.ts index fc7814e..b0801fd 100644 --- a/packages/lsp/src/root.ts +++ b/packages/lsp/src/root.ts @@ -3,42 +3,42 @@ */ export async function findRoot( - startDir: string, - cwd: string, - markers: readonly string[], - exists: (path: string) => Promise<boolean>, + startDir: string, + cwd: string, + markers: readonly string[], + exists: (path: string) => Promise<boolean>, ): Promise<string> { - const normalizedStart = normalizePath(startDir); - const normalizedCwd = normalizePath(cwd); + const normalizedStart = normalizePath(startDir); + const normalizedCwd = normalizePath(cwd); - let current = normalizedStart; - while (true) { - for (const marker of markers) { - const markerPath = current === "/" ? `/${marker}` : `${current}/${marker}`; - if (await exists(markerPath)) { - return current; - } - } - if (current === normalizedCwd || current === "/") { - return normalizedCwd; - } - const parent = getParent(current); - if (parent === current) return normalizedCwd; - current = parent; - } + let current = normalizedStart; + while (true) { + for (const marker of markers) { + const markerPath = current === "/" ? `/${marker}` : `${current}/${marker}`; + if (await exists(markerPath)) { + return current; + } + } + if (current === normalizedCwd || current === "/") { + return normalizedCwd; + } + const parent = getParent(current); + if (parent === current) return normalizedCwd; + current = parent; + } } function normalizePath(p: string): string { - let normalized = p.replace(/\\/g, "/"); - if (normalized.length > 1 && normalized.endsWith("/")) { - normalized = normalized.slice(0, -1); - } - return normalized || "/"; + let normalized = p.replace(/\\/g, "/"); + if (normalized.length > 1 && normalized.endsWith("/")) { + normalized = normalized.slice(0, -1); + } + return normalized || "/"; } function getParent(p: string): string { - if (p === "/") return "/"; - const lastSlash = p.lastIndexOf("/"); - if (lastSlash <= 0) return "/"; - return p.slice(0, lastSlash) || "/"; + if (p === "/") return "/"; + const lastSlash = p.lastIndexOf("/"); + if (lastSlash <= 0) return "/"; + return p.slice(0, lastSlash) || "/"; } |
