initial commit
This commit is contained in:
40
src/root.ts
Normal file
40
src/root.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import * as fs from "node:fs";
|
||||
import * as path from "node:path";
|
||||
import { pathToFileURL, fileURLToPath } from "node:url";
|
||||
import { servers } from "../server.ts";
|
||||
import type { ServerConfig } from "./types.ts";
|
||||
|
||||
// Resolve File URI To Local Path
|
||||
export function uriToPath(uri: string): string {
|
||||
return fileURLToPath(uri);
|
||||
}
|
||||
|
||||
// Resolve Local Path To File URI
|
||||
export function pathToUri(p: string): string {
|
||||
return pathToFileURL(path.resolve(p)).toString();
|
||||
}
|
||||
|
||||
// Pick Server By File Extension - match[] entries are matched against the
|
||||
// file's extension (no dot). First server in the registry wins.
|
||||
export function pickServer(filePath: string): ServerConfig {
|
||||
const ext = path.extname(filePath).replace(/^\./, "");
|
||||
const hit = servers.find((s) => s.match.includes(ext));
|
||||
if (!hit) {
|
||||
throw new Error(`No LSP server registered for extension ".${ext}"`);
|
||||
}
|
||||
return hit;
|
||||
}
|
||||
|
||||
// Find Project Root By Walking Upward - stops at the first directory
|
||||
// containing any rootMarker. Falls back to the file's directory.
|
||||
export function findRoot(filePath: string, markers: string[]): string {
|
||||
let dir = path.dirname(path.resolve(filePath));
|
||||
const { root } = path.parse(dir);
|
||||
while (true) {
|
||||
for (const m of markers) {
|
||||
if (fs.existsSync(path.join(dir, m))) return dir;
|
||||
}
|
||||
if (dir === root) return path.dirname(path.resolve(filePath));
|
||||
dir = path.dirname(dir);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user