UNPKG

greybel-languageserver-core

Version:
67 lines 2.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DocumentGraphBuilder = void 0; const types_1 = require("../../types"); class DocumentGraphBuilder { constructor(options) { this.documentManager = options.documentManager; this.entrypoint = options.entrypoint; this.nodeCache = new Map(); this.pendingDocument = new Map(); } async getDocument(location) { if (this.pendingDocument.has(location)) { return this.pendingDocument.get(location); } const promise = this.documentManager.getOrOpen(location); this.pendingDocument.set(location, promise); const document = await promise; this.pendingDocument.delete(location); return document; } async build() { const initialNode = { item: { document: this.entrypoint, location: { type: types_1.DependencyType.Root, location: this.entrypoint.textDocument.uri } }, children: [] }; if (this.entrypoint.parsedPayload === null) { return initialNode; } this.nodeCache.set(this.entrypoint.textDocument.uri, initialNode); const traverse = async (rootResult, rootNode) => { const dependencies = await rootResult.getDependencies(); await Promise.all(dependencies.map(async (dependency) => { const existingNode = this.nodeCache.get(dependency.location); if (existingNode != null) { rootNode.children.push(existingNode); return; } const item = await this.getDocument(dependency.location); if (item === null) return; const childNode = { item: { document: item, location: dependency }, children: [] }; this.nodeCache.set(dependency.location, childNode); rootNode.children.push(childNode); if (item.parsedPayload !== null) { await traverse(item, childNode); } })); }; await traverse(this.entrypoint, initialNode); return initialNode; } } exports.DocumentGraphBuilder = DocumentGraphBuilder; //# sourceMappingURL=document-graph-builder.js.map