@Nullable private static TextRange getFileHeader(PsiJavaFile file) { PsiElement first = file.getFirstChild(); if (first instanceof PsiWhiteSpace) first = first.getNextSibling(); PsiElement element = first; while (element instanceof PsiComment) { element = element.getNextSibling(); if (element instanceof PsiWhiteSpace) { element = element.getNextSibling(); } else { break; } } if (element == null) return null; if (element.getPrevSibling() instanceof PsiWhiteSpace) element = element.getPrevSibling(); if (element == null || element.equals(first)) return null; return new UnfairTextRange(first.getTextOffset(), element.getTextOffset()); }
@Override @NotNull protected Pair<TextRange, PsiElement> computeProblemRange() { final PsiReference reference = myReference; PsiElement element = reference.getElement(); if (element instanceof XmlAttributeValue && element.getTextLength() == 0) return NO_PROBLEM; final TextRange referenceRange = reference.getRangeInElement(); if (referenceRange.isEmpty()) { int startOffset = referenceRange.getStartOffset(); return element instanceof XmlAttributeValue ? Pair.create((TextRange)new UnfairTextRange(startOffset - 1, startOffset + 1), element) : Pair.create(TextRange.from(startOffset, 1), element); } return Pair.create(referenceRange, element); }
@Override public void getLanguagesToInject(@NotNull final MultiHostRegistrar registrar, @NotNull final PsiElement host) { Pair<ASTNode, ASTNode> pair = parseConditionalCommentBoundaries(host); if (pair == null) { return; } final TextRange textRange = host.getTextRange(); final int startOffset = textRange.getStartOffset(); Language language = host.getParent().getLanguage(); ASTNode conditionalStart = pair.first; ASTNode conditionalEnd = pair.second; TextRange range = new UnfairTextRange(conditionalStart.getTextRange().getEndOffset() - startOffset, conditionalEnd.getStartOffset() - startOffset); if (range.getStartOffset() < range.getEndOffset()) { registrar.startInjecting(language).addPlace(null, null, (PsiLanguageInjectionHost)host, range).doneInjecting(); } }
private static void addTagSelection2(PsiElement e, List<TextRange> result) { XmlTag tag = PsiTreeUtil.getParentOfType(e, XmlTag.class, true); while (tag != null) { result.add(tag.getTextRange()); final ASTNode tagStartEnd = XmlChildRole.START_TAG_END_FINDER.findChild(tag.getNode()); final ASTNode tagEndStart = XmlChildRole.CLOSING_TAG_START_FINDER.findChild(tag.getNode()); if (tagStartEnd != null && tagEndStart != null) { result.add(new UnfairTextRange(tagStartEnd.getTextRange().getEndOffset(), tagEndStart.getTextRange().getStartOffset())); } if (tagStartEnd != null) { result.add(new TextRange(tag.getTextRange().getStartOffset(), tagStartEnd.getTextRange().getEndOffset())); } if (tagEndStart != null) { result.add(new TextRange(tagEndStart.getTextRange().getStartOffset(), tag.getTextRange().getEndOffset())); } tag = PsiTreeUtil.getParentOfType(tag, XmlTag.class, true); } }
@SuppressWarnings("ConstantConditions") @Nullable private static TextRange getFileHeader(PsiElement file) { PsiElement first = file.getFirstChild(); if (first instanceof PsiWhiteSpace) first = first.getNextSibling(); PsiElement element = first; while (isComment(element)) { element = element.getNextSibling(); if (element instanceof PsiWhiteSpace) { element = element.getNextSibling(); } else { break; } } if (element == null) return null; if (element.getPrevSibling() instanceof PsiWhiteSpace) element = element.getPrevSibling(); if (element == null || element.equals(first)) return null; return new UnfairTextRange(first.getTextOffset(), element.getTextOffset()); }
@Nullable private static TextRange processIfOnePoint(@Nonnull DocumentEvent e, int intervalStart, boolean greedyRight, boolean stickyRight) { int offset = e.getOffset(); int oldLength = e.getOldLength(); int oldEnd = offset + oldLength; if (offset < intervalStart && intervalStart < oldEnd) { return null; } if (offset == intervalStart && oldLength == 0) { if (greedyRight) { return new UnfairTextRange(intervalStart, intervalStart + e.getNewLength()); } else if (stickyRight) { return new UnfairTextRange(intervalStart + e.getNewLength(), intervalStart + e.getNewLength()); } } if (intervalStart > oldEnd || intervalStart == oldEnd && oldLength > 0) { return new UnfairTextRange(intervalStart + e.getNewLength() - oldLength, intervalStart + e.getNewLength() - oldLength); } return new UnfairTextRange(intervalStart, intervalStart); }
@Override public TextRange getRangeInElement() { final TextRange textRange = getTextRange(); AppleScriptReferenceElement[] appleScriptReferences = PsiTreeUtil.getChildrenOfType(this, AppleScriptReferenceElement.class); if (appleScriptReferences != null && appleScriptReferences.length > 0) { TextRange lastReferenceRange = appleScriptReferences[appleScriptReferences.length - 1].getTextRange(); return new UnfairTextRange( lastReferenceRange.getStartOffset() - textRange.getStartOffset(), lastReferenceRange.getEndOffset() - textRange.getEndOffset() ); } return new UnfairTextRange(0, textRange.getEndOffset() - textRange.getStartOffset()); }
@Nullable private static List<TextRange> getExtendedAffectedRanges(FormatTextRanges formatTextRanges) { if (formatTextRanges == null) return null; List<FormatTextRanges.FormatTextRange> ranges = formatTextRanges.getRanges(); List<TextRange> extended = ContainerUtil.newArrayList(); final int extendOffset = 500; for (FormatTextRanges.FormatTextRange textRange : ranges) { TextRange range = textRange.getTextRange(); extended.add(new UnfairTextRange(range.getStartOffset() - extendOffset, range.getEndOffset() + extendOffset)); } return extended; }
/** * Searches elements in selection * * @param editor editor to get text selection * @param project Project * @param filter PsiElement filter, e.g. PsiMethodCallExpression.class * @param searchChildrenOfFound if true, visitor will look for matching elements in the children of a found element, otherwise will not look inside found element. * @param <T> type based on PsiElement type * @return elements in selection */ @NotNull public static <T extends PsiElement> List<T> searchElementsInSelection(Editor editor, Project project, final Class<T> filter, final boolean searchChildrenOfFound) { final SelectionModel selectionModel = editor.getSelectionModel(); if (!selectionModel.hasSelection()) { return Collections.emptyList(); } final TextRange selection = new UnfairTextRange(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd()); final PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument()); if (file == null || file instanceof PsiCompiledElement) { return Collections.emptyList(); } final List<T> results = new ArrayList<T>(); final PsiElementVisitor visitor = new JavaRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { if (!selection.intersects(element.getTextRange())) { return; } if (filter.isAssignableFrom(element.getClass())) { results.add((T)element); if (!searchChildrenOfFound) { return; } } super.visitElement(element); } }; file.accept(visitor); return results; }
@Nullable private static TextRange foldFileHeader(@NotNull final List<FoldingDescriptor> descriptors, @NotNull final SquirrelFile squirrelFile, @NotNull final Document document) { PsiElement firstComment = squirrelFile.getFirstChild(); if (firstComment instanceof PsiWhiteSpace) { firstComment = firstComment.getNextSibling(); } if (!(firstComment instanceof PsiComment)) return null; boolean containsCustomRegionMarker = false; PsiElement nextAfterComments = firstComment; while (nextAfterComments instanceof PsiComment || nextAfterComments instanceof PsiWhiteSpace) { containsCustomRegionMarker |= isCustomRegionElement(nextAfterComments); nextAfterComments = nextAfterComments.getNextSibling(); } if (nextAfterComments == null) return null; final TextRange fileHeaderCommentsRange = new UnfairTextRange(firstComment.getTextOffset(), nextAfterComments.getTextOffset() - 1); if (fileHeaderCommentsRange.getLength() > 1 && document.getLineNumber(fileHeaderCommentsRange.getEndOffset()) > document.getLineNumber(fileHeaderCommentsRange.getStartOffset())) { if (!containsCustomRegionMarker) { descriptors.add(new FoldingDescriptor(squirrelFile, fileHeaderCommentsRange)); } return fileHeaderCommentsRange; } return null; }
@Nullable TextRange getUpdatedRange(@Nonnull SelfElementInfo info, @Nonnull FrozenDocument frozen, @Nonnull List<DocumentEvent> events) { UpdatedRanges struct = getUpdatedMarkers(frozen, events); int i = Collections.binarySearch(struct.mySortedInfos, info, INFO_COMPARATOR); ManualRangeMarker updated = i >= 0 ? struct.myMarkers[i] : null; return updated == null ? null : new UnfairTextRange(updated.getStartOffset(), updated.getEndOffset()); }
public List<TextRange> getExtendedFormattingRanges() { return myStorage .getRanges() .stream() .map((range) -> { TextRange textRange = range.getTextRange(); return new UnfairTextRange(textRange.getStartOffset() - 500, textRange.getEndOffset() + 500); }) .collect(Collectors.toList()); }
@NotNull protected Pair<TextRange, PsiElement> computeProblemRange() { final PsiReference reference = myReference; PsiElement element = reference.getElement(); if (element instanceof XmlAttributeValue && element.getTextLength() == 0) return NO_PROBLEM; final TextRange referenceRange = reference.getRangeInElement(); if (referenceRange.isEmpty()) { int startOffset = referenceRange.getStartOffset(); return element instanceof XmlAttributeValue ? Pair.create((TextRange)new UnfairTextRange(startOffset - 1, startOffset + 1), element) : Pair.create(TextRange.from(startOffset, 1), element); } return Pair.create(referenceRange, element); }
@Nullable private static TextRange getFileHeader(@NotNull PsiJavaFile file) { PsiElement first = file.getFirstChild(); if(first instanceof PsiWhiteSpace) { first = first.getNextSibling(); } PsiElement element = first; while(element instanceof PsiComment) { element = element.getNextSibling(); if(element instanceof PsiWhiteSpace) { element = element.getNextSibling(); } else { break; } } if(element == null) { return null; } PsiElement prevSibling = element.getPrevSibling(); if(prevSibling instanceof PsiWhiteSpace) { element = prevSibling; } if(element.equals(first)) { return null; } return new UnfairTextRange(first.getTextOffset(), element.getTextOffset()); }
private TextRange getTextRange(@NotNull final PsiElement element, @NotNull final Editor editor) { int startOffset = getStartOffset(element, editor); int endOffset = getEndOffset(element, editor); return new UnfairTextRange(startOffset, endOffset); }
@Nullable static TextRange applyChange(@Nonnull DocumentEvent e, int intervalStart, int intervalEnd, boolean isGreedyToLeft, boolean isGreedyToRight, boolean isStickingToRight) { if (intervalStart == intervalEnd) { return processIfOnePoint(e, intervalStart, isGreedyToRight, isStickingToRight); } final int offset = e.getOffset(); final int oldLength = e.getOldLength(); final int newLength = e.getNewLength(); // changes after the end. if (intervalEnd < offset) { return new UnfairTextRange(intervalStart, intervalEnd); } if (!isGreedyToRight && intervalEnd == offset) { // handle replaceString that was minimized and resulted in insertString at the range end if (e instanceof DocumentEventImpl && oldLength == 0 && ((DocumentEventImpl)e).getInitialStartOffset() < offset) { return new UnfairTextRange(intervalStart, intervalEnd + newLength); } return new UnfairTextRange(intervalStart, intervalEnd); } // changes before start if (intervalStart > offset + oldLength) { return new UnfairTextRange(intervalStart + newLength - oldLength, intervalEnd + newLength - oldLength); } if (!isGreedyToLeft && intervalStart == offset + oldLength) { // handle replaceString that was minimized and resulted in insertString at the range start if (e instanceof DocumentEventImpl && oldLength == 0 && ((DocumentEventImpl)e).getInitialStartOffset() + ((DocumentEventImpl)e).getInitialOldLength() > offset) { return new UnfairTextRange(intervalStart - oldLength, intervalEnd + newLength - oldLength); } return new UnfairTextRange(intervalStart + newLength - oldLength, intervalEnd + newLength - oldLength); } // Changes inside marker's area. Expand/collapse. if (intervalStart <= offset && intervalEnd >= offset + oldLength) { return new ProperTextRange(intervalStart, intervalEnd + newLength - oldLength); } // At this point we either have (myStart xor myEnd inside changed area) or whole area changed. // Replacing prefix or suffix... if (intervalStart >= offset && intervalStart <= offset + oldLength && intervalEnd > offset + oldLength) { return new ProperTextRange(offset + newLength, intervalEnd + newLength - oldLength); } if (intervalEnd >= offset && intervalEnd <= offset + oldLength && intervalStart < offset) { return new UnfairTextRange(intervalStart, offset); } return null; }
private TextRange getTextRange(@Nonnull final PsiElement element, @Nonnull final Editor editor) { int startOffset = getStartOffset(element, editor); int endOffset = getEndOffset(element, editor); return new UnfairTextRange(startOffset, endOffset); }
private static void addTagSelection(CharSequence editorText, int cursorOffset, FileType fileType, EditorHighlighter highlighter, List<TextRange> result) { int start = cursorOffset; while (true) { if (start < 0) return; HighlighterIterator i = highlighter.createIterator(start); if (i.atEnd()) return; while (true) { if (i.getTokenType() == XmlTokenType.XML_START_TAG_START) break; i.retreat(); if (i.atEnd()) return; } start = i.getStart(); final boolean matched = BraceMatchingUtil.matchBrace(editorText, fileType, i, true); if (matched) { final int tagEnd = i.getEnd(); result.add(new TextRange(start, tagEnd)); HighlighterIterator j = highlighter.createIterator(start); while (!j.atEnd() && j.getTokenType() != XmlTokenType.XML_TAG_END) j.advance(); while (!i.atEnd() && i.getTokenType() != XmlTokenType.XML_END_TAG_START) i.retreat(); if (!i.atEnd() && !j.atEnd()) { result.add(new UnfairTextRange(j.getEnd(), i.getStart())); } if (!j.atEnd()) { result.add(new TextRange(start, j.getEnd())); } if (!i.atEnd()) { result.add(new TextRange(i.getStart(), tagEnd)); } } start--; } }