예제 #1
0
  @NotNull
  public static HighlightInfo fromAnnotation(
      @NotNull Annotation annotation, @Nullable TextRange fixedRange, boolean batchMode) {
    final TextAttributes forcedAttributes = annotation.getEnforcedTextAttributes();
    final TextAttributesKey forcedAttributesKey =
        forcedAttributes == null ? annotation.getTextAttributes() : null;

    HighlightInfo info =
        new HighlightInfo(
            forcedAttributes,
            forcedAttributesKey,
            convertType(annotation),
            fixedRange != null ? fixedRange.getStartOffset() : annotation.getStartOffset(),
            fixedRange != null ? fixedRange.getEndOffset() : annotation.getEndOffset(),
            annotation.getMessage(),
            annotation.getTooltip(),
            annotation.getSeverity(),
            annotation.isAfterEndOfLine(),
            annotation.needsUpdateOnTyping(),
            annotation.isFileLevelAnnotation(),
            0);
    info.setGutterIconRenderer(annotation.getGutterIconRenderer());
    info.setProblemGroup(annotation.getProblemGroup());
    appendFixes(
        fixedRange, info, batchMode ? annotation.getBatchFixes() : annotation.getQuickFixes());
    return info;
  }
  private static void highlightTodos(
      @NotNull PsiFile file,
      @NotNull CharSequence text,
      int startOffset,
      int endOffset,
      @NotNull ProgressIndicator progress,
      @NotNull ProperTextRange priorityRange,
      @NotNull Collection<HighlightInfo> result,
      @NotNull Collection<HighlightInfo> outsideResult) {
    PsiSearchHelper helper = PsiSearchHelper.SERVICE.getInstance(file.getProject());
    TodoItem[] todoItems = helper.findTodoItems(file, startOffset, endOffset);
    if (todoItems.length == 0) return;

    for (TodoItem todoItem : todoItems) {
      progress.checkCanceled();
      TextRange range = todoItem.getTextRange();
      String description =
          text.subSequence(range.getStartOffset(), range.getEndOffset()).toString();
      TextAttributes attributes = todoItem.getPattern().getAttributes().getTextAttributes();
      HighlightInfo info =
          HighlightInfo.createHighlightInfo(
              HighlightInfoType.TODO, range, description, description, attributes);
      assert info != null;
      if (priorityRange.containsRange(info.getStartOffset(), info.getEndOffset())) {
        result.add(info);
      } else {
        outsideResult.add(info);
      }
    }
  }
 private void incErrorCount(RangeHighlighter highlighter, int delta) {
   Object o = highlighter.getErrorStripeTooltip();
   if (!(o instanceof HighlightInfo)) return;
   HighlightInfo info = (HighlightInfo) o;
   HighlightSeverity infoSeverity = info.getSeverity();
   final int severityIdx = mySeverityRegistrar.getSeverityIdx(infoSeverity);
   if (severityIdx != -1) {
     errorCount[severityIdx] += delta;
   }
 }
예제 #4
0
  public boolean equalsByActualOffset(@NotNull HighlightInfo info) {
    if (info == this) return true;

    return info.getSeverity() == getSeverity()
        && info.getActualStartOffset() == getActualStartOffset()
        && info.getActualEndOffset() == getActualEndOffset()
        && Comparing.equal(info.type, type)
        && Comparing.equal(info.gutterIconRenderer, gutterIconRenderer)
        && Comparing.equal(info.forcedTextAttributes, forcedTextAttributes)
        && Comparing.equal(info.forcedTextAttributesKey, forcedTextAttributesKey)
        && Comparing.strEqual(info.getDescription(), getDescription());
  }
 private static List<Problem> convertToProblems(
     final Collection<HighlightInfo> infos,
     final VirtualFile file,
     final boolean hasErrorElement) {
   List<Problem> problems = new SmartList<Problem>();
   for (HighlightInfo info : infos) {
     if (info.getSeverity() == HighlightSeverity.ERROR) {
       Problem problem = new ProblemImpl(file, info, hasErrorElement);
       problems.add(problem);
     }
   }
   return problems;
 }
예제 #6
0
  public boolean equals(Object obj) {
    if (obj == this) return true;
    if (!(obj instanceof HighlightInfo)) return false;
    HighlightInfo info = (HighlightInfo) obj;

    return info.getSeverity() == getSeverity()
        && info.startOffset == startOffset
        && info.endOffset == endOffset
        && Comparing.equal(info.type, type)
        && Comparing.equal(info.gutterIconRenderer, gutterIconRenderer)
        && Comparing.equal(info.forcedTextAttributes, forcedTextAttributes)
        && Comparing.equal(info.forcedTextAttributesKey, forcedTextAttributesKey)
        && Comparing.strEqual(info.getDescription(), getDescription());
  }
예제 #7
0
  public static void showInfoTooltip(
      @NotNull final HighlightInfo info,
      final Editor editor,
      final int defaultOffset,
      final int currentWidth) {
    if (info.toolTip == null || info.getSeverity() == HighlightSeverity.INFORMATION) return;
    Rectangle visibleArea = editor.getScrollingModel().getVisibleArea();
    int endOffset = info.highlighter.getEndOffset();
    int startOffset = info.highlighter.getStartOffset();

    Point top = editor.logicalPositionToXY(editor.offsetToLogicalPosition(startOffset));
    Point bottom = editor.logicalPositionToXY(editor.offsetToLogicalPosition(endOffset));

    Point bestPoint = new Point(top.x, bottom.y + editor.getLineHeight());

    if (!visibleArea.contains(bestPoint)) {
      bestPoint = editor.logicalPositionToXY(editor.offsetToLogicalPosition(defaultOffset));
    }

    Point p =
        SwingUtilities.convertPoint(
            editor.getContentComponent(),
            bestPoint,
            editor.getComponent().getRootPane().getLayeredPane());
    TooltipController.getInstance()
        .showTooltip(editor, p, info.toolTip, currentWidth, false, DAEMON_INFO_GROUP);
  }
  private void highlightInjectedSyntax(final PsiFile injectedPsi, HighlightInfoHolder holder) {
    List<Trinity<IElementType, PsiLanguageInjectionHost, TextRange>> tokens =
        InjectedLanguageUtil.getHighlightTokens(injectedPsi);
    if (tokens == null) return;

    final Language injectedLanguage = injectedPsi.getLanguage();
    Project project = injectedPsi.getProject();
    SyntaxHighlighter syntaxHighlighter =
        SyntaxHighlighterFactory.getSyntaxHighlighter(
            injectedLanguage, project, injectedPsi.getVirtualFile());
    final TextAttributes defaultAttrs = myGlobalScheme.getAttributes(HighlighterColors.TEXT);

    for (Trinity<IElementType, PsiLanguageInjectionHost, TextRange> token : tokens) {
      ProgressManager.checkCanceled();
      IElementType tokenType = token.getFirst();
      PsiLanguageInjectionHost injectionHost = token.getSecond();
      TextRange textRange = token.getThird();
      TextAttributesKey[] keys = syntaxHighlighter.getTokenHighlights(tokenType);
      if (textRange.getLength() == 0) continue;

      TextRange annRange = textRange.shiftRight(injectionHost.getTextRange().getStartOffset());
      // force attribute colors to override host' ones
      TextAttributes attributes = null;
      for (TextAttributesKey key : keys) {
        TextAttributes attrs2 = myGlobalScheme.getAttributes(key);
        if (attrs2 != null) {
          attributes = attributes == null ? attrs2 : TextAttributes.merge(attributes, attrs2);
        }
      }
      TextAttributes forcedAttributes;
      if (attributes == null || attributes.isEmpty() || attributes.equals(defaultAttrs)) {
        forcedAttributes = TextAttributes.ERASE_MARKER;
      } else {
        Color back =
            attributes.getBackgroundColor() == null
                ? myGlobalScheme.getDefaultBackground()
                : attributes.getBackgroundColor();
        Color fore =
            attributes.getForegroundColor() == null
                ? myGlobalScheme.getDefaultForeground()
                : attributes.getForegroundColor();
        forcedAttributes =
            new TextAttributes(
                fore,
                back,
                attributes.getEffectColor(),
                attributes.getEffectType(),
                attributes.getFontType());
      }

      HighlightInfo info =
          HighlightInfo.createHighlightInfo(
              HighlightInfoType.INJECTED_LANGUAGE_FRAGMENT, annRange, null, null, forcedAttributes);
      holder.add(info);
    }
  }
예제 #9
0
 private static boolean isAcceptedByFilters(
     @NotNull HighlightInfo info, @Nullable PsiElement psiElement) {
   PsiFile file = psiElement == null ? null : psiElement.getContainingFile();
   for (HighlightInfoFilter filter : FILTERS) {
     if (!filter.accept(info, file)) {
       return false;
     }
   }
   info.psiElement = psiElement;
   return true;
 }
  /**
   * Adds a highlight to the view. Returns a tag that can be used to refer to the highlight.
   *
   * @param p0 the start offset of the range to highlight &gt;= 0
   * @param p1 the end offset of the range to highlight &gt;= p0
   * @param p the painter to use to actually render the highlight
   * @return an object that can be used as a tag to refer to the highlight
   * @exception BadLocationException if the specified location is invalid
   */
  public Object addHighlight(int p0, int p1, Highlighter.HighlightPainter p)
      throws BadLocationException {
    if (p0 < 0) {
      throw new BadLocationException("Invalid start offset", p0);
    }

    if (p1 < p0) {
      throw new BadLocationException("Invalid end offset", p1);
    }

    Document doc = component.getDocument();
    HighlightInfo i =
        (getDrawsLayeredHighlights() && (p instanceof LayeredHighlighter.LayerPainter))
            ? new LayeredHighlightInfo()
            : new HighlightInfo();
    i.painter = p;
    i.p0 = doc.createPosition(p0);
    i.p1 = doc.createPosition(p1);
    highlights.addElement(i);
    safeDamageRange(p0, p1);
    return i;
  }
 /**
  * Renders the highlights.
  *
  * @param g the graphics context
  */
 public void paint(Graphics g) {
   // PENDING(prinz) - should cull ranges not visible
   int len = highlights.size();
   for (int i = 0; i < len; i++) {
     HighlightInfo info = highlights.elementAt(i);
     if (!(info instanceof LayeredHighlightInfo)) {
       // Avoid allocing unless we need it.
       Rectangle a = component.getBounds();
       Insets insets = component.getInsets();
       a.x = insets.left;
       a.y = insets.top;
       a.width -= insets.left + insets.right;
       a.height -= insets.top + insets.bottom;
       for (; i < len; i++) {
         info = highlights.elementAt(i);
         if (!(info instanceof LayeredHighlightInfo)) {
           Highlighter.HighlightPainter p = info.getPainter();
           p.paint(g, info.getStartOffset(), info.getEndOffset(), a, component);
         }
       }
     }
   }
 }
  /**
   * Changes a highlight.
   *
   * @param tag the highlight tag
   * @param p0 the beginning of the range &gt;= 0
   * @param p1 the end of the range &gt;= p0
   * @exception BadLocationException if the specified location is invalid
   */
  public void changeHighlight(Object tag, int p0, int p1) throws BadLocationException {
    if (p0 < 0) {
      throw new BadLocationException("Invalid beginning of the range", p0);
    }

    if (p1 < p0) {
      throw new BadLocationException("Invalid end of the range", p1);
    }

    Document doc = component.getDocument();
    if (tag instanceof LayeredHighlightInfo) {
      LayeredHighlightInfo lhi = (LayeredHighlightInfo) tag;
      if (lhi.width > 0 && lhi.height > 0) {
        component.repaint(lhi.x, lhi.y, lhi.width, lhi.height);
      }
      // Mark the highlights region as invalid, it will reset itself
      // next time asked to paint.
      lhi.width = lhi.height = 0;
      lhi.p0 = doc.createPosition(p0);
      lhi.p1 = doc.createPosition(p1);
      safeDamageRange(Math.min(p0, p1), Math.max(p0, p1));
    } else {
      HighlightInfo info = (HighlightInfo) tag;
      int oldP0 = info.p0.getOffset();
      int oldP1 = info.p1.getOffset();
      if (p0 == oldP0) {
        safeDamageRange(Math.min(oldP1, p1), Math.max(oldP1, p1));
      } else if (p1 == oldP1) {
        safeDamageRange(Math.min(p0, oldP0), Math.max(p0, oldP0));
      } else {
        safeDamageRange(oldP0, oldP1);
        safeDamageRange(p0, p1);
      }
      info.p0 = doc.createPosition(p0);
      info.p1 = doc.createPosition(p1);
    }
  }
예제 #13
0
 private static void appendFixes(
     @Nullable TextRange fixedRange,
     @NotNull HighlightInfo info,
     @Nullable List<Annotation.QuickFixInfo> fixes) {
   if (fixes != null) {
     for (final Annotation.QuickFixInfo quickFixInfo : fixes) {
       TextRange range = fixedRange != null ? fixedRange : quickFixInfo.textRange;
       HighlightDisplayKey key =
           quickFixInfo.key != null
               ? quickFixInfo.key
               : HighlightDisplayKey.find(ANNOTATOR_INSPECTION_SHORT_NAME);
       info.registerFix(
           quickFixInfo.quickFix, null, HighlightDisplayKey.getDisplayNameByKey(key), range, key);
     }
   }
 }
  private static void addPatchedInfos(
      @NotNull HighlightInfo info,
      @NotNull PsiFile injectedPsi,
      @NotNull DocumentWindow documentWindow,
      @NotNull InjectedLanguageManager injectedLanguageManager,
      @Nullable TextRange fixedTextRange,
      @NotNull Collection<HighlightInfo> out) {
    ProperTextRange textRange = new ProperTextRange(info.startOffset, info.endOffset);
    List<TextRange> editables =
        injectedLanguageManager.intersectWithAllEditableFragments(injectedPsi, textRange);
    for (TextRange editable : editables) {
      TextRange hostRange =
          fixedTextRange == null ? documentWindow.injectedToHost(editable) : fixedTextRange;

      boolean isAfterEndOfLine = info.isAfterEndOfLine;
      if (isAfterEndOfLine) {
        // convert injected afterEndOfLine to either host' afterEndOfLine or not-afterEndOfLine
        // highlight of the injected fragment boundary
        int hostEndOffset = hostRange.getEndOffset();
        int lineNumber = documentWindow.getDelegate().getLineNumber(hostEndOffset);
        int hostLineEndOffset = documentWindow.getDelegate().getLineEndOffset(lineNumber);
        if (hostEndOffset < hostLineEndOffset) {
          // convert to non-afterEndOfLine
          isAfterEndOfLine = false;
          hostRange = new ProperTextRange(hostRange.getStartOffset(), hostEndOffset + 1);
        }
      }

      HighlightInfo patched =
          new HighlightInfo(
              info.forcedTextAttributes,
              info.forcedTextAttributesKey,
              info.type,
              hostRange.getStartOffset(),
              hostRange.getEndOffset(),
              info.description,
              info.toolTip,
              info.type.getSeverity(null),
              isAfterEndOfLine,
              null,
              false);
      patched.setHint(info.hasHint());
      patched.setGutterIconRenderer(info.getGutterIconRenderer());

      if (info.quickFixActionRanges != null) {
        for (Pair<HighlightInfo.IntentionActionDescriptor, TextRange> pair :
            info.quickFixActionRanges) {
          TextRange quickfixTextRange = pair.getSecond();
          List<TextRange> editableQF =
              injectedLanguageManager.intersectWithAllEditableFragments(
                  injectedPsi, quickfixTextRange);
          for (TextRange editableRange : editableQF) {
            HighlightInfo.IntentionActionDescriptor descriptor = pair.getFirst();
            if (patched.quickFixActionRanges == null)
              patched.quickFixActionRanges =
                  new ArrayList<Pair<HighlightInfo.IntentionActionDescriptor, TextRange>>();
            TextRange hostEditableRange = documentWindow.injectedToHost(editableRange);
            patched.quickFixActionRanges.add(Pair.create(descriptor, hostEditableRange));
          }
        }
      }
      patched.fromInjection = true;
      out.add(patched);
    }
  }