Search in sources :

Example 6 with UnfairTextRange

use of com.intellij.openapi.util.UnfairTextRange in project intellij-community by JetBrains.

the class MarkerCache method getUpdatedRange.

@Nullable
TextRange getUpdatedRange(@NotNull SelfElementInfo info, @NotNull FrozenDocument frozen, @NotNull 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());
}
Also used : ManualRangeMarker(com.intellij.openapi.editor.impl.ManualRangeMarker) UnfairTextRange(com.intellij.openapi.util.UnfairTextRange) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with UnfairTextRange

use of com.intellij.openapi.util.UnfairTextRange in project intellij-community by JetBrains.

the class GenericPatchApplier method testForPartialContextMatch.

private boolean testForPartialContextMatch(final SplitHunk splitHunkWithExtendedContext, final MismatchSolver mismatchSolver, final int maxWalkFromBinding, @Nullable final SplitHunk originalSplitHunk) {
    final List<BeforeAfter<List<String>>> steps = splitHunkWithExtendedContext.getPatchSteps();
    final BetterPoint betterPoint = new BetterPoint();
    if (splitHunkWithExtendedContext.isInsertion())
        return false;
    // if it is not just insertion, then in first step will be both parts
    final Iterator<FirstLineDescriptor> iterator = mismatchSolver.getStartLineVariationsIterator();
    while (iterator.hasNext() && (betterPoint.getPoint() == null || !betterPoint.getPoint().idealFound())) {
        final FirstLineDescriptor descriptor = iterator.next();
        final Iterator<Integer> matchingIterator = getMatchingIterator(descriptor.getLine(), splitHunkWithExtendedContext.getStartLineBefore() + descriptor.getOffset(), maxWalkFromBinding);
        while (matchingIterator.hasNext() && (betterPoint.getPoint() == null || !betterPoint.getPoint().idealFound())) {
            final Integer lineNumber = matchingIterator.next();
            // go back and forward from point
            final List<BeforeAfter<List<String>>> patchSteps = splitHunkWithExtendedContext.getPatchSteps();
            final BeforeAfter<List<String>> step = patchSteps.get(descriptor.getStepNumber());
            final FragmentResult fragmentResult = checkFragmented(lineNumber, descriptor.getOffsetInStep(), step, descriptor.isIsInBefore());
            // we go back - if middle fragment ok
            if (descriptor.getStepNumber() > 0 && fragmentResult.isStartAtEdge()) {
                // not including step number here
                final List<BeforeAfter<List<String>>> list = Collections.unmodifiableList(patchSteps.subList(0, descriptor.getStepNumber()));
                int offsetForStart = -descriptor.getOffsetInStep() - 1;
                final SequentialStepsChecker backChecker = new SequentialStepsChecker(lineNumber + offsetForStart, false);
                backChecker.go(list);
                fragmentResult.setContainAlreadyApplied(fragmentResult.isContainAlreadyApplied() || backChecker.isUsesAlreadyApplied());
                fragmentResult.setStart(fragmentResult.getStart() - backChecker.getSizeOfFragmentToBeReplaced());
                fragmentResult.addDistance(backChecker.getDistance());
                fragmentResult.setStartAtEdge(backChecker.getDistance() == 0);
            }
            if (steps.size() > descriptor.getStepNumber() + 1 && fragmentResult.isEndAtEdge()) {
                // forward
                final List<BeforeAfter<List<String>>> list = Collections.unmodifiableList(patchSteps.subList(descriptor.getStepNumber() + 1, patchSteps.size()));
                if (!list.isEmpty()) {
                    final SequentialStepsChecker checker = new SequentialStepsChecker(fragmentResult.getEnd() + 1, true);
                    checker.go(list);
                    fragmentResult.setContainAlreadyApplied(fragmentResult.isContainAlreadyApplied() || checker.isUsesAlreadyApplied());
                    fragmentResult.setEnd(fragmentResult.getEnd() + checker.getSizeOfFragmentToBeReplaced());
                    fragmentResult.addDistance(checker.getDistance());
                    fragmentResult.setEndAtEdge(checker.getDistance() == 0);
                }
            }
            final TextRange textRangeInOldDocument = new UnfairTextRange(fragmentResult.getStart(), fragmentResult.getEnd());
            //if (pointCanBeUsed(textRangeInOldDocument) && (! mismatchSolver.isAllowMismatch() || fragmentResult.getEnd() - fragmentResult.getStart() > 1)) {
            if (pointCanBeUsed(textRangeInOldDocument)) {
                final int distance = fragmentResult.myDistance;
                final int commonPart = fragmentResult.getEnd() - fragmentResult.getStart() + 1;
                int contextDistance = 0;
                if (distance == 0 || commonPart < 2) {
                    final int distanceBack = getDistanceBack(fragmentResult.getStart() - 1, splitHunkWithExtendedContext.getContextBefore());
                    final int distanceInContextAfter = getDistance(fragmentResult.getEnd() + 1, splitHunkWithExtendedContext.getContextAfter());
                    contextDistance = distanceBack + distanceInContextAfter;
                }
                betterPoint.feed(new Point(distance, textRangeInOldDocument, fragmentResult.isContainAlreadyApplied(), contextDistance, commonPart));
            }
        }
    }
    final Point pointPoint = betterPoint.getPoint();
    if (pointPoint == null)
        return false;
    if (!mismatchSolver.isAllowMismatch()) {
        if (pointPoint.getDistance() > 0)
            return false;
        if (pointPoint.myCommon < 2) {
            final int contextCommon = splitHunkWithExtendedContext.getContextBefore().size() + splitHunkWithExtendedContext.getContextAfter().size() - pointPoint.myContextDistance;
            if (contextCommon == 0)
                return false;
        }
    }
    putCutIntoTransformations(pointPoint.getInOldDocument(), originalSplitHunk, new MyAppliedData(splitHunkWithExtendedContext.getAfterAll(), pointPoint.myUsesAlreadyApplied, false, pointPoint.getDistance() == 0, ChangeType.REPLACE), originalSplitHunk == null ? EMPTY_OFFSET : new IntPair(originalSplitHunk.getContextBefore().size() - splitHunkWithExtendedContext.getContextBefore().size(), originalSplitHunk.getContextAfter().size() - splitHunkWithExtendedContext.getContextAfter().size()));
    return true;
}
Also used : BeforeAfter(com.intellij.util.BeforeAfter) TextRange(com.intellij.openapi.util.TextRange) UnfairTextRange(com.intellij.openapi.util.UnfairTextRange) IntPair(com.intellij.diff.util.IntPair) UnfairTextRange(com.intellij.openapi.util.UnfairTextRange)

Example 8 with UnfairTextRange

use of com.intellij.openapi.util.UnfairTextRange in project intellij-community by JetBrains.

the class GenericPatchApplier method putCutIntoTransformations.

public void putCutIntoTransformations(TextRange range, @Nullable SplitHunk splitHunk, final MyAppliedData value, @NotNull IntPair contextOffsetInPatchSteps) {
    // cut last lines but not the very first
    final List<String> list = value.getList();
    //last line should be taken from includeConsumer even it seems to be equal with base context line( they can differ with line separator)
    boolean eofHunkAndLastLineShouldBeChanged = containsLastLine(range) && splitHunk != null && splitHunk.getContextAfter().isEmpty() && !splitHunk.getAfterAll().isEmpty();
    int cnt = list.size() - 1;
    int i = range.getEndOffset();
    if (!eofHunkAndLastLineShouldBeChanged) {
        for (; i > range.getStartOffset() && cnt >= 0; i--, cnt--) {
            if (!list.get(cnt).equals(myLines.get(i))) {
                break;
            }
        }
    }
    int endSize = list.size();
    if (cnt + 1 <= list.size() - 1) {
        endSize = cnt + 1;
    }
    int cntStart = 0;
    int j = range.getStartOffset();
    if (endSize > 0) {
        int lastProcessedIndex = eofHunkAndLastLineShouldBeChanged ? list.size() - 1 : list.size();
        for (; j < range.getEndOffset() && cntStart < lastProcessedIndex; j++, cntStart++) {
            if (!list.get(cntStart).equals(myLines.get(j))) {
                break;
            }
        }
    }
    if (j != range.getStartOffset() || i != range.getEndOffset()) {
        if (cntStart >= endSize) {
            // +1 since we set end index inclusively
            if (list.size() == (range.getLength() + 1)) {
                // for already applied
                myHadAlreadyAppliedMet = value.isHaveAlreadyApplied();
                processAppliedInfo(splitHunk, range, contextOffsetInPatchSteps, AppliedTextPatch.HunkStatus.ALREADY_APPLIED);
            } else {
                // deletion
                final UnfairTextRange textRange = new UnfairTextRange(j, i + (cntStart - endSize));
                myTransformations.put(textRange, new MyAppliedData(Collections.emptyList(), value.isHaveAlreadyApplied(), value.isPlaceCoinside(), value.isChangedCoinside(), value.myChangeType));
                processAppliedInfo(splitHunk, range, contextOffsetInPatchSteps, AppliedTextPatch.HunkStatus.EXACTLY_APPLIED);
            }
        } else {
            if (i < j) {
                // just took one line
                assert cntStart > 0;
                final MyAppliedData newData = new MyAppliedData(new ArrayList<>(list.subList(cntStart - (j - i), endSize)), value.isHaveAlreadyApplied(), value.isPlaceCoinside(), value.isChangedCoinside(), value.myChangeType);
                final TextRange newRange = new TextRange(i, i);
                myTransformations.put(newRange, newData);
                processAppliedInfo(splitHunk, range, contextOffsetInPatchSteps, AppliedTextPatch.HunkStatus.EXACTLY_APPLIED);
                return;
            }
            final MyAppliedData newData = new MyAppliedData(new ArrayList<>(list.subList(cntStart, endSize)), value.isHaveAlreadyApplied(), value.isPlaceCoinside(), value.isChangedCoinside(), value.myChangeType);
            final TextRange newRange = new TextRange(j, i);
            myTransformations.put(newRange, newData);
            processAppliedInfo(splitHunk, range, contextOffsetInPatchSteps, AppliedTextPatch.HunkStatus.EXACTLY_APPLIED);
        }
    } else {
        myTransformations.put(range, value);
        processAppliedInfo(splitHunk, range, contextOffsetInPatchSteps, AppliedTextPatch.HunkStatus.EXACTLY_APPLIED);
    }
}
Also used : UnfairTextRange(com.intellij.openapi.util.UnfairTextRange) TextRange(com.intellij.openapi.util.TextRange) UnfairTextRange(com.intellij.openapi.util.UnfairTextRange)

Example 9 with UnfairTextRange

use of com.intellij.openapi.util.UnfairTextRange in project intellij-community by JetBrains.

the class PsiSelectionSearcher method searchElementsInSelection.

/**
   * 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<>();
    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;
}
Also used : UnfairTextRange(com.intellij.openapi.util.UnfairTextRange) ArrayList(java.util.ArrayList) SelectionModel(com.intellij.openapi.editor.SelectionModel) UnfairTextRange(com.intellij.openapi.util.UnfairTextRange) TextRange(com.intellij.openapi.util.TextRange) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with UnfairTextRange

use of com.intellij.openapi.util.UnfairTextRange in project intellij-community by JetBrains.

the class HtmlSelectioner method addTagSelection2.

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);
    }
}
Also used : UnfairTextRange(com.intellij.openapi.util.UnfairTextRange) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) UnfairTextRange(com.intellij.openapi.util.UnfairTextRange)

Aggregations

UnfairTextRange (com.intellij.openapi.util.UnfairTextRange)13 TextRange (com.intellij.openapi.util.TextRange)12 PsiElement (com.intellij.psi.PsiElement)4 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)3 Nullable (org.jetbrains.annotations.Nullable)3 ASTNode (com.intellij.lang.ASTNode)2 NotNull (org.jetbrains.annotations.NotNull)2 IntPair (com.intellij.diff.util.IntPair)1 Language (com.intellij.lang.Language)1 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)1 Document (com.intellij.openapi.editor.Document)1 SelectionModel (com.intellij.openapi.editor.SelectionModel)1 ManualRangeMarker (com.intellij.openapi.editor.impl.ManualRangeMarker)1 PsiComment (com.intellij.psi.PsiComment)1 PsiLanguageInjectionHost (com.intellij.psi.PsiLanguageInjectionHost)1 PsiNamedElement (com.intellij.psi.PsiNamedElement)1 PsiReference (com.intellij.psi.PsiReference)1 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)1 BeforeAfter (com.intellij.util.BeforeAfter)1 ArrayList (java.util.ArrayList)1