Java Code Examples for com.intellij.openapi.util.TextRange#intersectsStrict()

The following examples show how to use com.intellij.openapi.util.TextRange#intersectsStrict() . 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: BlockUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
public static List<DataLanguageBlockWrapper> filterBlocksByRange(@Nonnull List<DataLanguageBlockWrapper> list, @Nonnull TextRange textRange) {
  int i = 0;
  while (i < list.size()) {
    final DataLanguageBlockWrapper wrapper = list.get(i);
    final TextRange range = wrapper.getTextRange();
    if (textRange.contains(range)) {
      i++;
    }
    else if (range.intersectsStrict(textRange)) {
      list.remove(i);
      list.addAll(i, buildChildWrappers(wrapper.getOriginal()));
    }
    else {
      list.remove(i);
    }
  }
  return list;
}
 
Example 2
Source File: DocPreviewUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static boolean intersects(@Nonnull List<TextRange> ranges, int start, int end) {
  for (TextRange range : ranges) {
    if (range.intersectsStrict(start, end)) {
      return true;
    }
  }
  return false;
}
 
Example 3
Source File: InjectedLanguageBlockWrapper.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void collectBlocksIntersectingRange(final List<Block> list, final List<Block> result, @Nonnull final TextRange range) {
  for (Block block : list) {
    final TextRange textRange = block.getTextRange();
    if (block instanceof InjectedLanguageBlockWrapper && block.getTextRange().equals(range)) {
      continue;
    }
    if (range.contains(textRange)) {
      result.add(new InjectedLanguageBlockWrapper(block, myOffset, range, null, myLanguage));
    }
    else if (textRange.intersectsStrict(range)) {
      collectBlocksIntersectingRange(block.getSubBlocks(), result, range);
    }
  }
}
 
Example 4
Source File: SpeedSearchUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static void applySpeedSearchHighlighting(@Nonnull JComponent speedSearchEnabledComponent,
                                                @Nonnull SimpleColoredComponent coloredComponent,
                                                boolean mainTextOnly,
                                                boolean selected) {
  SpeedSearchSupply speedSearch = SpeedSearchSupply.getSupply(speedSearchEnabledComponent);
  // The bad thing is that SpeedSearch model is decoupled from UI presentation so we don't know the real matched text.
  // Our best guess is to get strgin from the ColoredComponent. We can only provide main-text-only option.
  Iterable<TextRange> ranges = speedSearch == null ? null : speedSearch.matchingFragments(coloredComponent.getCharSequence(mainTextOnly).toString());
  Iterator<TextRange> rangesIterator = ranges != null ? ranges.iterator() : null;
  if (rangesIterator == null || !rangesIterator.hasNext()) return;
  Color bg = selected ? UIUtil.getTreeSelectionBackground() : UIUtil.getTreeTextBackground();

  SimpleColoredComponent.ColoredIterator coloredIterator = coloredComponent.iterator();
  TextRange range = rangesIterator.next();
  main: while (coloredIterator.hasNext()) {
    coloredIterator.next();
    int offset = coloredIterator.getOffset();
    int endOffset = coloredIterator.getEndOffset();
    if (!range.intersectsStrict(offset, endOffset)) continue;
    SimpleTextAttributes attributes = coloredIterator.getTextAttributes();
    SimpleTextAttributes highlighted = new SimpleTextAttributes(bg, attributes.getFgColor(), null, attributes.getStyle() | SimpleTextAttributes.STYLE_SEARCH_MATCH);
    if (range.getStartOffset() > offset) {
      offset = coloredIterator.split(range.getStartOffset() - offset, attributes);
    }
    do {
      if (range.getEndOffset() <= endOffset) {
        offset = coloredIterator.split(range.getEndOffset() - offset, highlighted);
        if (rangesIterator.hasNext()) {
          range = rangesIterator.next();
        }
        else {
          break main;
        }
      }
      else {
        coloredIterator.split(endOffset - offset, highlighted);
        continue main;
      }
    }
    while (range.intersectsStrict(offset, endOffset));
  }
}
 
Example 5
Source File: PathReferenceManagerImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static boolean intersect(final TextRange range1, final TextRange range2) {
  return range2.intersectsStrict(range1) || range2.intersects(range1) && (range1.isEmpty() || range2.isEmpty());
}
 
Example 6
Source File: BlockUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static List<Block> mergeBlocks(@Nonnull List<TemplateLanguageBlock> tlBlocks, @Nonnull List<DataLanguageBlockWrapper> foreignBlocks) {
  ArrayList<Block> result = new ArrayList<Block>(tlBlocks.size() + foreignBlocks.size());
  int vInd = 0;
  int fInd = 0;
  while (vInd < tlBlocks.size() && fInd < foreignBlocks.size()) {
    final TemplateLanguageBlock v = tlBlocks.get(vInd);
    final DataLanguageBlockWrapper f = foreignBlocks.get(fInd);
    final TextRange vRange = v.getTextRange();
    final TextRange fRange = f.getTextRange();
    if (vRange.getStartOffset() >= fRange.getEndOffset()) {
      // add leading foreign blocks
      result.add(f);
      fInd++;
    }
    else if (vRange.getEndOffset() <= fRange.getStartOffset()) {
      // add leading TL blocks
      result.add(v);
      vInd++;
    }
    else if (vRange.getStartOffset() < fRange.getStartOffset() ||
             vRange.getStartOffset() == fRange.getStartOffset() && vRange.getEndOffset() >= fRange.getEndOffset()) {
      // add including TL blocks and split intersecting foreign blocks
      result.add(v);
      while (fInd < foreignBlocks.size() && vRange.contains(foreignBlocks.get(fInd).getTextRange())) {
        v.addForeignChild(foreignBlocks.get(fInd++));
      }
      if (fInd < foreignBlocks.size()) {
        final DataLanguageBlockWrapper notContainedF = foreignBlocks.get(fInd);
        if (vRange.intersectsStrict(notContainedF.getTextRange())) {
          Pair<List<DataLanguageBlockWrapper>, List<DataLanguageBlockWrapper>> splitBlocks = splitBlocksByRightBound(notContainedF.getOriginal(), vRange);
          v.addForeignChildren(splitBlocks.getFirst());
          foreignBlocks.remove(fInd);
          if (splitBlocks.getSecond().size() > 0) {
            foreignBlocks.addAll(fInd, splitBlocks.getSecond());
          }
        }
      }
      vInd++;
    }
    else if (vRange.getStartOffset() > fRange.getStartOffset() ||
             vRange.getStartOffset() == fRange.getStartOffset() && vRange.getEndOffset() < fRange.getEndOffset()) {
      // add including foreign blocks or split them if needed
      int lastContainedTlInd = vInd;
      while (lastContainedTlInd < tlBlocks.size() && fRange.intersectsStrict(tlBlocks.get(lastContainedTlInd).getTextRange())) {
        lastContainedTlInd++;
      }
      if (fRange.contains(tlBlocks.get(lastContainedTlInd - 1).getTextRange())) {
        result.add(f);
        fInd++;
        while (vInd < lastContainedTlInd) {
          f.addTlChild(tlBlocks.get(vInd++));
        }
      }
      else {
        foreignBlocks.remove(fInd);
        foreignBlocks.addAll(fInd, buildChildWrappers(f.getOriginal()));
      }
    }
  }
  while (vInd < tlBlocks.size()) {
    result.add(tlBlocks.get(vInd++));
  }
  while (fInd < foreignBlocks.size()) {
    result.add(foreignBlocks.get(fInd++));
  }
  return result;
}