Exemplo n.º 1
0
  private static void checkForUnexpectedErrors() {
    AnalyzeExhaust exhaust =
        WholeProjectAnalyzerFacade.analyzeProjectWithCacheOnAFile((JetFile) getFile());
    Collection<Diagnostic> diagnostics = exhaust.getBindingContext().getDiagnostics();

    if (diagnostics.size() != 0) {
      String[] expectedErrorStrings =
          InTextDirectivesUtils.findListWithPrefix("// ERROR:", getFile().getText());

      System.out.println(getFile().getText());

      Collection<String> expectedErrors = new HashSet<String>(Arrays.asList(expectedErrorStrings));

      StringBuilder builder = new StringBuilder();
      boolean hasErrors = false;

      for (Diagnostic diagnostic : diagnostics) {
        if (diagnostic.getSeverity() == Severity.ERROR) {
          String errorText = IdeErrorMessages.RENDERER.render(diagnostic);
          if (!expectedErrors.contains(errorText)) {
            hasErrors = true;
            builder.append("// ERROR: ").append(errorText).append("\n");
          }
        }
      }

      Assert.assertFalse(
          "There should be no unexpected errors after applying fix (Use \"// ERROR:\" directive): \n"
              + builder.toString(),
          hasErrors);
    }
  }
Exemplo n.º 2
0
  private static void registerDiagnosticAnnotations(
      @NotNull Diagnostic diagnostic,
      @NotNull Set<PsiElement> redeclarations,
      @NotNull final AnnotationHolder holder) {
    List<TextRange> textRanges = diagnostic.getTextRanges();
    if (diagnostic.getSeverity() == Severity.ERROR) {
      if (diagnostic.getFactory() == Errors.UNRESOLVED_IDE_TEMPLATE) {
        return;
      }
      if (diagnostic instanceof UnresolvedReferenceDiagnostic) {
        UnresolvedReferenceDiagnostic unresolvedReferenceDiagnostic =
            (UnresolvedReferenceDiagnostic) diagnostic;
        JetReferenceExpression referenceExpression = unresolvedReferenceDiagnostic.getPsiElement();
        PsiReference reference = referenceExpression.getReference();
        if (reference instanceof MultiRangeReference) {
          MultiRangeReference mrr = (MultiRangeReference) reference;
          for (TextRange range : mrr.getRanges()) {
            Annotation annotation =
                holder.createErrorAnnotation(
                    range.shiftRight(referenceExpression.getTextOffset()), diagnostic.getMessage());

            registerQuickFix(annotation, diagnostic);

            annotation.setHighlightType(ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
          }
        } else {
          for (TextRange textRange : textRanges) {
            Annotation annotation =
                holder.createErrorAnnotation(textRange, diagnostic.getMessage());
            registerQuickFix(annotation, diagnostic);
            annotation.setHighlightType(ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
          }
        }

        return;
      }

      if (diagnostic.getFactory() == Errors.ILLEGAL_ESCAPE_SEQUENCE) {
        for (TextRange textRange : diagnostic.getTextRanges()) {
          Annotation annotation = holder.createErrorAnnotation(textRange, diagnostic.getMessage());
          annotation.setTextAttributes(JetHighlightingColors.INVALID_STRING_ESCAPE);
        }
      }

      if (diagnostic instanceof RedeclarationDiagnostic) {
        RedeclarationDiagnostic redeclarationDiagnostic = (RedeclarationDiagnostic) diagnostic;
        registerQuickFix(
            markRedeclaration(redeclarations, redeclarationDiagnostic, holder), diagnostic);
        return;
      }

      // Generic annotation
      for (TextRange textRange : textRanges) {
        Annotation errorAnnotation =
            holder.createErrorAnnotation(textRange, getMessage(diagnostic));
        registerQuickFix(errorAnnotation, diagnostic);

        if (diagnostic.getFactory() == Errors.INVISIBLE_REFERENCE) {
          errorAnnotation.setHighlightType(ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
        }
      }
    } else if (diagnostic.getSeverity() == Severity.WARNING) {
      for (TextRange textRange : textRanges) {
        Annotation annotation = holder.createWarningAnnotation(textRange, getMessage(diagnostic));
        registerQuickFix(annotation, diagnostic);

        if (diagnostic.getFactory() instanceof UnusedElementDiagnosticFactory) {
          annotation.setHighlightType(ProblemHighlightType.LIKE_UNUSED_SYMBOL);
        }
      }
    }
  }