protected HighlightSeverity getSeverity(@NotNull RefElement element) {
    final PsiElement psiElement = element.getPointer().getContainingFile();
    if (psiElement != null) {
      final GlobalInspectionContextImpl context = getContext();
      final String shortName = getSeverityDelegateName();
      final Tools tools = context.getTools().get(shortName);
      if (tools != null) {
        for (ScopeToolState state : tools.getTools()) {
          InspectionToolWrapper toolWrapper = state.getTool();
          if (toolWrapper == getToolWrapper()) {
            return context
                .getCurrentProfile()
                .getErrorLevel(HighlightDisplayKey.find(shortName), psiElement)
                .getSeverity();
          }
        }
      }

      final InspectionProfile profile =
          InspectionProjectProfileManager.getInstance(context.getProject()).getInspectionProfile();
      final HighlightDisplayLevel level =
          profile.getErrorLevel(HighlightDisplayKey.find(shortName), psiElement);
      return level.getSeverity();
    }
    return null;
  }
  @Nullable
  private static HighlightInfo createAnnotationForRef(
      @NotNull GrReferenceExpression ref, boolean compileStatic, @NotNull String message) {
    PsiElement refNameElement = ref.getReferenceNameElement();
    assert refNameElement != null;

    HighlightDisplayLevel displayLevel = getHighlightDisplayLevel(ref.getProject(), ref);

    if (compileStatic || displayLevel == HighlightDisplayLevel.ERROR) {
      return HighlightInfo.newHighlightInfo(HighlightInfoType.WRONG_REF)
          .range(refNameElement)
          .descriptionAndTooltip(message)
          .create();
    }

    if (displayLevel == HighlightDisplayLevel.WARNING) {
      boolean isTestMode = ApplicationManager.getApplication().isUnitTestMode();
      HighlightInfoType infotype =
          isTestMode ? HighlightInfoType.WARNING : HighlightInfoType.INFORMATION;

      HighlightInfo.Builder builder =
          HighlightInfo.newHighlightInfo(infotype).range(refNameElement);
      builder.descriptionAndTooltip(message);
      return builder.needsUpdateOnTyping(false).textAttributes(UNRESOLVED_ACCESS).create();
    }

    HighlightInfoType highlightInfoType = HighlightInfo.convertSeverity(displayLevel.getSeverity());
    return HighlightInfo.newHighlightInfo(highlightInfoType)
        .range(refNameElement)
        .descriptionAndTooltip(message)
        .create();
  }
Exemplo n.º 3
0
  public Icon getRendererIconByIndex(int i) {
    final HighlightSeverity severity = getSeverityByIndex(i);
    HighlightDisplayLevel level = HighlightDisplayLevel.find(severity);
    if (level != null) {
      return level.getIcon();
    }

    return HighlightDisplayLevel.createIconByMask(myRendererColors.get(severity.getName()));
  }
 @Override
 @NotNull
 public HighlightSeverity getSeverity(final PsiElement psiElement) {
   InspectionProfile profile =
       psiElement == null
           ? (InspectionProfile) InspectionProfileManager.getInstance().getRootProfile()
           : InspectionProjectProfileManager.getInstance(psiElement.getProject())
               .getInspectionProfile();
   HighlightDisplayLevel level = profile.getErrorLevel(myToolKey, psiElement);
   LOG.assertTrue(level != HighlightDisplayLevel.DO_NOT_SHOW);
   return level.getSeverity();
 }
 public HighlightDisplayLevel getErrorLevel(
     @NotNull HighlightDisplayKey inspectionToolKey, PsiElement element) {
   final ToolsImpl tools = getTools(inspectionToolKey.toString());
   HighlightDisplayLevel level =
       tools != null ? tools.getLevel(element) : HighlightDisplayLevel.WARNING;
   if (!((SeverityProvider) getProfileManager())
       .getOwnSeverityRegistrar()
       .isSeverityValid(level.getSeverity().toString())) {
     level = HighlightDisplayLevel.WARNING;
     setErrorLevel(inspectionToolKey, level);
   }
   return level;
 }
 @NotNull
 public HighlightDisplayLevel getDefaultLevel() {
   if (level == null) return HighlightDisplayLevel.WARNING;
   HighlightDisplayLevel displayLevel = HighlightDisplayLevel.find(level);
   if (displayLevel == null) {
     LOG.error("Can't find highlight display level: " + level);
     return HighlightDisplayLevel.WARNING;
   }
   return displayLevel;
 }
Exemplo n.º 7
0
 public void registerSeverity(@NotNull SeverityBasedTextAttributes info, Color renderColor) {
   final HighlightSeverity severity = info.getType().getSeverity(null);
   myMap.put(severity.getName(), info);
   if (renderColor != null) {
     myRendererColors.put(severity.getName(), renderColor);
   }
   myOrderMap = null;
   HighlightDisplayLevel.registerSeverity(
       severity, getHighlightInfoTypeBySeverity(severity).getAttributesKey());
   severitiesChanged();
 }
Exemplo n.º 8
0
  void readExternal(
      @NotNull Element toolElement,
      @NotNull InspectionProfile profile,
      Map<String, List<String>> dependencies)
      throws InvalidDataException {
    final String levelName = toolElement.getAttributeValue(LEVEL_ATTRIBUTE);
    final ProfileManager profileManager = profile.getProfileManager();
    final SeverityRegistrar registrar =
        ((SeverityProvider) profileManager).getOwnSeverityRegistrar();
    HighlightDisplayLevel level =
        levelName != null ? HighlightDisplayLevel.find(registrar.getSeverity(levelName)) : null;
    if (level == null) {
      level = HighlightDisplayLevel.WARNING;
    }
    myDefaultState.setLevel(level);
    final String enabled = toolElement.getAttributeValue(ENABLED_ATTRIBUTE);
    final boolean isEnabled = enabled != null && Boolean.parseBoolean(enabled);

    final String enabledTool = toolElement.getAttributeValue(ENABLED_BY_DEFAULT_ATTRIBUTE);
    myDefaultState.setEnabled(enabledTool != null ? Boolean.parseBoolean(enabledTool) : isEnabled);
    final InspectionToolWrapper toolWrapper = myDefaultState.getTool();

    final List scopeElements = toolElement.getChildren(ProfileEx.SCOPE);
    final List<String> scopeNames = new ArrayList<String>();
    for (Object sO : scopeElements) {
      final Element scopeElement = (Element) sO;
      final String scopeName = scopeElement.getAttributeValue(ProfileEx.NAME);
      if (scopeName == null) {
        continue;
      }
      final NamedScopesHolder scopesHolder = profileManager.getScopesManager();
      NamedScope namedScope = null;
      if (scopesHolder != null) {
        namedScope = scopesHolder.getScope(scopeName);
      }
      final String errorLevel = scopeElement.getAttributeValue(LEVEL_ATTRIBUTE);
      final String enabledInScope = scopeElement.getAttributeValue(ENABLED_ATTRIBUTE);
      final InspectionToolWrapper copyToolWrapper = toolWrapper.createCopy();
      // check if unknown children exists
      if (scopeElement.getAttributes().size() > 3 || !scopeElement.getChildren().isEmpty()) {
        copyToolWrapper.getTool().readSettings(scopeElement);
      }
      HighlightDisplayLevel scopeLevel =
          errorLevel != null ? HighlightDisplayLevel.find(registrar.getSeverity(errorLevel)) : null;
      if (scopeLevel == null) {
        scopeLevel = level;
      }
      if (namedScope != null) {
        addTool(
            namedScope,
            copyToolWrapper,
            enabledInScope != null && Boolean.parseBoolean(enabledInScope),
            scopeLevel);
      } else {
        addTool(
            scopeName,
            copyToolWrapper,
            enabledInScope != null && Boolean.parseBoolean(enabledInScope),
            scopeLevel);
      }

      scopeNames.add(scopeName);
    }

    for (int i = 0; i < scopeNames.size(); i++) {
      String scopeName = scopeNames.get(i);
      List<String> order = dependencies.get(scopeName);
      if (order == null) {
        order = new ArrayList<String>();
        dependencies.put(scopeName, order);
      }
      for (int j = i + 1; j < scopeNames.size(); j++) {
        order.add(scopeNames.get(j));
      }
    }

    // check if unknown children exists
    if (toolElement.getAttributes().size() > 4
        || toolElement.getChildren().size() > scopeElements.size()) {
      toolWrapper.getTool().readSettings(toolElement);
    }

    myEnabled = isEnabled;
  }
  private void exportResults(
      @NotNull final CommonProblemDescriptor[] descriptors,
      @NotNull RefEntity refEntity,
      @NotNull Element parentNode) {
    for (CommonProblemDescriptor descriptor : descriptors) {
      @NonNls final String template = descriptor.getDescriptionTemplate();
      int line =
          descriptor instanceof ProblemDescriptor
              ? ((ProblemDescriptor) descriptor).getLineNumber()
              : -1;
      final PsiElement psiElement =
          descriptor instanceof ProblemDescriptor
              ? ((ProblemDescriptor) descriptor).getPsiElement()
              : null;
      @NonNls
      String problemText =
          StringUtil.replace(
              StringUtil.replace(
                  template,
                  "#ref",
                  psiElement != null
                      ? ProblemDescriptorUtil.extractHighlightedText(descriptor, psiElement)
                      : ""),
              " #loc ",
              " ");

      Element element = refEntity.getRefManager().export(refEntity, parentNode, line);
      if (element == null) return;
      @NonNls
      Element problemClassElement =
          new Element(InspectionsBundle.message("inspection.export.results.problem.element.tag"));
      problemClassElement.addContent(myToolWrapper.getDisplayName());

      final HighlightSeverity severity;
      if (refEntity instanceof RefElement) {
        final RefElement refElement = (RefElement) refEntity;
        severity = getSeverity(refElement);
      } else {
        final InspectionProfile profile =
            InspectionProjectProfileManager.getInstance(getContext().getProject())
                .getInspectionProfile();
        final HighlightDisplayLevel level =
            profile.getErrorLevel(
                HighlightDisplayKey.find(myToolWrapper.getShortName()), psiElement);
        severity = level.getSeverity();
      }

      if (severity != null) {
        ProblemHighlightType problemHighlightType =
            descriptor instanceof ProblemDescriptor
                ? ((ProblemDescriptor) descriptor).getHighlightType()
                : ProblemHighlightType.GENERIC_ERROR_OR_WARNING;
        final String attributeKey =
            getTextAttributeKey(getRefManager().getProject(), severity, problemHighlightType);
        problemClassElement.setAttribute("severity", severity.myName);
        problemClassElement.setAttribute("attribute_key", attributeKey);
      }

      element.addContent(problemClassElement);
      if (myToolWrapper instanceof GlobalInspectionToolWrapper) {
        final GlobalInspectionTool globalInspectionTool =
            ((GlobalInspectionToolWrapper) myToolWrapper).getTool();
        final QuickFix[] fixes = descriptor.getFixes();
        if (fixes != null) {
          @NonNls Element hintsElement = new Element("hints");
          for (QuickFix fix : fixes) {
            final String hint = globalInspectionTool.getHint(fix);
            if (hint != null) {
              @NonNls Element hintElement = new Element("hint");
              hintElement.setAttribute("value", hint);
              hintsElement.addContent(hintElement);
            }
          }
          element.addContent(hintsElement);
        }
      }
      try {
        Element descriptionElement =
            new Element(InspectionsBundle.message("inspection.export.results.description.tag"));
        descriptionElement.addContent(problemText);
        element.addContent(descriptionElement);
      } catch (IllegalDataException e) {
        //noinspection HardCodedStringLiteral,UseOfSystemOutOrSystemErr
        System.out.println(
            "Cannot save results for "
                + refEntity.getName()
                + ", inspection which caused problem: "
                + myToolWrapper.getShortName());
      }
    }
  }