com.intellij.openapi.editor.RangeMarker Java Examples

The following examples show how to use com.intellij.openapi.editor.RangeMarker. 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: LazyRangeMarkerFactoryImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
@Nonnull
public RangeMarker createRangeMarker(@Nonnull final VirtualFile file, final int line, final int column, final boolean persistent) {
  return ApplicationManager.getApplication().runReadAction(new Computable<RangeMarker>() {
    @Override
    public RangeMarker compute() {
      final Document document = FileDocumentManager.getInstance().getCachedDocument(file);
      if (document != null) {
        int myTabSize = CodeStyleFacade.getInstance(myProject).getTabSize(file.getFileType());
        final int offset = calculateOffset(document, line, column, myTabSize);
        return document.createRangeMarker(offset, offset, persistent);
      }

      final LazyMarker marker = new LineColumnLazyMarker(myProject, file, line, column);
      addToLazyMarkersList(marker, file);
      return marker;
    }
  });
}
 
Example #2
Source File: HighlightInfoComposite.java    From consulo with Apache License 2.0 6 votes vote down vote up
private HighlightInfoComposite(@Nonnull List<? extends HighlightInfo> infos, @Nonnull HighlightInfo anchorInfo) {
  super(null, null, anchorInfo.type, anchorInfo.startOffset, anchorInfo.endOffset, createCompositeDescription(infos), createCompositeTooltip(infos), anchorInfo.type.getSeverity(null), false, null,
        false, 0, anchorInfo.getProblemGroup(), null, anchorInfo.getGutterIconRenderer());
  highlighter = anchorInfo.getHighlighter();
  setGroup(anchorInfo.getGroup());
  List<Pair<IntentionActionDescriptor, RangeMarker>> markers = ContainerUtil.emptyList();
  List<Pair<IntentionActionDescriptor, TextRange>> ranges = ContainerUtil.emptyList();
  for (HighlightInfo info : infos) {
    if (info.quickFixActionMarkers != null) {
      if (markers == ContainerUtil.<Pair<IntentionActionDescriptor, RangeMarker>>emptyList()) markers = new ArrayList<>();
      markers.addAll(info.quickFixActionMarkers);
    }
    if (info.quickFixActionRanges != null) {
      if (ranges == ContainerUtil.<Pair<IntentionActionDescriptor, TextRange>>emptyList()) ranges = new ArrayList<>();
      ranges.addAll(info.quickFixActionRanges);
    }
  }
  quickFixActionMarkers = ContainerUtil.createLockFreeCopyOnWriteList(markers);
  quickFixActionRanges = ContainerUtil.createLockFreeCopyOnWriteList(ranges);
}
 
Example #3
Source File: ShowIntentionsPass.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static boolean markActionInvoked(@Nonnull Project project, @Nonnull final Editor editor, @Nonnull IntentionAction action) {
  final int offset = ((EditorEx)editor).getExpectedCaretOffset();

  List<HighlightInfo> infos = new ArrayList<>();
  DaemonCodeAnalyzerImpl.processHighlightsNearOffset(editor.getDocument(), project, HighlightSeverity.INFORMATION, offset, true, new CommonProcessors.CollectProcessor<>(infos));
  boolean removed = false;
  for (HighlightInfo info : infos) {
    if (info.quickFixActionMarkers != null) {
      for (Pair<HighlightInfo.IntentionActionDescriptor, RangeMarker> pair : info.quickFixActionMarkers) {
        HighlightInfo.IntentionActionDescriptor actionInGroup = pair.first;
        if (actionInGroup.getAction() == action) {
          // no CME because the list is concurrent
          removed |= info.quickFixActionMarkers.remove(pair);
        }
      }
    }
  }
  return removed;
}
 
Example #4
Source File: OffsetMap.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * @param key key
 * @return offset An offset registered earlier with this key.
 * -1 if offset wasn't registered or became invalidated due to document changes
 */
public int getOffset(OffsetKey key) {
  synchronized (myMap) {
    final RangeMarker marker = myMap.get(key);
    if (marker == null) throw new IllegalArgumentException("Offset " + key + " is not registered");
    if (!marker.isValid()) {
      removeOffset(key);
      throw new IllegalStateException("Offset " + key + " is invalid: " + marker);
    }

    final int endOffset = marker.getEndOffset();
    if (marker.getStartOffset() != endOffset) {
      saveOffset(key, endOffset, false);
    }
    return endOffset;
  }
}
 
Example #5
Source File: MemberInplaceRenamer.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
public PsiElement getSubstituted() {
  if (mySubstituted != null && mySubstituted.isValid()){
    if (mySubstituted instanceof PsiNameIdentifierOwner) {
      if (Comparing.strEqual(myOldName, ((PsiNameIdentifierOwner)mySubstituted).getName())) return mySubstituted;

      final RangeMarker rangeMarker = mySubstitutedRange != null ? mySubstitutedRange : myRenameOffset;
      if (rangeMarker != null) return PsiTreeUtil.getParentOfType(mySubstituted.getContainingFile().findElementAt(rangeMarker.getStartOffset()), PsiNameIdentifierOwner.class);
    }
    return mySubstituted;
  }
  if (mySubstitutedRange != null) {
    final PsiFile psiFile = PsiDocumentManager.getInstance(myProject).getPsiFile(myEditor.getDocument());
    if (psiFile != null) {
      return PsiTreeUtil.getParentOfType(psiFile.findElementAt(mySubstitutedRange.getStartOffset()), PsiNameIdentifierOwner.class);
    }
  }
  return getVariable();
}
 
Example #6
Source File: FileStatusMap.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
private static RangeMarker combineScopes(RangeMarker old, @Nonnull TextRange scope, int textLength, @Nonnull Document document) {
  if (old == null) {
    if (scope.equalsToRange(0, textLength)) return WHOLE_FILE_DIRTY_MARKER;
    return document.createRangeMarker(scope);
  }
  if (old == WHOLE_FILE_DIRTY_MARKER) return old;
  TextRange oldRange = TextRange.create(old);
  TextRange union = scope.union(oldRange);
  if (old.isValid() && union.equals(oldRange)) {
    return old;
  }
  if (union.getEndOffset() > textLength) {
    union = union.intersection(new TextRange(0, textLength));
  }
  assert union != null;
  return document.createRangeMarker(union);
}
 
Example #7
Source File: TemplateBuilderImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
public Template buildInlineTemplate() {
  Template template = buildTemplate();
  template.setInline(true);

  ApplicationManager.getApplication().assertWriteAccessAllowed();

  //this is kinda hacky way of doing things, but have not got a better idea
  for (RangeMarker element : myElements) {
    if (element != myEndElement) {
      myDocument.deleteString(element.getStartOffset(), element.getEndOffset());
    }
  }

  return template;
}
 
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: StatisticsUpdate.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void trackStatistics(InsertionContext context) {
  if (ourPendingUpdate != this) {
    return;
  }

  if (!context.getOffsetMap().containsOffset(CompletionInitializationContext.START_OFFSET)) {
    return;
  }

  final Document document = context.getDocument();
  int startOffset = context.getStartOffset();
  int tailOffset = context.getEditor().getCaretModel().getOffset();
  if (startOffset < 0 || tailOffset <= startOffset) {
    return;
  }

  final RangeMarker marker = document.createRangeMarker(startOffset, tailOffset);
  final DocumentAdapter listener = new DocumentAdapter() {
    @Override
    public void beforeDocumentChange(DocumentEvent e) {
      if (!marker.isValid() || e.getOffset() > marker.getStartOffset() && e.getOffset() < marker.getEndOffset()) {
        cancelLastCompletionStatisticsUpdate();
      }
    }
  };

  ourStatsAlarm.addRequest(() -> {
    if (ourPendingUpdate == this) {
      applyLastCompletionStatisticsUpdate();
    }
  }, 20 * 1000);

  document.addDocumentListener(listener);
  Disposer.register(this, () -> {
    document.removeDocumentListener(listener);
    marker.dispose();
    ourStatsAlarm.cancelAllRequests();
  });
}
 
Example #11
Source File: ConsoleLog.java    From aem-ide-tooling-4-intellij with Apache License 2.0 5 votes vote down vote up
private static void indentNewLines(DocumentImpl logDoc, List<RangeMarker> lineSeparators, RangeMarker afterTitle, boolean hasHtml, String indent) {
    if(!hasHtml) {
        int i = -1;
        while(true) {
            i = StringUtil.indexOf(logDoc.getText(), '\n', i + 1);
            if(i < 0) {
                break;
            }
            lineSeparators.add(logDoc.createRangeMarker(i, i + 1));
        }
    }
    if(!lineSeparators.isEmpty() && afterTitle != null && afterTitle.isValid()) {
        lineSeparators.add(afterTitle);
    }
    int nextLineStart = -1;
    for(RangeMarker separator : lineSeparators) {
        if(separator.isValid()) {
            int start = separator.getStartOffset();
            if(start == nextLineStart) {
                continue;
            }

            logDoc.replaceString(start, separator.getEndOffset(), "\n" + indent);
            nextLineStart = start + 1 + indent.length();
            while(nextLineStart < logDoc.getTextLength() && Character.isWhitespace(logDoc.getCharsSequence().charAt(nextLineStart))) {
                logDoc.deleteString(nextLineStart, nextLineStart + 1);
            }
        }
    }
}
 
Example #12
Source File: PantsHighlightingIntegrationTest.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
@Nullable
protected <T> T findIntention(@NotNull HighlightInfo info, @NotNull Class<T> aClass) {
  for (Pair<HighlightInfo.IntentionActionDescriptor, RangeMarker> pair : info.quickFixActionMarkers) {
    final HighlightInfo.IntentionActionDescriptor intensionDescriptor = pair.getFirst();
    final IntentionAction action = intensionDescriptor.getAction();
    if (aClass.isInstance(action)) {
      //noinspection unchecked
      return (T)action;
    }
  }
  return null;
}
 
Example #13
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 #14
Source File: PostprocessReformattingAspect.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public int compareTo(@Nonnull PostprocessFormattingTask o) {
  RangeMarker o1 = myRange;
  RangeMarker o2 = o.myRange;
  if (o1.equals(o2)) return 0;
  final int diff = o2.getEndOffset() - o1.getEndOffset();
  if (diff == 0) {
    if (o1.getStartOffset() == o2.getStartOffset()) return 0;
    if (o1.getStartOffset() == o1.getEndOffset()) return -1; // empty ranges first
    if (o2.getStartOffset() == o2.getEndOffset()) return 1; // empty ranges first
    return o1.getStartOffset() - o2.getStartOffset();
  }
  return diff;
}
 
Example #15
Source File: DocumentFragmentContent.java    From consulo with Apache License 2.0 5 votes vote down vote up
public MyDocumentsSynchronizer(@javax.annotation.Nullable Project project,
                               @Nonnull RangeMarker range,
                               @Nonnull Document document1,
                               @Nonnull Document document2) {
  super(project, document1, document2);
  myRangeMarker = range;
}
 
Example #16
Source File: ConsoleLog.java    From aem-ide-tooling-4-intellij with Apache License 2.0 5 votes vote down vote up
private static void removeJavaNewLines(Document document, List<RangeMarker> lineSeparators, boolean hasHtml) {
    CharSequence text = document.getCharsSequence();
    int i = 0;
    while(true) {
        i = StringUtil.indexOf(text, '\n', i);
        if(i < 0) {
            break;
        }
        document.deleteString(i, i + 1);
        if(!hasHtml) {
            lineSeparators.add(document.createRangeMarker(TextRange.from(i, 0)));
        }
    }
}
 
Example #17
Source File: ConsoleLog.java    From aem-ide-tooling-4-intellij with Apache License 2.0 5 votes vote down vote up
private static void insertNewLineSubstitutors(Document document, AtomicBoolean showMore, List<RangeMarker> lineSeparators) {
    for(RangeMarker marker : lineSeparators) {
        if(!marker.isValid()) {
            showMore.set(true);
            continue;
        }

        int offset = marker.getStartOffset();
        if(offset == 0 || offset == document.getTextLength()) {
            continue;
        }
        boolean spaceBefore = offset > 0 && Character.isWhitespace(document.getCharsSequence().charAt(offset - 1));
        if(offset < document.getTextLength()) {
            boolean spaceAfter = Character.isWhitespace(document.getCharsSequence().charAt(offset));
            int next = CharArrayUtil.shiftForward(document.getCharsSequence(), offset, " \t");
            if(next < document.getTextLength() && !Character.isLowerCase(document.getCharsSequence().charAt(next))) {
                document.insertString(offset, (spaceBefore ? "" : " ") + "//" + (spaceAfter ? "" : " "));
                continue;
            }
            if(spaceAfter) {
                continue;
            }
        }
        if(spaceBefore) {
            continue;
        }

        document.insertString(offset, " ");
    }
}
 
Example #18
Source File: InplaceVariableIntroducer.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected void initOccurrencesMarkers() {
  if (myOccurrenceMarkers != null) return;
  myOccurrenceMarkers = new ArrayList<RangeMarker>();
  for (E occurrence : myOccurrences) {
    myOccurrenceMarkers.add(createMarker(occurrence));
  }
}
 
Example #19
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 #20
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 #21
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 #22
Source File: Bookmark.java    From consulo with Apache License 2.0 5 votes vote down vote up
public int getLine() {
  RangeMarker marker = myTarget.getRangeMarker();
  if (marker != null && marker.isValid()) {
    Document document = marker.getDocument();
    return document.getLineNumber(marker.getStartOffset());
  }
  return myTarget.getLine();
}
 
Example #23
Source File: SearchResults.java    From consulo with Apache License 2.0 5 votes vote down vote up
public boolean isExcluded(FindResult occurrence) {
  for (RangeMarker rangeMarker : myExcluded) {
    if (TextRange.areSegmentsEqual(rangeMarker, occurrence)) {
      return true;
    }
  }
  return false;
}
 
Example #24
Source File: TodoItemNode.java    From consulo with Apache License 2.0 5 votes vote down vote up
public TodoItemNode(Project project, @Nonnull SmartTodoItemPointer value, TodoTreeBuilder builder) {
  super(project, value, builder);
  RangeMarker rangeMarker = getValue().getRangeMarker();
  LOG.assertTrue(rangeMarker.isValid());

  myHighlightedRegions = new ArrayList<>();
  myAdditionalLines = new ArrayList<>();
}
 
Example #25
Source File: TemplateBuilderImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void replaceElement(PsiElement element, TextRange textRange, String primaryVariableName, String otherVariableName, boolean alwaysStopAt) {
  final RangeMarker key = myDocument.createRangeMarker(textRange.shiftRight(element.getTextRange().getStartOffset()));
  myAlwaysStopAtMap.put(key, alwaysStopAt ? Boolean.TRUE : Boolean.FALSE);
  myVariableNamesMap.put(key, primaryVariableName);
  myVariableExpressions.put(key, otherVariableName);
  myElements.add(key);
}
 
Example #26
Source File: IdeDocumentHistoryImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
public PlaceInfo(@Nonnull VirtualFile file, @Nonnull FileEditorState navigationState, @Nonnull String editorTypeId, @Nullable EditorWindow window, @Nullable RangeMarker caretPosition) {
  myNavigationState = navigationState;
  myFile = file;
  myEditorTypeId = editorTypeId;
  myWindow = new WeakReference<>(window);
  myCaretPosition = caretPosition;
  myTimeStamp = -1;
}
 
Example #27
Source File: IdeDocumentHistoryImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
public PlaceInfo(@Nonnull VirtualFile file,
                 @Nonnull FileEditorState navigationState,
                 @Nonnull String editorTypeId,
                 @Nullable EditorWindow window,
                 @Nullable RangeMarker caretPosition,
                 long stamp) {
  myNavigationState = navigationState;
  myFile = file;
  myEditorTypeId = editorTypeId;
  myWindow = new WeakReference<>(window);
  myCaretPosition = caretPosition;
  myTimeStamp = stamp;
}
 
Example #28
Source File: FoldingTransformation.java    From consulo with Apache License 2.0 5 votes vote down vote up
public FoldingTransformation(Editor editor) {
  myEditor = editor;
  FoldRegion[] foldRegions = myEditor.getFoldingModel().getAllFoldRegions();
  Arrays.sort(foldRegions, RangeMarker.BY_START_OFFSET);
  TIntArrayList foldBeginings = new TIntArrayList();
  for (FoldRegion foldRegion : foldRegions) {
    if (!foldRegion.isValid() || foldRegion.isExpanded()) continue;
    foldBeginings.add(getStartLine(foldRegion));
    myCollapsed.add(foldRegion);
  }
  myFoldBeginings = foldBeginings.toNativeArray();
}
 
Example #29
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 #30
Source File: TabOutScopesTrackerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
private List<RangeMarker> getCurrentScopes(boolean create) {
  Caret currentCaret = myEditor.getCaretModel().getCurrentCaret();
  List<RangeMarker> result = currentCaret.getUserData(TRACKED_SCOPES);
  if (result == null && create) {
    currentCaret.putUserData(TRACKED_SCOPES, result = new ArrayList<>());
  }
  return result;
}