Java Code Examples for com.intellij.openapi.editor.markup.RangeHighlighter#putUserData()

The following examples show how to use com.intellij.openapi.editor.markup.RangeHighlighter#putUserData() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EditorHyperlinkSupport.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static void linkFollowed(Editor editor, Collection<? extends RangeHighlighter> ranges, final RangeHighlighter link) {
  MarkupModelEx markupModel = (MarkupModelEx)editor.getMarkupModel();
  for (RangeHighlighter range : ranges) {
    TextAttributes oldAttr = range.getUserData(OLD_HYPERLINK_TEXT_ATTRIBUTES);
    if (oldAttr != null) {
      markupModel.setRangeHighlighterAttributes(range, oldAttr);
      range.putUserData(OLD_HYPERLINK_TEXT_ATTRIBUTES, null);
    }
    if (range == link) {
      range.putUserData(OLD_HYPERLINK_TEXT_ATTRIBUTES, range.getTextAttributes());
      markupModel.setRangeHighlighterAttributes(range, getFollowedHyperlinkAttributes(range));
    }
  }
  //refresh highlighter text attributes
  markupModel.addRangeHighlighter(0, 0, link.getLayer(), getHyperlinkAttributes(), HighlighterTargetArea.EXACT_RANGE).dispose();
}
 
Example 2
Source File: LivePreview.java    From consulo with Apache License 2.0 6 votes vote down vote up
private List<RangeHighlighter> addNewHighlighters() {
  List<FindResult> occurrences = mySearchResults.getOccurrences();
  List<RangeHighlighter> newHighlighters = new ArrayList<>(occurrences.size());
  for (FindResult range : occurrences) {
    if (range.getEndOffset() > mySearchResults.getEditor().getDocument().getTextLength()) continue;
    TextAttributes attributes = createAttributes(range);
    RangeHighlighter existingHighlighter = findExistingHighlighter(range.getStartOffset(), range.getEndOffset(), attributes);
    if (existingHighlighter == null) {
      RangeHighlighter highlighter = addHighlighter(range.getStartOffset(), range.getEndOffset(), attributes);
      if (highlighter != null) {
        highlighter.putUserData(SEARCH_MARKER, Boolean.TRUE);
        newHighlighters.add(highlighter);
      }
    }
    else {
      existingHighlighter.putUserData(MARKER_USED, Boolean.TRUE);
    }
  }
  return newHighlighters;
}
 
Example 3
Source File: LivePreview.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void updateInSelectionHighlighters() {
  final SelectionModel selectionModel = mySearchResults.getEditor().getSelectionModel();
  int[] starts = selectionModel.getBlockSelectionStarts();
  int[] ends = selectionModel.getBlockSelectionEnds();

  for (RangeHighlighter highlighter : myHighlighters) {
    if (!highlighter.isValid()) continue;
    boolean needsAdditionalHighlighting = false;
    TextRange cursor = mySearchResults.getCursor();
    if (cursor == null || highlighter.getStartOffset() != cursor.getStartOffset() || highlighter.getEndOffset() != cursor.getEndOffset()) {
      for (int i = 0; i < starts.length; ++i) {
        TextRange selectionRange = new TextRange(starts[i], ends[i]);
        needsAdditionalHighlighting = selectionRange.intersects(highlighter.getStartOffset(), highlighter.getEndOffset()) &&
                                      selectionRange.getEndOffset() != highlighter.getStartOffset() &&
                                      highlighter.getEndOffset() != selectionRange.getStartOffset();
        if (needsAdditionalHighlighting) break;
      }
    }

    RangeHighlighter inSelectionHighlighter = highlighter.getUserData(IN_SELECTION_KEY);
    if (inSelectionHighlighter != null) {
      if (!needsAdditionalHighlighting) {
        removeHighlighter(inSelectionHighlighter);
      }
    }
    else if (needsAdditionalHighlighting) {
      RangeHighlighter additionalHighlighter = addHighlighter(highlighter.getStartOffset(), highlighter.getEndOffset(), new TextAttributes(null, null, Color.WHITE, EffectType.ROUNDED_BOX, Font.PLAIN));
      highlighter.putUserData(IN_SELECTION_KEY, additionalHighlighter);
    }
  }
}
 
Example 4
Source File: EditorHyperlinkSupport.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static void associateHyperlink(@Nonnull RangeHighlighter highlighter, @Nonnull HyperlinkInfo hyperlinkInfo, @Nullable TextAttributes followedHyperlinkAttributes) {
  highlighter.putUserData(HYPERLINK, new HyperlinkInfoTextAttributes(hyperlinkInfo, followedHyperlinkAttributes));
}
 
Example 5
Source File: LineMarkersUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
private static RangeHighlighter createOrReuseLineMarker(@Nonnull LineMarkerInfo<?> info, @Nonnull MarkupModelEx markupModel, @Nullable HighlightersRecycler toReuse) {
  LineMarkerInfo.LineMarkerGutterIconRenderer<?> newRenderer = (LineMarkerInfo.LineMarkerGutterIconRenderer<?>)info.createGutterRenderer();

  RangeHighlighter highlighter = toReuse == null ? null : toReuse.pickupHighlighterFromGarbageBin(info.startOffset, info.endOffset, HighlighterLayer.ADDITIONAL_SYNTAX);
  boolean newHighlighter = false;
  if (highlighter == null) {
    newHighlighter = true;
    highlighter = markupModel.addRangeHighlighterAndChangeAttributes(info.startOffset, info.endOffset, HighlighterLayer.ADDITIONAL_SYNTAX, null, HighlighterTargetArea.LINES_IN_RANGE, false, markerEx -> {
              markerEx.setGutterIconRenderer(newRenderer);
              markerEx.setLineSeparatorColor(info.separatorColor);
              markerEx.setLineSeparatorPlacement(info.separatorPlacement);

              markerEx.putUserData(LINE_MARKER_INFO, info);
            });

    MarkupEditorFilter editorFilter = info.getEditorFilter();
    if (editorFilter != MarkupEditorFilter.EMPTY) {
      highlighter.setEditorFilter(editorFilter);
    }
  }

  if (!newHighlighter) {
    highlighter.putUserData(LINE_MARKER_INFO, info);

    LineMarkerInfo.LineMarkerGutterIconRenderer<?> oldRenderer =
            highlighter.getGutterIconRenderer() instanceof LineMarkerInfo.LineMarkerGutterIconRenderer ? (LineMarkerInfo.LineMarkerGutterIconRenderer<?>)highlighter.getGutterIconRenderer() : null;
    boolean rendererChanged = newRenderer == null || !newRenderer.equals(oldRenderer);
    boolean lineSeparatorColorChanged = !Comparing.equal(highlighter.getLineSeparatorColor(), info.separatorColor);
    boolean lineSeparatorPlacementChanged = !Comparing.equal(highlighter.getLineSeparatorPlacement(), info.separatorPlacement);

    if (rendererChanged || lineSeparatorColorChanged || lineSeparatorPlacementChanged) {
      markupModel.changeAttributesInBatch((RangeHighlighterEx)highlighter, markerEx -> {
        if (rendererChanged) {
          markerEx.setGutterIconRenderer(newRenderer);
        }
        if (lineSeparatorColorChanged) {
          markerEx.setLineSeparatorColor(info.separatorColor);
        }
        if (lineSeparatorPlacementChanged) {
          markerEx.setLineSeparatorPlacement(info.separatorPlacement);
        }
      });
    }
  }
  info.highlighter = highlighter;
  return highlighter;
}