Ejemplo n.º 1
0
  /** A synchronized call to {@link DartCompiler#analyzeLibraries} */
  public static Map<URI, LibraryUnit> secureAnalyzeLibraries(
      LibrarySource librarySource,
      SelectiveCache selectiveCache,
      CompilerConfiguration config,
      DartArtifactProvider provider,
      DartCompilerListener listener,
      boolean resolveAllNewLibs)
      throws IOException {
    // All calls to DartC must be synchronized

    long start = System.currentTimeMillis();
    Map<URI, LibraryUnit> ret;

    synchronized (compilerLock) {
      ret =
          DartCompiler.analyzeLibraries(
              librarySource, selectiveCache, config, provider, listener, resolveAllNewLibs);
    }

    long elapsed = System.currentTimeMillis() - start;
    Instrumentation.metric("DartCompilerUtils-secureAnalyzeLibraries", elapsed).log();
    Instrumentation.operation("DartCompilerUtils-secureAnalyzeLibraries", elapsed)
        .with("librarySource.Name", librarySource.getName())
        .with("librarySource.LastModified", librarySource.getLastModified())
        .log();

    return ret;
  }
Ejemplo n.º 2
0
  /**
   * Parse the compilation units in the specified library. Any exceptions thrown by the {@link
   * DartParser} will be logged and a {@link DartModelException} thrown.
   *
   * @param library the library to be parsed (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
   * @throws DartModelException if the library could not be parsed
   */
  public static LibraryUnit resolveLibrary(
      LibrarySource library,
      Collection<DartUnit> suppliedUnits,
      final Collection<DartCompilationError> parseErrors)
      throws DartModelException {

    long start = System.currentTimeMillis();

    ResolverRunnable runnable =
        new ResolverRunnable(library, createMap(suppliedUnits), false, parseErrors);
    runnable.runSafe();

    long elapsed = System.currentTimeMillis() - start;
    Instrumentation.metric("DartCompilerUtils-resolveLibrary", elapsed).log();
    Instrumentation.operation("DartCompilerUtils-resolveLibrary", elapsed)
        .with("librarySource.Name", library.getName())
        .with("librarySource.LastModified", library.getLastModified())
        .log();

    if (runnable.exception != null) {
      throw new DartModelException(
          new CoreException(
              new Status(
                  IStatus.ERROR,
                  DartCore.PLUGIN_ID,
                  "Failed to parse " + library.getName(),
                  runnable.exception)));
    }
    return runnable.libraryResult;
  }
Ejemplo n.º 3
0
  /**
   * Parse the specified source. Any exceptions thrown by the {@link DartParser} will be logged and
   * a {@link DartModelException} thrown.
   *
   * @param librarySource the source for the library containing the compilation unit being parsed
   * @param unitUri the URI of the compilation unit being parsed
   * @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 resolveUnit(
      LibrarySource librarySource,
      URI unitUri,
      Map<URI, String> suppliedSources,
      final Collection<DartCompilationError> parseErrors)
      throws DartModelException {

    long start = System.currentTimeMillis();

    ResolverRunnable runnable =
        new ResolverRunnable(librarySource, unitUri, suppliedSources, false, parseErrors);
    runnable.runSafe();

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

    Instrumentation.operation("DartCompilerUtils-resolveUnit", elapsed)
        .with("librarySource-ElementName", librarySource.getName())
        .with("librarySource.LastModified", librarySource.getLastModified())
        .log();

    if (runnable.exception != null) {
      throw new DartModelException(
          new CoreException(
              new Status(
                  IStatus.ERROR,
                  DartCore.PLUGIN_ID,
                  "Failed to parse " + unitUri,
                  runnable.exception)));
    }
    return runnable.unitResult;
  }
Ejemplo n.º 4
0
  /**
   * Parse the specified source. Any exceptions thrown by the {@link DartParser} will be logged and
   * a {@link DartModelException} thrown.
   *
   * @param sourceRef the Dart source being parsed
   * @param source the source to be parsed (not <code>null</code>)
   * @param preserveComments <code>true</code> if comments are to be preserved
   * @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 parseSource(
      DartSource sourceRef,
      String source,
      boolean preserveComments,
      Collection<DartCompilationError> parseErrors)
      throws DartModelException {

    long start = System.currentTimeMillis();

    ParserRunnable runnable = new ParserRunnable(sourceRef, source, preserveComments, parseErrors);
    runnable.runSafe();

    long elapsed = System.currentTimeMillis() - start;
    Instrumentation.metric("DartCompilerUtils-parseSource", elapsed).log();
    Instrumentation.operation("DartCompilerUtils-parseSource", elapsed)
        .with("sourceRef.Name", sourceRef.getName())
        .with("source", source)
        .with("parseErrors", parseErrors != null ? parseErrors.size() : 0)
        .log();

    if (runnable.exception != null) {
      throw new DartModelException(
          new CoreException(
              new Status(
                  IStatus.ERROR,
                  DartCore.PLUGIN_ID,
                  "Failed to parse " + sourceRef.getName(),
                  runnable.exception)));
    }
    return runnable.result;
  }
Ejemplo n.º 5
0
  /**
   * A synchronized call to {@link DartCompiler#compileLib(LibrarySource, CompilerConfiguration,
   * DartArtifactProvider, DartCompilerListener)}
   */
  public static void secureCompileLib(
      LibrarySource libSource,
      CompilerConfiguration config,
      DartArtifactProvider provider,
      DartCompilerListener listener)
      throws IOException {

    long start = System.currentTimeMillis();

    if (!DartSdkManager.getManager().hasSdk()) {
      return;
    }

    List<LibrarySource> embeddedLibraries = new ArrayList<LibrarySource>();
    // All calls to DartC must be synchronized
    synchronized (compilerLock) {
      DartCompiler.compileLib(libSource, embeddedLibraries, config, provider, listener);
    }

    long elapsed = System.currentTimeMillis() - start;
    Instrumentation.metric("DartCompilerUtils-secureCompileLib", elapsed).log();

    Instrumentation.operation("DartCompilerUtils-secureCompileLib", elapsed)
        .with("librarySource.Name", libSource.getName())
        .log();
  }
Ejemplo n.º 6
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;
  }
Ejemplo n.º 7
0
  /** A synchronized call to {@link DartParser#parseUnit(DartSource)} */
  public static DartUnit secureParseUnit(DartParser parser, DartSource sourceRef) {

    long start = System.currentTimeMillis();

    // All calls to DartC must be synchronized
    synchronized (compilerLock) {
      DartUnit ret = parser.parseUnit();

      long elapsed = System.currentTimeMillis() - start;
      Instrumentation.metric("DartCompilerUtils-secureParseUnit", elapsed).log();

      Instrumentation.operation("DartCompilerUtils-secureParseUnit", elapsed)
          .with("sourceRef.Name", sourceRef != null ? sourceRef.getName() : "null")
          .log();

      return ret;
    }
  }
  @Override
  public void apply(IDocument document, char trigger, int offset) {

    InstrumentationBuilder instrumentation = Instrumentation.builder("CompletionProposal-Apply");
    instrumentation.metric("Trigger", trigger);

    try {
      if (isSupportingRequiredProposals()) {
        CompletionProposal coreProposal = ((MemberProposalInfo) getProposalInfo()).fProposal;
        CompletionProposal[] requiredProposals = coreProposal.getRequiredProposals();
        for (int i = 0; requiredProposals != null && i < requiredProposals.length; i++) {
          int oldLen = document.getLength();
          if (requiredProposals[i].getKind() == CompletionProposal.TYPE_REF) {
            LazyDartCompletionProposal proposal =
                createRequiredTypeCompletionProposal(requiredProposals[i], fInvocationContext);
            proposal.apply(document);
            setReplacementOffset(getReplacementOffset() + document.getLength() - oldLen);
          } else {
            /*
             * we only support the above required proposals, see
             * CompletionProposal#getRequiredProposals()
             */
            Assert.isTrue(false);
          }
        }
      }

      try {
        boolean isSmartTrigger = isSmartTrigger(trigger);
        instrumentation.metric("isSmartTrigger", isSmartTrigger);

        String replacement;
        if (isSmartTrigger || trigger == (char) 0) {
          replacement = getReplacementString();
        } else {
          StringBuffer buffer = new StringBuffer(getReplacementString());

          // fix for PR #5533. Assumes that no eating takes place.
          if ((getCursorPosition() > 0
              && getCursorPosition() <= buffer.length()
              && buffer.charAt(getCursorPosition() - 1) != trigger)) {
            buffer.insert(getCursorPosition(), trigger);
            setCursorPosition(getCursorPosition() + 1);
          }

          replacement = buffer.toString();
          setReplacementString(replacement);
        }

        instrumentation.data("Replacement", replacement);

        // reference position just at the end of the document change.
        int referenceOffset = getReplacementOffset() + getReplacementLength();
        final ReferenceTracker referenceTracker = new ReferenceTracker();
        referenceTracker.preReplace(document, referenceOffset);

        replace(document, getReplacementOffset(), getReplacementLength(), replacement);

        referenceOffset = referenceTracker.postReplace(document);
        int delta = replacement == null ? 0 : replacement.length();
        if (delta > 0 && replacement.charAt(replacement.length() - 1) == ']') {
          delta += 1;
        }
        setReplacementOffset(referenceOffset - delta);

        // PR 47097
        if (isSmartTrigger) {
          handleSmartTrigger(document, trigger, referenceOffset);
        }

      } catch (BadLocationException x) {
        instrumentation.metric("Problem", "BadLocationException");
        // ignore
      }
    } finally {
      instrumentation.log();
    }
  }
Ejemplo n.º 9
0
  /** 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;
  }
Ejemplo n.º 10
0
  public static DartNode analyzeDelta(
      LibrarySource library,
      String sourceString,
      DartUnit suppliedUnit,
      DartNode completionNode,
      int completionLocation,
      final Collection<DartCompilationError> parseErrors)
      throws DartModelException {

    long start = System.currentTimeMillis();

    if (cachedLibraries.get(library) == null) {
      Collection<DartUnit> parsedUnits = new ArrayList<DartUnit>();
      parsedUnits.add(suppliedUnit);
      LibraryUnit resolvedLib = resolveLibrary(library, parsedUnits, parseErrors);
      synchronized (cachedLibraries) {
        LibraryUnit newLib = cachedLibraries.get(library);
        if (newLib == null) {
          cachedLibraries.put(library, resolvedLib);
        }
      }

      long elapsed = System.currentTimeMillis() - start;
      Instrumentation.metric("DartCompilerUtils-analyzeDelta", elapsed)
          .with("Cached", "false")
          .with("ParsedUnits", parsedUnits.size())
          .with("ParseErrors", parseErrors != null ? parseErrors.size() : 0)
          .with("sourceString-length", sourceString.length())
          .with("CachedLibraries", "false")
          .log();

      Instrumentation.operation("DartCompilerUtils-analyzeDelta", elapsed)
          .with("analyzeDelta.Name", library.getName())
          .with("librarySource.LastModified", library.getLastModified())
          .with("sourceString", sourceString)
          .with("sourceString-length", sourceString.length())
          .with("CachedLibraries", "true")
          .log();

      return completionNode;
    }
    DartSource src = (DartSource) suppliedUnit.getSourceInfo().getSource();
    URI unitUri = src.getUri();
    Map<URI, String> suppliedSources = new HashMap<URI, String>();
    suppliedSources.put(unitUri, sourceString);
    DeltaAnalysisRunnable runnable =
        new DeltaAnalysisRunnable(
            library,
            unitUri,
            suppliedSources,
            suppliedUnit,
            completionNode,
            completionLocation,
            parseErrors);
    runnable.runSafe();

    long elapsed = System.currentTimeMillis() - start;
    Instrumentation.metric("DartCompilerUtils-analyzeDelta", elapsed)
        .with("Cached", "false")
        .with("ParseErrors", parseErrors != null ? parseErrors.size() : 0)
        .with("sourceString-length", sourceString.length())
        .with("CachedLibraries", "false")
        .log();

    Instrumentation.operation("DartCompilerUtils-analyzeDelta", elapsed)
        .with("analyzeDelta.Name", library.getName())
        .with("librarySource.LastModified", library.getLastModified())
        .with("sourceString", sourceString)
        .with("sourceString-length", sourceString.length())
        .with("CachedLibraries", "true")
        .log();

    if (runnable.exception != null) {
      throw new DartModelException(
          new CoreException(
              new Status(
                  IStatus.ERROR,
                  DartCore.PLUGIN_ID,
                  "Failed to parse " + library.getName(),
                  runnable.exception)));
    }
    return runnable.analyzedNode;
  }