in tools/languageserver/src/main/java/com/google/cloud/verticals/foundations/dataharmonization/lsp/EngineUtil.java [179:281]
public InitializedBuilder loadEngine(String documentURI, String documentText, String importRoot) {
try {
// Build the ImportPath from the given jupyter documentURI.
String schema = URIParser.getSchema(URI.create(documentURI));
Path mappingPath = URIParser.getPath(URI.create(documentURI));
Path rootDir;
if (Strings.isNullOrEmpty(importRoot)) {
rootDir = mappingPath.getParent();
} else {
rootDir = Paths.get(importRoot);
}
ImportPath mappingImportPath = ImportPath.of(schema, mappingPath, rootDir);
// Add a list to the metadata which will store any ImportExceptions.
DefaultMetaData metadata = new DefaultMetaData();
metadata.setMeta(IMPORT_EXCEPTION_LIST_KEY, new ArrayList<ImportException>());
// Init engine with the loaded file and get all loaded plugins
InitializedBuilder engine =
new Engine.Builder(
InlineConfigExtractor.of(documentText, mappingImportPath, false), false)
.withDefaultLoaders(loaders)
.initialize(metadata);
Registries registries = engine.getRegistries();
// Currently present plugins retrieved.
Set<String> pluginsFromEngine =
registries.getLoadedPlugins().stream()
.map(Plugin::getPackageName)
.collect(Collectors.toSet());
// Remove any unused plugins for this document
removePlugins(
pluginsFromEngine,
pluginCompletionItems.column(documentURI),
loadedFunctionRegistries.get(documentURI),
loadedTargetRegistries.get(documentURI));
// Create completion items from registries
loadCompletionItems(
documentURI,
engine,
pluginCompletionItems,
loadedFunctionRegistries,
loadedTargetRegistries,
userDefinedCompletionItems);
// Keep track of the imported files for the document being processed, and init the
// symbolLookup maps for them.
// TODO(): Make importPaths part of the ImportProcessor interface
DefaultImportProcessor importProcessor = (DefaultImportProcessor) engine.getImportProcessor();
ImmutableSet<ImportPath> importPaths =
importProcessor.getImportPaths().stream()
.filter(i -> !i.getLoader().equals(PluginClassLoader.NAME))
.collect(toImmutableSet());
importPaths.forEach(
i -> {
documentImportURIs.get(documentURI).add(i.toFileInfo().getUrl());
symbolLookup.openDocument(i.toFileInfo().getUrl());
});
// Load location definitions
loadSymbolsFromRegistries(symbolLookup, engine);
// Add any import exceptions to the diagnostics.
List<ImportException> importExceptions = metadata.getMeta(IMPORT_EXCEPTION_LIST_KEY);
List<ImportExceptionDiagnostic> importDiagnostics =
createDiagnosticSourcesFromImportException(importExceptions);
importDiagnostics.forEach(
d ->
diagnosticMessageCollector.addDiagnosticsToDocumentURI(
d.getDocumentURI(), d.getDiagnostic()));
// Add a diagnostic to the root file to indicate which of its imports are creating diagnostic
// errors. This is primarily being done due to Jupyters inablility to publish diagnostics for
// files which have not explictily been opened.
if (!importDiagnostics.isEmpty()) {
ImmutableSet<String> importedURIsWithErrors =
importDiagnostics.stream()
.map(ImportExceptionDiagnostic::getDocumentURI)
.filter(uri -> !uri.equals(documentURI))
.collect(toImmutableSet());
if (!importedURIsWithErrors.isEmpty()) {
// Only publish the diagnostic if the set is non-empty.
Diagnostic rootFileImportDiagnostics =
new Diagnostic(
new Range(new Position(0, 0), new Position(0, 2)),
String.format("Errors in imported files: %s", importedURIsWithErrors),
DiagnosticSeverity.Error,
ENGINE_INIT_DIAGNOSTICS);
diagnosticMessageCollector.addDiagnosticsToDocumentURI(
documentURI, rootFileImportDiagnostics);
}
}
return engine;
} catch (IOException | RuntimeException e) {
logger.atSevere().withCause(e).log(
"Error while trying to init Engine instance for file %s.", documentURI);
return null;
}
}