Java Code Examples for com.intellij.openapi.editor.RangeMarker#setGreedyToLeft()

The following examples show how to use com.intellij.openapi.editor.RangeMarker#setGreedyToLeft() . 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: HaxeDocumentModel.java    From intellij-haxe with Apache License 2.0 6 votes vote down vote up
/**
 * Replace the text within the given range and reformat it according to the user's
 * code style/formatting rules.
 *
 * NOTE: The PSI may be entirely invalidated and re-created by this call.
 *
 * @param range Range of text or PsiElements to replace.
 * @param text Replacement text (may be null).
 */
public void replaceAndFormat(@NotNull final TextRange range, @Nullable String text) {
  if (null == text) {
    text = "";
  }

  // Mark the beginning and end so that we have the proper range after adding text.
  // Greedy means that the text immediately added at the beginning/end of the marker are included.
  RangeMarker marker = document.createRangeMarker(range);
  marker.setGreedyToLeft(true);
  marker.setGreedyToRight(true);

  try {

    document.replaceString(range.getStartOffset(), range.getEndOffset(), text);

    //PsiDocumentManager.getInstance(file.getProject()).commitDocument(document); // force update PSI.

    if (marker.isValid()) { // If the range wasn't reduced to zero.
      CodeStyleManager.getInstance(file.getProject()).reformatText(file, marker.getStartOffset(), marker.getEndOffset());
    }
  }
  finally {
    marker.dispose();
  }
}
 
Example 2
Source File: TemplateSegments.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * IDEADEV-13618
 *
 * prevent two different segments to grow simultaneously if they both starts at the same offset.
 */
public void lockSegmentAtTheSameOffsetIfAny(final int number) {
  if (number == -1) {
    return;
  }

  final RangeMarker current = mySegments.get(number);
  int offset = current.getStartOffset();

  for (int i = 0; i < mySegments.size(); i++) {
    if (i != number) {
      final RangeMarker segment = mySegments.get(i);
      final int startOffset2 = segment.getStartOffset();
      if (offset == startOffset2) {
        segment.setGreedyToLeft(false);
      }
    }
  }
}
 
Example 3
Source File: DocumentFragmentContent.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static RangeMarker createRangeMarker(@Nonnull Document document, @Nonnull TextRange range) {
  RangeMarker rangeMarker = document.createRangeMarker(range.getStartOffset(), range.getEndOffset(), true);
  rangeMarker.setGreedyToLeft(true);
  rangeMarker.setGreedyToRight(true);
  return rangeMarker;
}
 
Example 4
Source File: GuardBlockTest.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void testZero() throws Exception {
  configureFromFileText("x.txt", "xxxx");
  RangeMarker guard = createGuard(0, 0);
  guard.setGreedyToLeft(true);
  guard.setGreedyToRight(true);
  checkUnableToTypeIn(0);
  checkCanTypeIn(1);
}
 
Example 5
Source File: GuardBlockTest.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void testGreedyEnd() throws Exception {
  configureFromFileText("x.txt", "012345678");
  {
    RangeMarker guard = createGuard(0, 5);
    guard.setGreedyToLeft(true);
    guard.setGreedyToRight(true);
  }
  checkUnableToTypeIn(5);
}
 
Example 6
Source File: TabOutScopesTrackerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void registerScope(int offset, int caretShift) {
  RangeMarker marker = myEditor.getDocument().createRangeMarker(offset, offset);
  marker.setGreedyToLeft(true);
  marker.setGreedyToRight(true);
  if (caretShift > 1) marker.putUserData(CARET_SHIFT, caretShift);
  getCurrentScopes(true).add(marker);
}
 
Example 7
Source File: TemplateSegments.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void replaceSegmentAt(int index, int start, int end, boolean preserveGreediness) {
  RangeMarker rangeMarker = mySegments.get(index);
  boolean greedyToLeft = rangeMarker.isGreedyToLeft();
  boolean greedyToRight = rangeMarker.isGreedyToRight();
  rangeMarker.dispose();

  Document doc = myEditor.getDocument();
  rangeMarker = doc.createRangeMarker(start, end);
  rangeMarker.setGreedyToLeft(greedyToLeft || !preserveGreediness);
  rangeMarker.setGreedyToRight(greedyToRight || !preserveGreediness);
  mySegments.set(index, rangeMarker);
}
 
Example 8
Source File: TemplateSegments.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void setNeighboursGreedy(final int segmentNumber, final boolean greedy) {
  if (segmentNumber > 0) {
    final RangeMarker left = mySegments.get(segmentNumber - 1);
    left.setGreedyToLeft(greedy);
    left.setGreedyToRight(greedy);
  }
  if (segmentNumber + 1 < mySegments.size()) {
    final RangeMarker right = mySegments.get(segmentNumber + 1);
    right.setGreedyToLeft(greedy);
    right.setGreedyToRight(greedy);
  }
}
 
Example 9
Source File: TemplateSegments.java    From consulo with Apache License 2.0 4 votes vote down vote up
public void setSegmentsGreedy(boolean greedy) {
  for (final RangeMarker segment : mySegments) {
    segment.setGreedyToRight(greedy);
    segment.setGreedyToLeft(greedy);
  }
}
 
Example 10
Source File: LookupOffsets.java    From consulo with Apache License 2.0 4 votes vote down vote up
private RangeMarker createLeftGreedyMarker(int start) {
  RangeMarker marker = myEditor.getDocument().createRangeMarker(start, start);
  marker.setGreedyToLeft(true);
  return marker;
}