/** Initialize this index with information from the user libraries. */ private boolean indexUserLibraries() { boolean librariesIndexed = true; try { AnalysisServer analysisServer = PackageLibraryManagerProvider.getDefaultAnalysisServer(); SavedContext savedContext = analysisServer.getSavedContext(); DartModel model = DartCore.create(ResourcesPlugin.getWorkspace().getRoot()); for (DartProject project : model.getDartProjects()) { for (DartLibrary library : project.getDartLibraries()) { CompilationUnit compilationUnit = library.getDefiningCompilationUnit(); if (compilationUnit == null) { continue; } IResource libraryResource = compilationUnit.getResource(); if (libraryResource == null) { continue; } IPath libraryLocation = libraryResource.getLocation(); if (libraryLocation == null) { continue; } File libraryFile = libraryLocation.toFile(); savedContext.resolve(libraryFile, null); } } } catch (Exception exception) { librariesIndexed = false; DartCore.logError("Could not index user libraries", exception); } return librariesIndexed; }
/** * Initialize this index with information from the bundled libraries. * * @return <code>true</code> if the bundled libraries were successfully indexed */ private boolean indexBundledLibraries() { boolean librariesIndexed = true; long startTime = System.currentTimeMillis(); PackageLibraryManager libraryManager = PackageLibraryManagerProvider.getPackageLibraryManager(); ArrayList<String> librarySpecs = new ArrayList<String>(libraryManager.getAllLibrarySpecs()); if (librarySpecs.remove("dart:html")) { librarySpecs.add("dart:html"); } AnalysisServer analysisServer = PackageLibraryManagerProvider.getDefaultAnalysisServer(); analysisServer.reanalyze(); SavedContext savedContext = analysisServer.getSavedContext(); for (String urlSpec : librarySpecs) { try { URI libraryUri = new URI(urlSpec); File libraryFile = new File(libraryManager.resolveDartUri(libraryUri)); savedContext.resolve(libraryFile, null); } catch (URISyntaxException exception) { librariesIndexed = false; DartCore.logError( "Invalid URI returned from the system library manager: \"" + urlSpec + "\"", exception); } catch (Exception exception) { librariesIndexed = false; DartCore.logError("Could not index bundled libraries", exception); } } if (DartCoreDebug.PERF_INDEX) { long endTime = System.currentTimeMillis(); DartCore.logInformation( "Initializing the index with information from bundled libraries took " + (endTime - startTime) + " ms (" + initIndexingTime + " ms indexing)"); } return librariesIndexed; }
@Override public void run() throws Exception { final PackageLibraryManager libraryManager = PackageLibraryManagerProvider.getPackageLibraryManager(); final CompilerConfiguration config = new DefaultCompilerConfiguration(DartCompilerUtilities.COMPILER_OPTIONS, libraryManager) { @Override public boolean incremental() { return true; } @Override public boolean resolveDespiteParseErrors() { return true; } }; DartArtifactProvider provider = new LocalArtifactProvider(RootArtifactProvider.getInstance()) { @Override protected boolean isOutOfDateInParent(Source source, Source base, String extension) { if (forceFullAST || equalUris(libraryManager, unitUri, source.getUri())) { return true; } if (parsedUnits != null && parsedUnits.containsKey(source.getUri())) { return true; } return super.isOutOfDateInParent(source, base, extension); } }; libraryResult = secureAnalyzeLibrary(librarySource, parsedUnits, config, provider, this); if (libraryResult != null && unitUri != null) { for (DartUnit unit : libraryResult.getUnits()) { Source source = unit.getSourceInfo().getSource(); if (source != null) { if (equalUris(libraryManager, unitUri, source.getUri())) { unitResult = unit; return; } } } } }
@Override public String getSourceLocationPath() { URI uri = URI.create(vmFrame.getLocation().getUrl()); // Resolve a package: reference. if (PackageLibraryManager.isPackageUri(uri) && DartCore.getPlugin().getPackageRootPref() != null) { uri = PackageLibraryManagerProvider.getPackageLibraryManager() .resolvePackageUri(vmFrame.getLocation().getUrl()); } if ("file".equals(uri.getScheme())) { return uri.getPath(); } else if (PackageLibraryManager.isDartUri(uri)) { return uri.toString(); } else { return "builtin:" + vmFrame.getLibraryId() + ":" + vmFrame.getLocation().getUrl(); } }
/** A synchronized call to {@link DartCompiler#analyzeLibrary} */ public static LibraryUnit secureAnalyzeLibrary( LibrarySource librarySource, final Map<URI, DartUnit> parsedUnits, final CompilerConfiguration config, DartArtifactProvider provider, DartCompilerListener listener) throws IOException { long start = System.currentTimeMillis(); if (!DartSdkManager.getManager().hasSdk()) { return null; } final PackageLibraryManager manager = PackageLibraryManagerProvider.getPackageLibraryManager(); AnalysisServer server = PackageLibraryManagerProvider.getDefaultAnalysisServer(); URI librarySourceUri = librarySource.getUri(); if (parsedUnits == null && !(librarySource instanceof LibraryWithSuppliedSources)) { // Resolve dart:<libname> to file URI before calling AnalysisServer URI libraryFileUri = manager.resolveDartUri(librarySourceUri); File libraryFile = new File(libraryFileUri.getPath()); LibraryUnit ret = server.getSavedContext().resolve(libraryFile, 30000); long elapsed = System.currentTimeMillis() - start; Instrumentation.metric("DartCompilerUtils-secureAnalyzeLibrary", elapsed).log(); Instrumentation.operation("DartCompilerUtils-secureAnalyzeLibrary", elapsed) .with("librarySource.Name", librarySource.getName()) .with("librarySource.LastModified", librarySource.getLastModified()) .log(); return ret; } // Resolve the specified library against all currently cached libraries final Map<URI, LibraryUnit> resolvedLibs = server.getSavedContext().getResolvedLibraries(50); resolvedLibs.remove(librarySourceUri); // Construct the selective cache SelectiveCache selectiveCache = new DartCompiler.SelectiveCache() { @Override public Map<URI, LibraryUnit> getResolvedLibraries() { return resolvedLibs; } @Override public DartUnit getUnresolvedDartUnit(DartSource dartSrc) { if (parsedUnits == null) { return null; } URI srcUri = dartSrc.getUri(); DartUnit parsedUnit = parsedUnits.remove(srcUri); if (parsedUnit != null) { return parsedUnit; } URI fileUri = manager.resolveDartUri(srcUri); return parsedUnits.remove(fileUri); } }; Map<URI, LibraryUnit> libMap; // All calls to DartC must be synchronized synchronized (compilerLock) { libMap = DartCompiler.analyzeLibraries( librarySource, selectiveCache, config, provider, listener, false); } long elapsed = System.currentTimeMillis() - start; Instrumentation.metric("DartCompilerUtils-secureAnalyzeLibrary", elapsed) .with("libMapHasResult", Boolean.toString(libMap != null)) .log(); Instrumentation.operation("DartCompilerUtils-secureAnalyzeLibrary", elapsed) .with("librarySource.Name", librarySource.getName()) .with("libMapHasResult", Boolean.toString(libMap != null)) .with("librarySource.LastModified", librarySource.getLastModified()) .log(); return libMap != null ? libMap.get(librarySourceUri) : null; }
/** * An in-memory {@link LibrarySource} wrapping an underlying library and containing {@link * DartURIStringSource} to represent in memory changes to on disk sources */ private static class LibraryWithSuppliedSources implements LibrarySource { private final LibrarySource wrappedSource; private final PackageLibraryManager libraryManager = PackageLibraryManagerProvider.getPackageLibraryManager(); private final Map<URI, String> suppliedSources; private LibraryWithSuppliedSources( LibrarySource wrappedSource, Map<URI, String> suppliedSources) { this.wrappedSource = wrappedSource; this.suppliedSources = suppliedSources; } @Override public boolean exists() { return wrappedSource.exists(); } @Override public LibrarySource getImportFor(String relPath) throws IOException { try { return wrappedSource.getImportFor(relPath); } catch (Exception exception) { DartCore.logInformation("Could not find import: '" + relPath + "'", exception); return null; } } @Override public long getLastModified() { return wrappedSource.getLastModified(); } @Override public String getName() { return wrappedSource.getName(); } @Override public DartSource getSourceFor(String relPath) { URI uri; try { uri = wrappedSource.getUri().resolve(new URI(null, null, relPath, null)); } catch (URISyntaxException e) { uri = null; } if (uri != null) { for (URI key : suppliedSources.keySet()) { if (equalUris(libraryManager, key, uri)) { String source = suppliedSources.get(key); return new DartURIStringSource(this, uri, relPath, source); } } } return wrappedSource.getSourceFor(relPath); } @Override public Reader getSourceReader() throws IOException { return wrappedSource.getSourceReader(); } @Override public String getUniqueIdentifier() { return wrappedSource.getUniqueIdentifier(); } @Override public URI getUri() { return wrappedSource.getUri(); } }
@Override public void run() throws Exception { final PackageLibraryManager libraryManager = PackageLibraryManagerProvider.getPackageLibraryManager(); final LibraryElement enclosingLibrary = cachedLibraries.get(librarySource.wrappedSource).getElement(); // Try to find the core library in the enclosing set of libraries, otherwise the typeAnalyzer // will be void of core types. LibraryUnit coreUnit; // All calls to DartC must be synchronized synchronized (compilerLock) { coreUnit = DartCompiler.getCoreLib(enclosingLibrary.getLibraryUnit()); } if (coreUnit == null) { throw new RuntimeException("Unable to locate core library"); } LibraryElement coreLibrary = coreUnit.getElement(); SourceDelta delta = new SourceDelta() { @Override public DartSource getSourceAfter() { return source; } @Override public Source getSourceBefore() { for (DartUnit u : enclosingLibrary.getLibraryUnit().getUnits()) { Source unitSource = u.getSourceInfo().getSource(); if (unitSource.getUri().equals(unitUri)) { return unitSource; } } return null; } @Override public DartUnit getUnitAfter() { return parsedUnit; } }; final CompilerConfiguration config = new DefaultCompilerConfiguration(DartCompilerUtilities.COMPILER_OPTIONS, libraryManager) { @Override public boolean incremental() { return true; } @Override public boolean resolveDespiteParseErrors() { return true; } @Override public boolean typeErrorsAreFatal() { return false; } @Override public boolean warningsAreFatal() { return false; } }; // All calls to DartC must be synchronized synchronized (compilerLock) { analyzedNode = DartCompiler.analyzeDelta( delta, enclosingLibrary, coreLibrary, completionNode, completionLocation, 0, config, this); } }