예제 #1
0
 /**
  * Parse the source for the specified compilation unit. Any exceptions thrown by the {@link
  * DartParser} will be logged and a {@link DartModelException} thrown.
  *
  * @param compilationUnit the compilation unit (not <code>null</code>)
  * @param parseErrors a collection to which parse errors are appended or <code>null</code> if
  *     parse errors should be ignored
  * @return the parse result
  */
 public static DartUnit parseUnit(
     CompilationUnit compilationUnit, Collection<DartCompilationError> parseErrors)
     throws DartModelException {
   String name = compilationUnit.getElementName();
   String source = compilationUnit.getSource();
   return parseSource(name, source, parseErrors);
 }
 @Override
 public void setInput(RefactoringStatusContext context) {
   if (context instanceof DartStatusContext) {
     DartStatusContext jsc = (DartStatusContext) context;
     IDocument document = null;
     {
       CompilationUnit cunit = jsc.getCompilationUnit();
       if (cunit.isWorkingCopy()) {
         try {
           document = newDocument(cunit.getSource());
         } catch (DartModelException e) {
           // document is null which is a valid input.
         }
       } else {
         IEditorInput editorInput = new FileEditorInput((IFile) cunit.getResource());
         document =
             getDocument(
                 DartToolsPlugin.getDefault().getCompilationUnitDocumentProvider(), editorInput);
       }
       if (document == null) {
         document = new Document(RefactoringMessages.DartStatusContextViewer_no_source_available);
       }
       updateTitle(cunit);
     }
     setInput(document, createRegion(jsc.getSourceRange()));
   } else if (context instanceof DartStringStatusContext) {
     updateTitle(null);
     DartStringStatusContext sc = (DartStringStatusContext) context;
     setInput(newDocument(sc.getSource()), createRegion(sc.getSourceRange()));
   }
 }
예제 #3
0
  /**
   * Parse the source for the specified compilation unit and resolve any elements found in it. Any
   * exceptions thrown by the {@link DartParser} will be added to the given collection.
   *
   * @param compilationUnit the compilation unit (not <code>null</code>)
   * @param parseErrors a collection to which parse errors are added or <code>null</code> if parse
   *     errors should be ignored
   * @return the parse result
   */
  public static DartUnit resolveUnit(
      CompilationUnit compilationUnit, Collection<DartCompilationError> parseErrors)
      throws DartModelException {

    long start = System.currentTimeMillis();

    DartLibraryImpl library = (DartLibraryImpl) compilationUnit.getLibrary();
    if (library == null) {
      // If we cannot get the library, we cannot resolve any elements so we
      // revert to simply parsing the compilation unit.
      DartUnit ret = parseUnit(compilationUnit, parseErrors);

      long elapsed = System.currentTimeMillis() - start;
      Instrumentation.metric("DartCompilerUtils-resolveUnit", elapsed)
          .with("ParseOnlyFallback", "true")
          .log();

      Instrumentation.operation("DartCompilerUtils-resolveUnit", elapsed)
          .with("ParseOnlyFallback", "true")
          .with("CompilaitonUnit-ElementName", compilationUnit.getElementName())
          .log();

      return ret;
    }
    IResource resource = compilationUnit.getResource();
    URI unitUri = null;
    if (resource != null) {
      unitUri = resource.getLocationURI();
    }
    if (unitUri == null && compilationUnit instanceof ExternalCompilationUnitImpl) {
      unitUri = ((ExternalCompilationUnitImpl) compilationUnit).getUri();
    }
    if (unitUri == null) {
      unitUri = ((CompilationUnitImpl) compilationUnit).getSourceRef().getUri();
    }
    String unitSource = compilationUnit.getSource();
    Map<URI, String> suppliedSources = new HashMap<URI, String>();
    if (unitSource != null) {
      suppliedSources.put(unitUri, unitSource);
    }
    DartUnit ret =
        resolveUnit(library.getLibrarySourceFile(), unitUri, suppliedSources, parseErrors);

    long elapsed = System.currentTimeMillis() - start;
    Instrumentation.metric("DartCompilerUtils-resolveUnit", elapsed)
        .with("ParseOnlyFallback", "false")
        .log();

    Instrumentation.operation("DartCompilerUtils-resolveUnit", elapsed)
        .with("ParseOnlyFallback", "false")
        .with("CompilaitonUnit-ElementName", compilationUnit.getElementName())
        .with("compilationUnit.LastModified", compilationUnit.getModificationStamp())
        .log();

    return ret;
  }
 /** Test for {@link DartFunction#getVisibleRange()} for local functions. */
 public void test_getVisibleRange() throws Exception {
   TestProject testProject = new TestProject("Test");
   try {
     CompilationUnit unit =
         testProject.setUnitContent(
             "Test.dart",
             Joiner.on("\n")
                 .join(
                     "// filler filler filler filler filler filler filler filler filler filler",
                     "f() {",
                     "  v1() {};",
                     "  {",
                     "    v2() {};",
                     "  }//marker1",
                     "  v3() {};",
                     "}//marker2",
                     ""));
     String source = unit.getSource();
     DartFunction globalFunction = (DartFunction) unit.getChildren()[0];
     DartElement[] functions = globalFunction.getChildren();
     assertThat(functions).hasSize(3);
     // v1
     {
       DartFunction f = (DartFunction) functions[0];
       SourceRange range = f.getVisibleRange();
       assertEquals("v1", f.getElementName());
       assertEquals(source.indexOf("v1"), range.getOffset());
       assertEquals(source.indexOf("marker2") - 1, range.getOffset() + range.getLength());
     }
     // v2
     {
       DartFunction f = (DartFunction) functions[1];
       SourceRange range = f.getVisibleRange();
       assertEquals("v2", f.getElementName());
       assertEquals(source.indexOf("v2"), range.getOffset());
       assertEquals(source.indexOf("marker1") - 1, range.getOffset() + range.getLength());
     }
     // v3
     {
       DartFunction f = (DartFunction) functions[2];
       SourceRange range = f.getVisibleRange();
       assertEquals("v3", f.getElementName());
       assertEquals(source.indexOf("v3"), range.getOffset());
       assertEquals(source.indexOf("marker2") - 1, range.getOffset() + range.getLength());
     }
   } finally {
     testProject.dispose();
   }
 }
 /** Asserts that {@link CompilationUnit} has expected content. */
 protected static void assertUnitContent(CompilationUnit unit, String... lines) throws Exception {
   assertEquals(makeSource(lines), unit.getSource());
 }
 /** Prints lines of code to insert into {@link #assertUnitContent(String...)}. */
 protected static void printUnitLinesSource(CompilationUnit unit) throws Exception {
   String source = unit.getSource();
   Iterable<String> lines = Splitter.on('\n').split(source);
   System.out.println(getLinesForSource(lines));
 }
 /** @return the offset of given <code>search</code> string. Fails test if not found. */
 protected static int findOffset(CompilationUnit unit, String search) throws Exception {
   String source = unit.getSource();
   int offset = source.indexOf(search);
   assertThat(offset).describedAs(source).isNotEqualTo(-1);
   return offset;
 }