private void updateInSelectionHighlighters() {
    if (mySearchResults.getEditor() == null) return;
    final SelectionModel selectionModel = mySearchResults.getEditor().getSelectionModel();
    int[] starts = selectionModel.getBlockSelectionStarts();
    int[] ends = selectionModel.getBlockSelectionEnds();

    final HashSet<RangeHighlighter> toRemove = new HashSet<RangeHighlighter>();
    Set<RangeHighlighter> toAdd = new HashSet<RangeHighlighter>();
    for (RangeHighlighter highlighter : myHighlighters) {
      boolean intersectsWithSelection = false;
      for (int i = 0; i < starts.length; ++i) {
        TextRange selectionRange = new TextRange(starts[i], ends[i]);
        intersectsWithSelection =
            selectionRange.intersects(highlighter.getStartOffset(), highlighter.getEndOffset())
                && selectionRange.getEndOffset() != highlighter.getStartOffset()
                && highlighter.getEndOffset() != selectionRange.getStartOffset();
        if (intersectsWithSelection) break;
      }

      final Object userData = highlighter.getUserData(IN_SELECTION_KEY);
      if (userData != null) {
        if (!intersectsWithSelection) {
          if (userData == IN_SELECTION2) {
            HighlightManager.getInstance(mySearchResults.getProject())
                .removeSegmentHighlighter(mySearchResults.getEditor(), highlighter);
            toRemove.add(highlighter);
          } else {
            highlighter.putUserData(IN_SELECTION_KEY, null);
          }
        }
      } else if (intersectsWithSelection) {
        TextRange cursor = mySearchResults.getCursor();
        if (cursor != null
            && highlighter.getStartOffset() == cursor.getStartOffset()
            && highlighter.getEndOffset() == cursor.getEndOffset()) continue;
        final RangeHighlighter toAnnotate =
            highlightRange(
                new TextRange(highlighter.getStartOffset(), highlighter.getEndOffset()),
                new TextAttributes(null, null, Color.WHITE, EffectType.ROUNDED_BOX, 0),
                toAdd);
        highlighter.putUserData(IN_SELECTION_KEY, IN_SELECTION1);
        toAnnotate.putUserData(IN_SELECTION_KEY, IN_SELECTION2);
      }
    }
    myHighlighters.removeAll(toRemove);
    myHighlighters.addAll(toAdd);
  }
 @NotNull
 private RangeHighlighter highlightRange(
     TextRange textRange, TextAttributes attributes, Set<RangeHighlighter> highlighters) {
   if (myInSmartUpdate) {
     for (RangeHighlighter highlighter : myHighlighters) {
       if (highlighter.isValid()
           && highlighter.getStartOffset() == textRange.getStartOffset()
           && highlighter.getEndOffset() == textRange.getEndOffset()) {
         if (attributes.equals(highlighter.getTextAttributes())) {
           highlighter.putUserData(MARKER_USED, YES);
           if (highlighters != myHighlighters) {
             highlighters.add(highlighter);
           }
           return highlighter;
         }
       }
     }
   }
   final RangeHighlighter highlighter = doHightlightRange(textRange, attributes, highlighters);
   if (myInSmartUpdate) {
     highlighter.putUserData(MARKER_USED, YES);
   }
   return highlighter;
 }
  private RangeHighlighter doHightlightRange(
      final TextRange textRange,
      final TextAttributes attributes,
      Set<RangeHighlighter> highlighters) {
    HighlightManager highlightManager = HighlightManager.getInstance(mySearchResults.getProject());

    MarkupModelEx markupModel = (MarkupModelEx) mySearchResults.getEditor().getMarkupModel();

    final RangeHighlighter[] candidate = new RangeHighlighter[1];

    boolean notFound =
        markupModel.processRangeHighlightersOverlappingWith(
            textRange.getStartOffset(),
            textRange.getEndOffset(),
            new Processor<RangeHighlighterEx>() {
              @Override
              public boolean process(RangeHighlighterEx highlighter) {
                TextAttributes textAttributes = highlighter.getTextAttributes();
                if (highlighter.getUserData(SEARCH_MARKER) != null
                    && textAttributes != null
                    && textAttributes.equals(attributes)
                    && highlighter.getStartOffset() == textRange.getStartOffset()
                    && highlighter.getEndOffset() == textRange.getEndOffset()) {
                  candidate[0] = highlighter;
                  return false;
                }
                return true;
              }
            });

    if (!notFound && highlighters.contains(candidate[0])) {
      return candidate[0];
    }
    final ArrayList<RangeHighlighter> dummy = new ArrayList<RangeHighlighter>();
    highlightManager.addRangeHighlight(
        mySearchResults.getEditor(),
        textRange.getStartOffset(),
        textRange.getEndOffset(),
        attributes,
        false,
        dummy);
    final RangeHighlighter h = dummy.get(0);
    highlighters.add(h);
    h.putUserData(SEARCH_MARKER, YES);
    return h;
  }
 private void clearUnusedHightlighters() {
   Set<RangeHighlighter> unused = new com.intellij.util.containers.HashSet<RangeHighlighter>();
   for (RangeHighlighter highlighter : myHighlighters) {
     if (highlighter.getUserData(MARKER_USED) == null) {
       unused.add(highlighter);
     } else {
       highlighter.putUserData(MARKER_USED, null);
     }
   }
   myHighlighters.removeAll(unused);
   Project project = mySearchResults.getProject();
   if (!project.isDisposed()) {
     for (RangeHighlighter highlighter : unused) {
       HighlightManager.getInstance(project)
           .removeSegmentHighlighter(mySearchResults.getEditor(), highlighter);
     }
   }
 }
  @Nullable
  protected static RangeHighlighter createHighlighter(
      @NotNull Project project, @NotNull Document document, int lineIndex) {
    if (lineIndex < 0 || lineIndex >= document.getLineCount()) {
      return null;
    }

    EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
    TextAttributes attributes = scheme.getAttributes(DebuggerColors.BREAKPOINT_ATTRIBUTES);

    RangeHighlighter highlighter =
        ((MarkupModelEx) DocumentMarkupModel.forDocument(document, project, true))
            .addPersistentLineHighlighter(
                lineIndex, DebuggerColors.BREAKPOINT_HIGHLIGHTER_LAYER, attributes);
    if (highlighter == null || !highlighter.isValid()) {
      return null;
    }
    highlighter.putUserData(DebuggerColors.BREAKPOINT_HIGHLIGHTER_KEY, Boolean.TRUE);
    highlighter.setErrorStripeTooltip(
        DebuggerBundle.message("breakpoint.tooltip.text", lineIndex + 1));
    return highlighter;
  }