Search in sources :

Example 6 with IntPair

use of com.intellij.diff.util.IntPair in project intellij-community by JetBrains.

the class GenericPatchApplier method testForExactMatch.

private boolean testForExactMatch(final SplitHunk splitHunk, final SplitHunk originalHunk) {
    final int offset = splitHunk.getContextBefore().size();
    final List<BeforeAfter<List<String>>> steps = splitHunk.getPatchSteps();
    if (splitHunk.isInsertion()) {
        final boolean emptyFile = myLines.isEmpty() || myLines.size() == 1 && myLines.get(0).trim().length() == 0;
        if (emptyFile) {
            myNotBound.add(splitHunk);
        }
        return emptyFile;
    }
    int idx = splitHunk.getStartLineBefore() + offset;
    int cnt = 0;
    boolean hadAlreadyApplied = false;
    for (BeforeAfter<List<String>> step : steps) {
        if (myLines.size() <= idx)
            return false;
        // can occur only in the end
        if (step.getBefore().isEmpty())
            continue;
        final Pair<Integer, Boolean> distance = new FragmentMatcher(idx + cnt, step).find(false);
        if (distance.getFirst() > 0) {
            return false;
        }
        // fits!
        int length;
        if (distance.getSecond()) {
            length = step.getBefore().size();
        } else {
            length = step.getAfter().size();
            hadAlreadyApplied = true;
        }
        cnt += length;
    //idx += length - 1;
    }
    putCutIntoTransformations(new TextRange(idx, idx + cnt - 1), originalHunk, new MyAppliedData(splitHunk.getAfterAll(), hadAlreadyApplied, true, true, ChangeType.REPLACE), new IntPair(originalHunk.getContextBefore().size() - splitHunk.getContextBefore().size(), originalHunk.getContextAfter().size() - splitHunk.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)

Example 7 with IntPair

use of com.intellij.diff.util.IntPair 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)

Aggregations

IntPair (com.intellij.diff.util.IntPair)7 TextRange (com.intellij.openapi.util.TextRange)4 NotNull (org.jetbrains.annotations.NotNull)3 MergeRange (com.intellij.diff.util.MergeRange)2 Range (com.intellij.diff.util.Range)2 UnfairTextRange (com.intellij.openapi.util.UnfairTextRange)2 BeforeAfter (com.intellij.util.BeforeAfter)2 ArrayList (java.util.ArrayList)2 DiffIterable (com.intellij.diff.comparison.iterables.DiffIterable)1 DiffIterableUtil (com.intellij.diff.comparison.iterables.DiffIterableUtil)1 DiffIterableUtil.fair (com.intellij.diff.comparison.iterables.DiffIterableUtil.fair)1 FairDiffIterable (com.intellij.diff.comparison.iterables.FairDiffIterable)1 DiffContent (com.intellij.diff.contents.DiffContent)1 com.intellij.diff.fragments (com.intellij.diff.fragments)1 LoadingDiffRequest (com.intellij.diff.requests.LoadingDiffRequest)1 MessageDiffRequest (com.intellij.diff.requests.MessageDiffRequest)1 SimpleDiffRequest (com.intellij.diff.requests.SimpleDiffRequest)1 DiffUtil (com.intellij.diff.util.DiffUtil)1 Logger (com.intellij.openapi.diagnostic.Logger)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1