示例#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);
    }
  }
示例#2
0
  @Override
  public void annotate(@NotNull PsiElement element, @NotNull final AnnotationHolder holder) {
    for (HighlightingVisitor visitor : getBeforeAnalysisVisitors(holder)) {
      element.accept(visitor);
    }

    if (element instanceof JetFile) {
      JetFile file = (JetFile) element;

      try {
        BindingContext bindingContext =
            WholeProjectAnalyzerFacade.analyzeProjectWithCacheOnAFile(file).getBindingContext();

        if (errorReportingEnabled) {
          Collection<Diagnostic> diagnostics =
              Sets.newLinkedHashSet(bindingContext.getDiagnostics());
          Set<PsiElement> redeclarations = Sets.newHashSet();
          for (Diagnostic diagnostic : diagnostics) {
            // This is needed because we have the same context for all files
            if (diagnostic.getPsiFile() != file) continue;

            registerDiagnosticAnnotations(diagnostic, redeclarations, holder);
          }
        }

        for (HighlightingVisitor visitor : getAfterAnalysisVisitor(holder, bindingContext)) {
          file.acceptChildren(visitor);
        }
      } catch (ProcessCanceledException e) {
        throw e;
      } catch (AssertionError e) {
        // For failing tests and to notify about idea internal error in -ea mode
        holder.createErrorAnnotation(
            element, e.getClass().getCanonicalName() + ": " + e.getMessage());
        throw e;
      } catch (Throwable e) {
        // TODO
        holder.createErrorAnnotation(
            element, e.getClass().getCanonicalName() + ": " + e.getMessage());
        e.printStackTrace();
      }
    }
  }
  private static JetValueArgumentList findCall(CreateParameterInfoContext context) {
    // todo: calls to this constructors, when we will have auxiliary constructors
    PsiFile file = context.getFile();
    if (!(file instanceof JetFile)) {
      return null;
    }

    JetValueArgumentList argumentList =
        PsiTreeUtil.getParentOfType(
            file.findElementAt(context.getOffset()), JetValueArgumentList.class);
    if (argumentList == null) {
      return null;
    }

    JetSimpleNameExpression callNameExpression = getCallSimpleNameExpression(argumentList);
    if (callNameExpression == null) {
      return null;
    }

    PsiReference[] references = callNameExpression.getReferences();
    if (references.length == 0) {
      return null;
    }

    CancelableResolveSession resolveSession =
        WholeProjectAnalyzerFacade.getLazyResolveResultForFile(
            (JetFile) callNameExpression.getContainingFile());
    BindingContext bindingContext = resolveSession.resolveToElement(callNameExpression);

    JetScope scope = bindingContext.get(BindingContext.RESOLUTION_SCOPE, callNameExpression);
    DeclarationDescriptor placeDescriptor = null;
    if (scope != null) {
      placeDescriptor = scope.getContainingDeclaration();
    }

    Collection<DeclarationDescriptor> variants =
        TipsManager.getReferenceVariants(callNameExpression, bindingContext);

    Name refName = callNameExpression.getReferencedNameAsName();

    Collection<Pair<? extends DeclarationDescriptor, CancelableResolveSession>> itemsToShow =
        new ArrayList<Pair<? extends DeclarationDescriptor, CancelableResolveSession>>();
    for (DeclarationDescriptor variant : variants) {
      if (variant instanceof FunctionDescriptor) {
        FunctionDescriptor functionDescriptor = (FunctionDescriptor) variant;
        if (functionDescriptor.getName().equals(refName)) {
          // todo: renamed functions?
          if (placeDescriptor != null
              && !JetVisibilityChecker.isVisible(placeDescriptor, functionDescriptor)) {
            continue;
          }
          itemsToShow.add(Pair.create(functionDescriptor, resolveSession));
        }
      } else if (variant instanceof ClassDescriptor) {
        ClassDescriptor classDescriptor = (ClassDescriptor) variant;
        if (classDescriptor.getName().equals(refName)) {
          // todo: renamed classes?
          for (ConstructorDescriptor constructorDescriptor : classDescriptor.getConstructors()) {
            if (placeDescriptor != null
                && !JetVisibilityChecker.isVisible(placeDescriptor, constructorDescriptor)) {
              continue;
            }
            itemsToShow.add(Pair.create(constructorDescriptor, resolveSession));
          }
        }
      }
    }

    context.setItemsToShow(ArrayUtil.toObjectArray(itemsToShow));
    return argumentList;
  }