UNPKG

@typespec/compiler

Version:

TypeSpec Compiler Preview

54 lines 1.88 kB
import { getNormalizedRealPath } from "../utils/misc.js"; export function createFileService({ serverHost }) { const compilerHost = serverHost.compilerHost; // Remember original URL when we convert it to a local path so that we can // get it back. We can't convert it back because things like URL-encoding // could give us back an equivalent but non-identical URL but the original // URL is used as a key into the opened documents and so we must reproduce // it exactly. const pathToURLMap = new Map(); return { upToDate, fileURLToRealPath, getPath, getURL, getOpenDocument, }; /** * Determine if the given document is the latest version. * * A document can become out-of-date if a change comes in during an async * operation. */ function upToDate(document) { if (!("version" in document)) { return true; } return document.version === serverHost.getOpenDocumentByURL(document.uri)?.version; } async function fileURLToRealPath(url) { return getNormalizedRealPath(compilerHost, compilerHost.fileURLToPath(url)); } async function getPath(document) { if (isUntitled(document.uri)) { return document.uri; } const path = await fileURLToRealPath(document.uri); pathToURLMap.set(path, document.uri); return path; } function getURL(path) { if (isUntitled(path)) { return path; } return pathToURLMap.get(path) ?? compilerHost.pathToFileURL(path); } function getOpenDocument(path) { const url = getURL(path); return url ? serverHost.getOpenDocumentByURL(url) : undefined; } } function isUntitled(pathOrUrl) { return pathOrUrl.startsWith("untitled:"); } //# sourceMappingURL=file-service.js.map