Java Code Examples for com.intellij.openapi.editor.event.DocumentEvent#isWholeTextReplaced()

The following examples show how to use com.intellij.openapi.editor.event.DocumentEvent#isWholeTextReplaced() . 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: DesktopCaretModelImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeDocumentChange(@Nonnull DocumentEvent e) {
  if (!myEditor.getDocument().isInBulkUpdate() && e.isWholeTextReplaced()) {
    for (DesktopCaretImpl caret : myCarets) {
      caret.updateCachedStateIfNeeded(); // logical position will be needed to restore caret position via diff
    }
  }
  myIsInUpdate = true;
  myVisualPositionUpdateScheduled = false;
}
 
Example 2
Source File: PersistentRangeMarkerUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * Answers if document region identified by the given range marker should be translated via diff algorithm on document change
 * identified by the given event.
 *
 * @param e             event that describes document change
 * @param rangeStart    target range marker start, for which update strategy should be selected
 * @param rangeEnd      target range marker end
 * @return              {@code true} if target document range referenced by the given range marker should be translated via
 *                      diff algorithm; {@code false} otherwise
 */
static boolean shouldTranslateViaDiff(@Nonnull DocumentEvent e, int rangeStart, int rangeEnd) {
  if (e.isWholeTextReplaced()) {
    // Perform translation if the whole text is replaced.
    return true;
  }

  if (e.getOffset() >= rangeEnd || e.getOffset() + e.getOldLength() <= rangeStart) {
    // Don't perform complex processing if the change doesn't affect target range.
    return false;
  }

  // Perform complex processing only if significant document part is updated.
  return (Math.max(e.getNewLength(), e.getOldLength()) * 1.0) / e.getDocument().getTextLength() >= 0.8;
}
 
Example 3
Source File: MarkerCache.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static UpdatedRanges applyEvents(@Nonnull List<? extends DocumentEvent> events, final UpdatedRanges struct) {
  FrozenDocument frozen = struct.myResultDocument;
  ManualRangeMarker[] resultMarkers = struct.myMarkers.clone();
  for (DocumentEvent event : events) {
    final FrozenDocument before = frozen;
    final DocumentEvent corrected;
    if (event instanceof RetargetRangeMarkers) {
      RetargetRangeMarkers retarget = (RetargetRangeMarkers)event;
      corrected = new RetargetRangeMarkers(frozen, retarget.getStartOffset(), retarget.getEndOffset(), retarget.getMoveDestinationOffset());
    }
    else {
      frozen = frozen.applyEvent(event, 0);
      corrected = new DocumentEventImpl(frozen, event.getOffset(), event.getOldFragment(), event.getNewFragment(), event.getOldTimeStamp(), event.isWholeTextReplaced(),
                                        ((DocumentEventImpl)event).getInitialStartOffset(), ((DocumentEventImpl)event).getInitialOldLength());
    }

    int i = 0;
    while (i < resultMarkers.length) {
      ManualRangeMarker currentRange = resultMarkers[i];

      int sameMarkersEnd = i + 1;
      while (sameMarkersEnd < resultMarkers.length && resultMarkers[sameMarkersEnd] == currentRange) {
        sameMarkersEnd++;
      }

      ManualRangeMarker updatedRange = currentRange == null ? null : currentRange.getUpdatedRange(corrected, before);
      while (i < sameMarkersEnd) {
        resultMarkers[i] = updatedRange;
        i++;
      }
    }
  }
  return new UpdatedRanges(struct.myEventCount + events.size(), frozen, struct.mySortedInfos, resultMarkers);
}
 
Example 4
Source File: DesktopCaretImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
protected void changedUpdateImpl(@Nonnull DocumentEvent e) {
  int oldOffset = intervalStart();
  super.changedUpdateImpl(e);
  if (isValid()) {
    // Under certain conditions, when text is inserted at caret position, we position caret at the end of inserted text.
    // Ideally, client code should be responsible for positioning caret after document modification, but in case of
    // postponed formatting (after PSI modifications), this is hard to implement, so a heuristic below is used.
    if (e.getOldLength() == 0 &&
        oldOffset == e.getOffset() &&
        !Boolean.TRUE.equals(myEditor.getUserData(DesktopEditorImpl.DISABLE_CARET_SHIFT_ON_WHITESPACE_INSERTION)) &&
        needToShiftWhiteSpaces(e)) {
      int afterInserted = e.getOffset() + e.getNewLength();
      setIntervalStart(afterInserted);
      setIntervalEnd(afterInserted);
    }
    int offset = intervalStart();
    if (DocumentUtil.isInsideSurrogatePair(getDocument(), offset)) {
      setIntervalStart(offset - 1);
      setIntervalEnd(offset - 1);
    }
  }
  else {
    setValid(true);
    int newOffset = Math.min(intervalStart(), e.getOffset() + e.getNewLength());
    if (!e.getDocument().isInBulkUpdate() && e.isWholeTextReplaced()) {
      try {
        final int line = ((DocumentEventImpl)e).translateLineViaDiff(myLogicalCaret.line);
        newOffset = myEditor.logicalPositionToOffset(new LogicalPosition(line, myLogicalCaret.column));
      }
      catch (FilesTooBigForDiffException ex) {
        LOG.info(ex);
      }
    }
    newOffset = DocumentUtil.alignToCodePointBoundary(getDocument(), newOffset);
    setIntervalStart(newOffset);
    setIntervalEnd(newOffset);
  }
  myLogicalColumnAdjustment = 0;
  myVisualColumnAdjustment = 0;
  if (oldOffset >= e.getOffset() && oldOffset <= e.getOffset() + e.getOldLength() && e.getNewLength() == 0 && e.getOldLength() > 0) {
    int inlaysToTheLeft = myEditor.getInlayModel().getInlineElementsInRange(e.getOffset(), e.getOffset()).size();
    boolean hasInlaysToTheRight = myEditor.getInlayModel().hasInlineElementAt(e.getOffset() + e.getOldLength());
    if (inlaysToTheLeft > 0 || hasInlaysToTheRight) {
      myLeansTowardsLargerOffsets = !hasInlaysToTheRight;
      myVisualColumnAdjustment = hasInlaysToTheRight ? inlaysToTheLeft : 0;
    }
    else if (oldOffset == e.getOffset()) {
      myLeansTowardsLargerOffsets = false;
    }
  }
}