Example #1
0
 @Override
 public Void visitImportDirective(DartImportDirective node) {
   DartLibrary library = compilationUnit.getLibrary();
   try {
     if (Objects.equal(compilationUnit, library.getDefiningCompilationUnit())) {
       DartImport[] imports = library.getImports();
       for (DartImport imprt : imports) {
         // on URI of library - return defining Unit of imported Library
         SourceRange uriRange = imprt.getUriRange();
         if (SourceRangeUtils.contains(uriRange, startOffset)) {
           resolvedElement = null;
           foundElement = imprt.getLibrary().getDefiningCompilationUnit();
           wordRegion = new Region(uriRange.getOffset(), uriRange.getLength());
           candidateRegion = new Region(0, 0);
           throw new DartElementFoundException();
         }
         // on #import directive - return DartImport element
         SourceRange sourceRange = imprt.getSourceRange();
         if (SourceRangeUtils.contains(sourceRange, startOffset)) {
           resolvedElement = null;
           foundElement = imprt;
           wordRegion = new Region(sourceRange.getOffset(), sourceRange.getLength());
           candidateRegion = new Region(sourceRange.getOffset(), sourceRange.getLength());
           throw new DartElementFoundException();
         }
       }
     }
   } catch (DartModelException e) {
     DartCore.logError("Cannot access imports of " + library.getElementName(), e);
   }
   return super.visitImportDirective(node);
 }
 /** 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;
 }
  @Override
  public StyledString getStyledText(Object element) {
    if (element instanceof IResource) {
      IResource resource = (IResource) element;

      // Un-analyzed resources are grey.
      if (!DartCore.isAnalyzed(resource)) {
        return new StyledString(resource.getName(), StyledString.QUALIFIER_STYLER);
      }

      StyledString string = new StyledString(resource.getName());

      DartElement dartElement = DartCore.create(resource);

      // Append the library name to library units.
      if (dartElement instanceof CompilationUnit) {
        if (((CompilationUnit) dartElement).definesLibrary()) {
          DartLibrary library = ((CompilationUnit) dartElement).getLibrary();

          string.append(" [" + library.getDisplayName() + "]", StyledString.QUALIFIER_STYLER);
        }
      }

      return string;
    }

    return workbenchLabelProvider.getStyledText(element);
  }
 public void test_DartModelManager_openLibrary_nonLibrary() throws Exception {
   final File nonLibraryFile =
       TestUtilities.getPluginRelativePath(
               "com.google.dart.tools.core_test", new Path("test_data/Geometry/license.txt"))
           .toFile();
   final DartLibrary[] libraryHolder = new DartLibrary[1];
   ResourcesPlugin.getWorkspace()
       .run(
           new IWorkspaceRunnable() {
             @Override
             public void run(IProgressMonitor monitor) throws CoreException {
               libraryHolder[0] = DartModelManager.getInstance().openLibrary(nonLibraryFile, null);
             }
           },
           null);
   DartLibrary library = libraryHolder[0];
   assertNotNull(library);
   final DartElement parent = library.getParent();
   try {
     assertValidLibrary(library);
   } finally {
     ResourcesPlugin.getWorkspace()
         .run(
             new IWorkspaceRunnable() {
               @Override
               public void run(IProgressMonitor monitor) throws CoreException {
                 ((DartProject) parent).getProject().delete(true, true, monitor);
               }
             },
             null);
   }
 }
Example #5
0
 /**
  * Assert that the given library is valid.
  *
  * @param library the library to be tested
  * @throws DartModelException if the state of the library could not be determined
  */
 private void assertValidLibrary(DartLibrary library) throws DartModelException {
   assertNotNull(library);
   final DartElement parent = library.getParent();
   assertNotNull(parent);
   assertTrue(parent instanceof DartProject);
   assertEquals("Geometry", library.getDisplayName());
   assertTrue(library.getElementName().endsWith("test_data/Geometry/geometry.dart"));
   CompilationUnit[] units = library.getCompilationUnits();
   assertNotNull(units);
   assertEquals(2, units.length);
 }
Example #6
0
 @Override
 public Void visitStringLiteral(DartStringLiteral node) {
   if (foundElement == null) {
     int start = node.getSourceInfo().getOffset();
     int length = node.getSourceInfo().getLength();
     int end = start + length;
     if (end == 0) {
       return null;
     }
     if (start <= startOffset && end >= endOffset) {
       wordRegion = computeInternalStringRegion(start, length);
       DartNode parent = node.getParent();
       if (parent instanceof DartSourceDirective
           && ((DartSourceDirective) parent).getSourceUri() == node) {
         // resolvedElement = ((DartSourceDirective) parent).getElement();
         DartLibrary library = compilationUnit.getLibrary();
         String fileName = getFileName(library, node.getValue());
         CompilationUnit sourcedUnit = library.getCompilationUnit(fileName);
         if (sourcedUnit != null && sourcedUnit.exists()) {
           foundElement = sourcedUnit;
         }
       } else if (parent instanceof DartResourceDirective
           && ((DartResourceDirective) parent).getResourceUri() == node) {
         // resolvedElement = ((DartResourceDirective) parent).getElement();
         DartLibrary library = compilationUnit.getLibrary();
         try {
           DartSource unitSource = compilationUnit.getSourceRef();
           if (unitSource != null) {
             LibrarySource librarySource = unitSource.getLibrary();
             if (librarySource != null) {
               DartSource resourceSource = librarySource.getSourceFor(node.getValue());
               if (resourceSource != null) {
                 URI resourceUri = resourceSource.getUri();
                 DartResource resource = library.getResource(resourceUri);
                 if (resource != null && resource.exists()) {
                   foundElement = resource;
                 }
               }
             }
           }
         } catch (DartModelException exception) {
           foundElement = null;
         }
       }
       throw new DartElementFoundException();
     }
   }
   return null;
 }