Java Code Examples for com.intellij.util.DocumentUtil#isInsideSurrogatePair()

The following examples show how to use com.intellij.util.DocumentUtil#isInsideSurrogatePair() . 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: FoldingModelImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public FoldRegion createFoldRegion(int startOffset, int endOffset, @Nonnull String placeholder, @Nullable FoldingGroup group, boolean neverExpands) {
  assertIsDispatchThreadForEditor();
  if (!myIsBatchFoldingProcessing) {
    LOG.error("Fold regions must be added or removed inside batchFoldProcessing() only.");
    return null;
  }
  if (!isFoldingEnabled() || startOffset >= endOffset ||
      DocumentUtil.isInsideSurrogatePair(myEditor.getDocument(), startOffset) ||
      DocumentUtil.isInsideSurrogatePair(myEditor.getDocument(), endOffset) ||
      !myFoldTree.checkIfValidToCreate(startOffset, endOffset)) return null;

  FoldRegionImpl region = new FoldRegionImpl(myEditor, startOffset, endOffset, placeholder, group, neverExpands);
  myRegionTree.addInterval(region, startOffset, endOffset, false, false, false, 0);
  LOG.assertTrue(region.isValid());
  myFoldRegionsProcessed = true;
  if (group != null) {
    myGroups.putValue(group, region);
  }
  notifyListenersOnFoldRegionStateChange(region);
  LOG.assertTrue(region.isValid());
  return region;
}
 
Example 2
Source File: DesktopCaretImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
protected void changedUpdateImpl(@Nonnull DocumentEvent e) {
  super.changedUpdateImpl(e);
  if (isValid()) {
    int startOffset = intervalStart();
    int endOffset = intervalEnd();
    if (DocumentUtil.isInsideSurrogatePair(getDocument(), startOffset)) setIntervalStart(startOffset - 1);
    if (DocumentUtil.isInsideSurrogatePair(getDocument(), endOffset)) setIntervalStart(endOffset - 1);
  }
  if (endVirtualOffset > 0 && isValid()) {
    Document document = e.getDocument();
    int startAfter = intervalStart();
    int endAfter = intervalEnd();
    if (!DocumentUtil.isAtLineEnd(endAfter, document) || document.getLineNumber(startAfter) != document.getLineNumber(endAfter)) {
      resetVirtualSelection();
    }
  }
}
 
Example 3
Source File: SoftWrapEngine.java    From consulo with Apache License 2.0 6 votes vote down vote up
private int calcSoftWrapOffset(int minOffset, int maxOffset, boolean preferMinOffset) {
  if (canBreakBeforeOrAfterCodePoint(Character.codePointAt(myText, maxOffset))) return maxOffset;
  for (int i = 0, offset = maxOffset; i < BASIC_LOOK_BACK_LENGTH && offset >= minOffset; i++) {
    int prevOffset = Character.offsetByCodePoints(myText, offset, -1);
    if (canBreakBeforeOrAfterCodePoint(Character.codePointAt(myText, prevOffset))) return offset;
    //noinspection AssignmentToForLoopParameter
    offset = prevOffset;
  }

  if (myLineWrapPositionStrategy == null) {
    myLineWrapPositionStrategy = LanguageLineWrapPositionStrategy.INSTANCE.forEditor(myEditor);
  }

  int wrapOffset = myLineWrapPositionStrategy.calculateWrapPosition(myDocument, myEditor.getProject(), minOffset - 1, maxOffset + 1, maxOffset + 1, false, true);
  if (wrapOffset < 0) return preferMinOffset ? minOffset : maxOffset;
  if (wrapOffset < minOffset) return minOffset;
  if (wrapOffset > maxOffset) return maxOffset;
  if (DocumentUtil.isInsideSurrogatePair(myDocument, wrapOffset)) return wrapOffset - 1;
  return wrapOffset;
}
 
Example 4
Source File: InlineInlayImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected void changedUpdateImpl(@Nonnull DocumentEvent e) {
  myEditor.getInlayModel().myPutMergedIntervalsAtBeginning = intervalStart() == e.getOffset();
  super.changedUpdateImpl(e);
  if (isValid() && DocumentUtil.isInsideSurrogatePair(getDocument(), intervalStart())) {
    invalidate(e);
  }
}
 
Example 5
Source File: InlineInlayImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected void onReTarget(int startOffset, int endOffset, int destOffset) {
  InlayModelImpl inlayModel = myEditor.getInlayModel();
  inlayModel.myPutMergedIntervalsAtBeginning = intervalStart() == endOffset;
  if (DocumentUtil.isInsideSurrogatePair(getDocument(), getOffset())) {
    inlayModel.myMoveInProgress = true;
    try {
      invalidate("moved inside surrogate pair on retarget");
    }
    finally {
      inlayModel.myMoveInProgress = false;
    }
  }
}
 
Example 6
Source File: DesktopCaretImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected void onReTarget(int startOffset, int endOffset, int destOffset) {
  int offset = intervalStart();
  if (DocumentUtil.isInsideSurrogatePair(getDocument(), offset)) {
    setIntervalStart(offset - 1);
    setIntervalEnd(offset - 1);
  }
}
 
Example 7
Source File: DesktopCaretImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected void onReTarget(int startOffset, int endOffset, int destOffset) {
  int start = intervalStart();
  if (DocumentUtil.isInsideSurrogatePair(getDocument(), start)) {
    setIntervalStart(start - 1);
  }
  int end = intervalEnd();
  if (DocumentUtil.isInsideSurrogatePair(getDocument(), end)) {
    setIntervalStart(end - 1);
  }
}
 
Example 8
Source File: FoldRegionImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void alignToSurrogateBoundaries() {
  Document document = getDocument();
  int start = intervalStart();
  int end = intervalEnd();
  if (DocumentUtil.isInsideSurrogatePair(document, start)) {
    setIntervalStart(start - 1);
  }
  if (DocumentUtil.isInsideSurrogatePair(document, end)) {
    setIntervalEnd(end - 1);
  }
}
 
Example 9
Source File: SoftWrapApplianceManager.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void doRecalculateSoftWrapsRoughly(IncrementalCacheUpdateEvent event) {
  Document document = myEditor.getDocument();
  int lineCount = document.getLineCount();
  int offset = event.getStartOffset();
  int line = document.getLineNumber(offset);
  int mandatoryEnd = event.getMandatoryEndOffset();
  while (true) {
    if ((offset += QUICK_WRAP_CHAR_COUNT) >= document.getLineEndOffset(line)) {
      if (++line >= lineCount) {
        offset = document.getTextLength();
        break;
      }
      offset = document.getLineStartOffset(line);
      if (offset > mandatoryEnd && myEditor.getFoldingModel().getCollapsedRegionAtOffset(offset - 1) == null) break;
      else continue;
    }
    FoldRegion foldRegion = myEditor.getFoldingModel().getCollapsedRegionAtOffset(offset);
    if (foldRegion != null) {
      offset = foldRegion.getEndOffset();
      line = document.getLineNumber(offset);
    }
    if (DocumentUtil.isInsideSurrogatePair(document, offset)) offset++;
    if (offset < document.getLineEndOffset(line)) {
      SoftWrapImpl wrap = new SoftWrapImpl(new TextChangeImpl("\n", offset), 1, 1);
      myStorage.storeOrReplace(wrap);
      if (offset > mandatoryEnd && myDataMapper.matchesOldSoftWrap(wrap, event.getLengthDiff())) break;
    }
  }
  event.setActualEndOffset(offset);
}
 
Example 10
Source File: SoftWrapApplianceManager.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * This method is assumed to be called in a situation when visible area width is exceeded. It tries to create and register
 * new soft wrap which data is defined in accordance with the given parameters.
 * <p/>
 * There is a possible case that no soft wrap is created and registered. That is true, for example, for a situation when
 * we have a long line of text that doesn't contain white spaces, operators or any other symbols that may be used
 * as a {@code 'wrap points'}. We just left such lines as-is.
 *
 * @param minOffset       min line {@code 'wrap point'} offset
 * @param preferredOffset preferred {@code 'wrap point'} offset, i.e. max offset which symbol doesn't exceed right margin
 * @param maxOffset       max line {@code 'wrap point'} offset
 * @param spaceSize       current space width in pixels
 * @param lineData        object that encapsulates information about currently processed logical line
 * @return newly created and registered soft wrap if any; {@code null} otherwise
 */
@Nullable
private SoftWrapImpl registerSoftWrap(int minOffset, int preferredOffset, int maxOffset, int spaceSize, LogicalLineData lineData) {
  int softWrapOffset = calculateBackwardSpaceOffsetIfPossible(minOffset, preferredOffset);
  if (softWrapOffset < 0) {
    softWrapOffset = calculateBackwardOffsetForEasternLanguageIfPossible(minOffset, preferredOffset);
  }
  if (softWrapOffset < 0) {
    Document document = myEditor.getDocument();

    // Performance optimization implied by profiling results analysis.
    if (myLineWrapPositionStrategy == null) {
      myLineWrapPositionStrategy = LanguageLineWrapPositionStrategy.INSTANCE.forEditor(myEditor);
    }

    softWrapOffset = myLineWrapPositionStrategy.calculateWrapPosition(document, myEditor.getProject(), minOffset, maxOffset, preferredOffset, true, true);
    if (DocumentUtil.isInsideSurrogatePair(document, softWrapOffset)) softWrapOffset--;
  }

  if (softWrapOffset >= lineData.endLineOffset ||
      softWrapOffset < 0 ||
      softWrapOffset <= minOffset ||
      myCustomIndentUsedLastTime && softWrapOffset == lineData.nonWhiteSpaceSymbolOffset ||
      softWrapOffset > preferredOffset && myContext.lastFoldStartPosition != null // Prefer to wrap on fold region backwards
      && myContext.lastFoldStartPosition.offset <= preferredOffset)              // to wrapping forwards.
  {
    return null;
  }

  return registerSoftWrap(softWrapOffset, spaceSize, lineData);
}
 
Example 11
Source File: InlayModelImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public <T extends EditorCustomElementRenderer> Inlay<T> addInlineElement(int offset, boolean relatesToPrecedingText, @Nonnull T renderer) {
  ApplicationManager.getApplication().assertIsDispatchThread();
  Document document = myEditor.getDocument();
  if (DocumentUtil.isInsideSurrogatePair(document, offset)) return null;
  offset = Math.max(0, Math.min(document.getTextLength(), offset));
  InlineInlayImpl<T> inlay = new InlineInlayImpl<>(myEditor, offset, relatesToPrecedingText, renderer);
  notifyAdded(inlay);
  return inlay;
}
 
Example 12
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;
    }
  }
}