@Override
 public void ignoreProblem(RefEntity refEntity, CommonProblemDescriptor problem, int idx) {
   if (refEntity == null) return;
   final Set<QuickFix> localQuickFixes = getQuickFixActions().get(refEntity);
   final QuickFix[] fixes = problem.getFixes();
   if (isIgnoreProblem(fixes, localQuickFixes, idx)) {
     getProblemToElements().remove(problem);
     Map<RefEntity, CommonProblemDescriptor[]> problemElements = getProblemElements();
     synchronized (lock) {
       CommonProblemDescriptor[] descriptors = problemElements.get(refEntity);
       if (descriptors != null) {
         ArrayList<CommonProblemDescriptor> newDescriptors =
             new ArrayList<CommonProblemDescriptor>(Arrays.asList(descriptors));
         newDescriptors.remove(problem);
         getQuickFixActions().put(refEntity, null);
         if (!newDescriptors.isEmpty()) {
           problemElements.put(
               refEntity,
               newDescriptors.toArray(new CommonProblemDescriptor[newDescriptors.size()]));
           for (CommonProblemDescriptor descriptor : newDescriptors) {
             collectQuickFixes(descriptor.getFixes(), refEntity);
           }
         } else {
           ignoreProblemElement(refEntity);
         }
       }
     }
   }
 }
 protected void addProblemElement(
     RefEntity refElement,
     boolean filterSuppressed,
     @NotNull CommonProblemDescriptor... descriptions) {
   if (refElement == null) return;
   if (descriptions.length == 0) return;
   if (filterSuppressed) {
     if (ourOutputPath == null || !(this instanceof LocalInspectionToolWrapper)) {
       synchronized (lock) {
         Map<RefEntity, CommonProblemDescriptor[]> problemElements = getProblemElements();
         CommonProblemDescriptor[] problems = problemElements.get(refElement);
         problems =
             problems == null
                 ? descriptions
                 : ArrayUtil.mergeArrays(
                     problems, descriptions, CommonProblemDescriptor.ARRAY_FACTORY);
         problemElements.put(refElement, problems);
       }
       for (CommonProblemDescriptor description : descriptions) {
         getProblemToElements().put(description, refElement);
         collectQuickFixes(description.getFixes(), refElement);
       }
     } else {
       writeOutput(descriptions, refElement);
     }
   } else { // just need to collect problems
     for (CommonProblemDescriptor description : descriptions) {
       getProblemToElements().put(description, refElement);
     }
   }
 }
 private static boolean contains(
     CommonProblemDescriptor descriptor, Collection<CommonProblemDescriptor> descriptors) {
   PsiElement element = null;
   if (descriptor instanceof ProblemDescriptor) {
     element = ((ProblemDescriptor) descriptor).getPsiElement();
   }
   for (CommonProblemDescriptor problemDescriptor : descriptors) {
     if (problemDescriptor instanceof ProblemDescriptor) {
       if (!Comparing.equal(element, ((ProblemDescriptor) problemDescriptor).getPsiElement())) {
         continue;
       }
     }
     if (Comparing.strEqual(
         problemDescriptor.getDescriptionTemplate(), descriptor.getDescriptionTemplate())) {
       return true;
     }
   }
   return false;
 }
  @Override
  @Nullable
  public IntentionAction findQuickFixes(
      @NotNull final CommonProblemDescriptor problemDescriptor, final String hint) {
    InspectionProfileEntry tool = getToolWrapper().getTool();
    if (!(tool instanceof GlobalInspectionTool)) return null;
    final QuickFix fix = ((GlobalInspectionTool) tool).getQuickFix(hint);
    if (fix == null) {
      return null;
    }
    if (problemDescriptor instanceof ProblemDescriptor) {
      final ProblemDescriptor descriptor =
          new ProblemDescriptorImpl(
              ((ProblemDescriptor) problemDescriptor).getStartElement(),
              ((ProblemDescriptor) problemDescriptor).getEndElement(),
              problemDescriptor.getDescriptionTemplate(),
              new LocalQuickFix[] {(LocalQuickFix) fix},
              ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
              false,
              null,
              false);
      return QuickFixWrapper.wrap(descriptor, 0);
    }
    return new IntentionAction() {
      @Override
      @NotNull
      public String getText() {
        return fix.getName();
      }

      @Override
      @NotNull
      public String getFamilyName() {
        return fix.getFamilyName();
      }

      @Override
      public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
        return true;
      }

      @Override
      public void invoke(@NotNull Project project, Editor editor, PsiFile file)
          throws IncorrectOperationException {
        fix.applyFix(project, problemDescriptor); // todo check type consistency
      }

      @Override
      public boolean startInWriteAction() {
        return true;
      }
    };
  }
 public void ignoreProblem(final CommonProblemDescriptor descriptor, final QuickFix fix) {
   RefEntity refElement = getProblemToElements().get(descriptor);
   if (refElement != null) {
     final QuickFix[] fixes = descriptor.getFixes();
     for (int i = 0; i < fixes.length; i++) {
       if (fixes[i] == fix) {
         ignoreProblem(refElement, descriptor, i);
         return;
       }
     }
   }
 }
示例#6
0
  @RequiredReadAction
  @Override
  public void compose(StringBuffer buf, RefEntity refElement, CommonProblemDescriptor descriptor) {
    CommonProblemDescriptor[] descriptions = myTool.getDescriptions(refElement);

    int problemIdx = 0;
    if (descriptions != null) { // server-side inspections
      problemIdx = -1;
      for (int i = 0; i < descriptions.length; i++) {
        CommonProblemDescriptor description = descriptions[i];
        if (description == descriptor) {
          problemIdx = i;
          break;
        }
      }
      if (problemIdx == -1) return;
    }

    genPageHeader(buf, refElement);
    appendHeading(buf, InspectionsBundle.message("inspection.problem.synopsis"));
    //noinspection HardCodedStringLiteral
    buf.append("<br>");
    appendAfterHeaderIndention(buf);

    composeDescription(descriptor, problemIdx, buf, refElement);

    if (refElement instanceof RefElement && !refElement.isValid()) return;

    final QuickFix[] fixes = descriptor.getFixes();
    if (fixes != null && fixes.length > 0) {
      //noinspection HardCodedStringLiteral
      buf.append("<br><br>");
      appendHeading(buf, InspectionsBundle.message("inspection.problem.resolution"));
      //noinspection HardCodedStringLiteral
      buf.append("<br>");
      appendAfterHeaderIndention(buf);

      int idx = 0;
      for (QuickFix fix : fixes) {
        //noinspection HardCodedStringLiteral
        //noinspection HardCodedStringLiteral
        buf.append("<a HREF=\"file://bred.txt#invokelocal:").append(idx++);
        buf.append("\">");
        buf.append(fix.getName());
        //noinspection HardCodedStringLiteral
        buf.append("</a>");
        //noinspection HardCodedStringLiteral
        buf.append("<br>");
        appendAfterHeaderIndention(buf);
      }
    }
  }
示例#7
0
 private void invokeFix(
     final RefEntity element, final CommonProblemDescriptor descriptor, final int idx) {
   final QuickFix[] fixes = descriptor.getFixes();
   if (fixes != null && fixes.length > idx && fixes[idx] != null) {
     if (element instanceof RefElement) {
       PsiElement psiElement = ((RefElement) element).getElement();
       if (psiElement != null && psiElement.isValid()) {
         if (!FileModificationService.getInstance().preparePsiElementForWrite(psiElement)) return;
         performFix(element, descriptor, idx, fixes[idx]);
       }
     } else {
       performFix(element, descriptor, idx, fixes[idx]);
     }
   }
 }
示例#8
0
  @RequiredReadAction
  protected void composeDescription(
      @NotNull CommonProblemDescriptor description,
      int i,
      @NotNull StringBuffer buf,
      @NotNull RefEntity refElement) {
    PsiElement expression =
        description instanceof ProblemDescriptor
            ? ((ProblemDescriptor) description).getPsiElement()
            : null;
    StringBuilder anchor = new StringBuilder();
    VirtualFile vFile = null;

    if (expression != null) {
      vFile = expression.getContainingFile().getVirtualFile();
      if (vFile instanceof VirtualFileWindow) vFile = ((VirtualFileWindow) vFile).getDelegate();

      //noinspection HardCodedStringLiteral
      anchor.append("<a HREF=\"");
      try {
        if (myExporter == null) {
          //noinspection HardCodedStringLiteral
          anchor.append(new URL(vFile.getUrl() + "#descr:" + i));
        } else {
          anchor.append(myExporter.getURL(refElement));
        }
      } catch (MalformedURLException e) {
        LOG.error(e);
      }

      anchor.append("\">");
      anchor.append(
          ProblemDescriptorUtil.extractHighlightedText(description, expression)
              .replaceAll("\\$", "\\\\\\$"));
      //noinspection HardCodedStringLiteral
      anchor.append("</a>");
    } else {
      //noinspection HardCodedStringLiteral
      anchor.append("<font style=\"font-weight:bold; color:#FF0000\";>");
      anchor.append(InspectionsBundle.message("inspection.export.results.invalidated.item"));
      //noinspection HardCodedStringLiteral
      anchor.append("</font>");
    }

    String descriptionTemplate = description.getDescriptionTemplate();
    //noinspection HardCodedStringLiteral
    final String reference = "#ref";
    final boolean containsReference = descriptionTemplate.contains(reference);
    String res = descriptionTemplate.replaceAll(reference, anchor.toString());
    final int lineNumber =
        description instanceof ProblemDescriptor
            ? ((ProblemDescriptor) description).getLineNumber()
            : -1;
    StringBuffer lineAnchor = new StringBuffer();
    if (expression != null && lineNumber > 0) {
      Document doc = FileDocumentManager.getInstance().getDocument(vFile);
      lineAnchor.append(InspectionsBundle.message("inspection.export.results.at.line")).append(" ");
      if (myExporter == null) {
        //noinspection HardCodedStringLiteral
        lineAnchor.append("<a HREF=\"");
        try {
          int offset = doc.getLineStartOffset(lineNumber - 1);
          offset = CharArrayUtil.shiftForward(doc.getCharsSequence(), offset, " \t");
          lineAnchor.append(new URL(vFile.getUrl() + "#" + offset));
        } catch (MalformedURLException e) {
          LOG.error(e);
        }
        lineAnchor.append("\">");
      }
      lineAnchor.append(Integer.toString(lineNumber));
      //noinspection HardCodedStringLiteral
      lineAnchor.append("</a>");
      //noinspection HardCodedStringLiteral
      final String location = "#loc";
      if (!res.contains(location)) {
        res += " (" + location + ")";
      }
      res = res.replaceAll(location, lineAnchor.toString());
    }
    buf.append(res);
    buf.append(BR).append(BR);
    composeAdditionalDescription(buf, refElement);
  }
  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());
      }
    }
  }
  @Override
  public void addProblemElement(
      final RefEntity refElement,
      boolean filterSuppressed,
      @NotNull final CommonProblemDescriptor... descriptors) {
    if (refElement == null) return;
    if (descriptors.length == 0) return;
    if (filterSuppressed) {
      if (!isOutputPathSet() || !(myToolWrapper instanceof LocalInspectionToolWrapper)) {
        synchronized (lock) {
          Map<RefEntity, CommonProblemDescriptor[]> problemElements = getProblemElements();
          CommonProblemDescriptor[] problems = problemElements.get(refElement);
          problems = problems == null ? descriptors : mergeDescriptors(problems, descriptors);
          problemElements.put(refElement, problems);
        }
        for (CommonProblemDescriptor description : descriptors) {
          getProblemToElements().put(description, refElement);
          collectQuickFixes(description.getFixes(), refElement);
        }
      } else {
        writeOutput(descriptors, refElement);
      }
    } else { // just need to collect problems
      for (CommonProblemDescriptor descriptor : descriptors) {
        getProblemToElements().put(descriptor, refElement);
      }
    }

    final GlobalInspectionContextImpl context = getContext();
    if (myToolWrapper instanceof LocalInspectionToolWrapper) {
      final InspectionResultsView view = context.getView();
      if (view == null || !(refElement instanceof RefElement)) {
        return;
      }
      UIUtil.invokeLaterIfNeeded(
          new Runnable() {
            @Override
            public void run() {
              if (!isDisposed()) {
                final InspectionNode toolNode;
                synchronized (myToolLock) {
                  if (myToolNode == null) {
                    final HighlightSeverity currentSeverity = getSeverity((RefElement) refElement);
                    toolNode =
                        view.addTool(
                            myToolWrapper,
                            HighlightDisplayLevel.find(currentSeverity),
                            context.getUIOptions().GROUP_BY_SEVERITY);
                  } else {
                    toolNode = myToolNode;
                    if (toolNode.isTooBigForOnlineRefresh()) {
                      return;
                    }
                  }
                }
                final Map<RefEntity, CommonProblemDescriptor[]> problems =
                    new HashMap<RefEntity, CommonProblemDescriptor[]>();
                problems.put(refElement, descriptors);
                final Map<String, Set<RefEntity>> contents = new HashMap<String, Set<RefEntity>>();
                final String groupName =
                    refElement.getRefManager().getGroupName((RefElement) refElement);
                Set<RefEntity> content = contents.get(groupName);
                if (content == null) {
                  content = new HashSet<RefEntity>();
                  contents.put(groupName, content);
                }
                content.add(refElement);

                view.getProvider()
                    .appendToolNodeContent(
                        context,
                        toolNode,
                        (InspectionTreeNode) toolNode.getParent(),
                        context.getUIOptions().SHOW_STRUCTURE,
                        contents,
                        problems,
                        (DefaultTreeModel) view.getTree().getModel());
                context.addView(view);
              }
            }
          });
    }
  }