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

The following examples show how to use com.intellij.openapi.editor.RangeMarker#dispose() . 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: LSPIJUtils.java    From intellij-quarkus with Eclipse Public License 2.0 6 votes vote down vote up
public static void applyEdit(Editor editor, TextEdit textEdit, Document document) {
    RangeMarker marker = document.createRangeMarker(LSPIJUtils.toOffset(textEdit.getRange().getStart(), document), LSPIJUtils.toOffset(textEdit.getRange().getEnd(), document));
    int startOffset = marker.getStartOffset();
    int endOffset = marker.getEndOffset();
    String text = textEdit.getNewText();
    if (text != null) {
        text = text.replaceAll("\r", "");
    }
    if (text == null || "".equals(text)) {
        document.deleteString(startOffset, endOffset);
    } else if (endOffset - startOffset <= 0) {
        document.insertString(startOffset, text);
    } else {
        document.replaceString(startOffset, endOffset, text);
    }
    if (text != null && !"".equals(text)) {
        editor.getCaretModel().moveCaretRelatively(text.length(), 0, false, false, true);
    }
    marker.dispose();
}
 
Example 2
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 3
Source File: CodeInsightUtilCore.java    From consulo with Apache License 2.0 6 votes vote down vote up
@RequiredReadAction
public static <T extends PsiElement> T forcePsiPostprocessAndRestoreElement(@Nonnull T element,
                                                                            boolean useFileLanguage) {
  final PsiFile psiFile = element.getContainingFile();
  final Document document = psiFile.getViewProvider().getDocument();
  //if (document == null) return element;
  final Language language = useFileLanguage ? psiFile.getLanguage() : PsiUtilCore.getDialect(element);
  final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(psiFile.getProject());
  final RangeMarker rangeMarker = document.createRangeMarker(element.getTextRange());
  documentManager.doPostponedOperationsAndUnblockDocument(document);
  documentManager.commitDocument(document);

  T elementInRange = findElementInRange(psiFile, rangeMarker.getStartOffset(), rangeMarker.getEndOffset(),
                                        (Class<? extends T>)element.getClass(),
                                        language, element);
  rangeMarker.dispose();
  return elementInRange;
}
 
Example 4
Source File: FileStatusMap.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void setDirtyScope(int passId, RangeMarker scope) {
  RangeMarker marker = dirtyScopes.get(passId);
  if (marker != scope) {
    if (marker != null) {
      marker.dispose();
    }
    dirtyScopes.put(passId, scope);
  }
}
 
Example 5
Source File: OffsetMap.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void dispose() {
  synchronized (myMap) {
    myDisposed = true;
    for (RangeMarker rangeMarker : myMap.values()) {
      rangeMarker.dispose();
    }
  }
}
 
Example 6
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 7
Source File: DocumentFoldingInfo.java    From consulo with Apache License 2.0 5 votes vote down vote up
void clear() {
  myInfos.clear();
  for (RangeMarker marker : myRangeMarkers) {
    marker.dispose();
  }
  myRangeMarkers.clear();
}
 
Example 8
Source File: PostprocessReformattingAspect.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void dispose() {
  for (Pair<Integer, RangeMarker> pair : myRangesToReindent) {
    RangeMarker marker = pair.second;
    if (marker.isValid()) {
      marker.dispose();
    }
  }
}
 
Example 9
Source File: SearchResults.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void exclude(FindResult occurrence) {
  boolean include = false;
  for (RangeMarker rangeMarker : myExcluded) {
    if (TextRange.areSegmentsEqual(rangeMarker, occurrence)) {
      myExcluded.remove(rangeMarker);
      rangeMarker.dispose();
      include = true;
      break;
    }
  }
  if (!include) {
    myExcluded.add(myEditor.getDocument().createRangeMarker(occurrence.getStartOffset(), occurrence.getEndOffset(), true));
  }
  notifyChanged();
}
 
Example 10
Source File: SearchResults.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void updateExcluded() {
  Set<RangeMarker> invalid = new HashSet<>();
  for (RangeMarker marker : myExcluded) {
    if (!marker.isValid()) {
      invalid.add(marker);
      marker.dispose();
    }
  }
  myExcluded.removeAll(invalid);
}
 
Example 11
Source File: InplaceVariableIntroducer.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected void moveOffsetAfter(boolean success) {
  super.moveOffsetAfter(success);
  if (myOccurrenceMarkers != null) {
    for (RangeMarker marker : myOccurrenceMarkers) {
      marker.dispose();
    }
  }
  if (myExprMarker != null && !isRestart()) {
    myExprMarker.dispose();
  }
}
 
Example 12
Source File: TemplateSegments.java    From consulo with Apache License 2.0 4 votes vote down vote up
public void removeAll() {
  for (RangeMarker segment : mySegments) {
    segment.dispose();
  }
  mySegments.clear();
}