UNPKG

@typespec/compiler

Version:

TypeSpec compiler and standard library

84 lines 3.11 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(); /** Track the init version when a doc is opened so that we can distinguish whether the doc is opened or changed */ const documentsOpenedInitVersion = new Map(); return { upToDate, fileURLToRealPath, getPath, getURL, getOpenDocument, getOpenDocumentInitVersion, notifyDocumentOpened, notifyDocumentClosed, }; /** * 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 getOpenDocumentInitVersion(uri) { return documentsOpenedInitVersion.get(uri) ?? undefined; } function notifyDocumentOpened(arg) { if (documentsOpenedInitVersion.has(arg.document.uri)) { // not expected, log something for troubleshooting serverHost.log({ level: "debug", message: "Document already opened", detail: arg.document.uri, }); } documentsOpenedInitVersion.set(arg.document.uri, arg.document.version); } function notifyDocumentClosed(arg) { if (!documentsOpenedInitVersion.has(arg.document.uri)) { // not expected, log something for troubleshooting serverHost.log({ level: "debug", message: "Document already closed", detail: arg.document.uri, }); } documentsOpenedInitVersion.delete(arg.document.uri); } } function isUntitled(pathOrUrl) { return pathOrUrl.startsWith("untitled:"); } //# sourceMappingURL=file-service.js.map