com.intellij.openapi.util.UnfairTextRange Java Examples

The following examples show how to use com.intellij.openapi.util.UnfairTextRange. 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: ANTLRv4FoldingBuilder.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@SuppressWarnings("ConstantConditions")
@Nullable
private static TextRange getFileHeader(PsiElement file) {
    PsiElement first = file.getFirstChild();
    if (first instanceof PsiWhiteSpace) first = first.getNextSibling();
    PsiElement element = first;
    while (isComment(element)) {
        element = element.getNextSibling();
        if (element instanceof PsiWhiteSpace) {
            element = element.getNextSibling();
        } else {
            break;
        }
    }
    if (element == null) return null;
    if (element.getPrevSibling() instanceof PsiWhiteSpace) element = element.getPrevSibling();
    if (element == null || element.equals(first)) return null;
    return new UnfairTextRange(first.getTextOffset(), element.getTextOffset());
}
 
Example #2
Source File: RangeMarkerImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
private static TextRange processIfOnePoint(@Nonnull DocumentEvent e, int intervalStart, boolean greedyRight, boolean stickyRight) {
  int offset = e.getOffset();
  int oldLength = e.getOldLength();
  int oldEnd = offset + oldLength;
  if (offset < intervalStart && intervalStart < oldEnd) {
    return null;
  }

  if (offset == intervalStart && oldLength == 0) {
    if (greedyRight) {
      return new UnfairTextRange(intervalStart, intervalStart + e.getNewLength());
    }
    else if (stickyRight) {
      return new UnfairTextRange(intervalStart + e.getNewLength(), intervalStart + e.getNewLength());
    }
  }

  if (intervalStart > oldEnd || intervalStart == oldEnd && oldLength > 0) {
    return new UnfairTextRange(intervalStart + e.getNewLength() - oldLength, intervalStart + e.getNewLength() - oldLength);
  }

  return new UnfairTextRange(intervalStart, intervalStart);
}
 
Example #3
Source File: MarkerCache.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
TextRange getUpdatedRange(@Nonnull SelfElementInfo info, @Nonnull FrozenDocument frozen, @Nonnull List<? extends DocumentEvent> events) {
  UpdatedRanges struct = getUpdatedMarkers(frozen, events);
  int i = Collections.binarySearch(struct.mySortedInfos, info, INFO_COMPARATOR);
  ManualRangeMarker updated = i >= 0 ? struct.myMarkers[i] : null;
  return updated == null ? null : new UnfairTextRange(updated.getStartOffset(), updated.getEndOffset());
}
 
Example #4
Source File: RangeMarkerImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
static TextRange applyChange(@Nonnull DocumentEvent e, int intervalStart, int intervalEnd, boolean isGreedyToLeft, boolean isGreedyToRight, boolean isStickingToRight) {
  if (intervalStart == intervalEnd) {
    return processIfOnePoint(e, intervalStart, isGreedyToRight, isStickingToRight);
  }

  final int offset = e.getOffset();
  final int oldLength = e.getOldLength();
  final int newLength = e.getNewLength();

  // changes after the end.
  if (intervalEnd < offset) {
    return new UnfairTextRange(intervalStart, intervalEnd);
  }
  if (!isGreedyToRight && intervalEnd == offset) {
    // handle replaceString that was minimized and resulted in insertString at the range end
    if (e instanceof DocumentEventImpl && oldLength == 0 && ((DocumentEventImpl)e).getInitialStartOffset() < offset) {
      return new UnfairTextRange(intervalStart, intervalEnd + newLength);
    }
    return new UnfairTextRange(intervalStart, intervalEnd);
  }

  // changes before start
  if (intervalStart > offset + oldLength) {
    return new UnfairTextRange(intervalStart + newLength - oldLength, intervalEnd + newLength - oldLength);
  }
  if (!isGreedyToLeft && intervalStart == offset + oldLength) {
    // handle replaceString that was minimized and resulted in insertString at the range start
    if (e instanceof DocumentEventImpl && oldLength == 0 && ((DocumentEventImpl)e).getInitialStartOffset() + ((DocumentEventImpl)e).getInitialOldLength() > offset) {
      return new UnfairTextRange(intervalStart, intervalEnd + newLength);
    }
    return new UnfairTextRange(intervalStart + newLength - oldLength, intervalEnd + newLength - oldLength);
  }

  // Changes inside marker's area. Expand/collapse.
  if (intervalStart <= offset && intervalEnd >= offset + oldLength) {
    return new ProperTextRange(intervalStart, intervalEnd + newLength - oldLength);
  }

  // At this point we either have (myStart xor myEnd inside changed area) or whole area changed.

  // Replacing prefix or suffix...
  if (intervalStart >= offset && intervalStart <= offset + oldLength && intervalEnd > offset + oldLength) {
    return new ProperTextRange(offset + newLength, intervalEnd + newLength - oldLength);
  }

  if (intervalEnd >= offset && intervalEnd <= offset + oldLength && intervalStart < offset) {
    return new UnfairTextRange(intervalStart, offset);
  }

  return null;
}
 
Example #5
Source File: ParagraphFillHandler.java    From consulo with Apache License 2.0 4 votes vote down vote up
private TextRange getTextRange(@Nonnull final PsiElement element, @Nonnull final Editor editor) {
  int startOffset = getStartOffset(element, editor);
  int endOffset = getEndOffset(element, editor);
  return new UnfairTextRange(startOffset, endOffset);
}