Skip to content

Commit b9a0dac

Browse files
committed
addressing PR comments
1 parent 4779cd2 commit b9a0dac

File tree

2 files changed

+10
-36
lines changed

2 files changed

+10
-36
lines changed

packages/language/src/index.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ export async function loadDocument(
6363

6464
// load imports
6565
const importedURIs = await loadImports(document, langiumDocuments);
66-
const importedDocuments = await Promise.all(importedURIs.map((uri) => langiumDocuments.getOrCreateDocument(uri)));
66+
const importedDocuments: LangiumDocument[] = [];
67+
for (const uri of importedURIs) {
68+
importedDocuments.push(await langiumDocuments.getOrCreateDocument(uri));
69+
}
6770

6871
// build the document together with standard library, plugin modules, and imported documents
6972
await services.shared.workspace.DocumentBuilder.build([stdLib, ...pluginDocs, document, ...importedDocuments], {
@@ -128,11 +131,7 @@ export async function loadDocument(
128131
};
129132
}
130133

131-
async function loadImports(
132-
document: LangiumDocument<AstNode>,
133-
documents: LangiumDocuments,
134-
uris: Set<string> = new Set(),
135-
) {
134+
async function loadImports(document: LangiumDocument, documents: LangiumDocuments, uris: Set<string> = new Set()) {
136135
const uriString = document.uri.toString();
137136
if (!uris.has(uriString)) {
138137
uris.add(uriString);
@@ -159,7 +158,7 @@ function mergeImportsDeclarations(documents: LangiumDocuments, model: Model) {
159158
// remove import directives
160159
model.imports = [];
161160

162-
// fix $containerIndex
161+
// fix $container, $containerIndex, and $containerProperty
163162
linkContentToContainer(model);
164163

165164
return importedModels;

packages/language/src/utils.ts

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -413,38 +413,13 @@ export function resolveImport(documents: LangiumDocuments, imp: ModelImport) {
413413
}
414414

415415
export function resolveImportUri(imp: ModelImport) {
416-
if (!imp.path) return undefined; // This will return true if imp.path is undefined, null, or an empty string ("").
417-
418-
if (!imp.path.endsWith('.zmodel')) {
419-
imp.path += '.zmodel';
420-
}
421-
422-
if (
423-
!imp.path.startsWith('.') && // Respect relative paths
424-
!path.isAbsolute(imp.path) // Respect Absolute paths
425-
) {
426-
// use the current model's path as the search context
427-
const contextPath = imp.$container.$document
428-
? path.dirname(imp.$container.$document.uri.fsPath)
429-
: process.cwd();
430-
imp.path = findNodeModulesFile(imp.path, contextPath) ?? imp.path;
416+
if (!imp.path) {
417+
return undefined;
431418
}
432-
433419
const doc = AstUtils.getDocument(imp);
434420
const dir = path.dirname(doc.uri.fsPath);
435-
return URI.file(path.resolve(dir, imp.path));
436-
}
437-
438-
export function findNodeModulesFile(name: string, cwd: string = process.cwd()) {
439-
if (!name) return undefined;
440-
try {
441-
// Use require.resolve to find the module/file. The paths option allows specifying the directory to start from.
442-
const resolvedPath = require.resolve(name, { paths: [cwd] });
443-
return resolvedPath;
444-
} catch {
445-
// If require.resolve fails to find the module/file, it will throw an error.
446-
return undefined;
447-
}
421+
const importPath = imp.path.endsWith('.zmodel') ? imp.path : `${imp.path}.zmodel`;
422+
return URI.file(path.resolve(dir, importPath));
448423
}
449424

450425
/**

0 commit comments

Comments
 (0)