下面列出了com.intellij.psi.util.PsiElementFilter#com.intellij.openapi.editor.RangeMarker 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
@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);
}
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;
}
@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();
}
/**
* @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;
}
}
@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;
}
});
}
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);
}
}
}
}
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, " ");
}
}
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)));
}
}
}
@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;
}
@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;
}
public MyDocumentsSynchronizer(@javax.annotation.Nullable Project project,
@Nonnull RangeMarker range,
@Nonnull Document document1,
@Nonnull Document document2) {
super(project, document1, document2);
myRangeMarker = range;
}
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();
}
@Override
public void dispose() {
synchronized (myMap) {
myDisposed = true;
for (RangeMarker rangeMarker : myMap.values()) {
rangeMarker.dispose();
}
}
}
protected void initOccurrencesMarkers() {
if (myOccurrenceMarkers != null) return;
myOccurrenceMarkers = new ArrayList<RangeMarker>();
for (E occurrence : myOccurrences) {
myOccurrenceMarkers.add(createMarker(occurrence));
}
}
public void testGreedyEnd() throws Exception {
configureFromFileText("x.txt", "012345678");
{
RangeMarker guard = createGuard(0, 5);
guard.setGreedyToLeft(true);
guard.setGreedyToRight(true);
}
checkUnableToTypeIn(5);
}
private void setDirtyScope(int passId, RangeMarker scope) {
RangeMarker marker = dirtyScopes.get(passId);
if (marker != scope) {
if (marker != null) {
marker.dispose();
}
dirtyScopes.put(passId, scope);
}
}
public int getLine() {
RangeMarker marker = myTarget.getRangeMarker();
if (marker != null && marker.isValid()) {
Document document = marker.getDocument();
return document.getLineNumber(marker.getStartOffset());
}
return myTarget.getLine();
}
public boolean isExcluded(FindResult occurrence) {
for (RangeMarker rangeMarker : myExcluded) {
if (TextRange.areSegmentsEqual(rangeMarker, occurrence)) {
return true;
}
}
return false;
}
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<>();
}
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);
}
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;
}
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;
}
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();
}
@Override
protected void moveOffsetAfter(boolean success) {
super.moveOffsetAfter(success);
if (myOccurrenceMarkers != null) {
for (RangeMarker marker : myOccurrenceMarkers) {
marker.dispose();
}
}
if (myExprMarker != null && !isRestart()) {
myExprMarker.dispose();
}
}
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;
}
@Override
public void dispose() {
for (Pair<Integer, RangeMarker> pair : myRangesToReindent) {
RangeMarker marker = pair.second;
if (marker.isValid()) {
marker.dispose();
}
}
}
@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;
}
@Override
@Nonnull
public RangeMarker createRangeMarker(@Nonnull final VirtualFile file, final int offset) {
return ApplicationManager.getApplication().runReadAction(new Computable<RangeMarker>() {
@Override
public RangeMarker compute() {
// even for already loaded document do not create range marker yet - wait until it really needed when e.g. user clicked to jump to OpenFileDescriptor
final LazyMarker marker = new OffsetLazyMarker(file, offset);
addToLazyMarkersList(marker, file);
return marker;
}
});
}
public void replaceElement (PsiElement element, String varName, String dependantVariableName, boolean alwaysStopAt) {
final RangeMarker key = wrapElement(element);
myAlwaysStopAtMap.put(key, alwaysStopAt ? Boolean.TRUE : Boolean.FALSE);
myVariableNamesMap.put(key, varName);
myVariableExpressions.put(key, dependantVariableName);
myElements.add(key);
}