Search in sources :

Example 1 with Range

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

the class DiffIterableUtil method verifyFullCover.

private static void verifyFullCover(@NotNull DiffIterable iterable) {
    int last1 = 0;
    int last2 = 0;
    Boolean lastEquals = null;
    for (Pair<Range, Boolean> pair : iterateAll(iterable)) {
        Range range = pair.first;
        Boolean equal = pair.second;
        assert last1 == range.start1;
        assert last2 == range.start2;
        assert !Comparing.equal(lastEquals, equal);
        last1 = range.end1;
        last2 = range.end2;
        lastEquals = equal;
    }
    assert last1 == iterable.getLength1();
    assert last2 == iterable.getLength2();
}
Also used : Range(com.intellij.diff.util.Range)

Example 2 with Range

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

the class DiffIterableUtil method iterateAll.

//
// Misc
//
@NotNull
public static Iterable<Pair<Range, Boolean>> iterateAll(@NotNull final DiffIterable iterable) {
    return () -> new Iterator<Pair<Range, Boolean>>() {

        @NotNull
        private final Iterator<Range> myChanges = iterable.changes();

        @NotNull
        private final Iterator<Range> myUnchanged = iterable.unchanged();

        @Nullable
        private Range lastChanged = myChanges.hasNext() ? myChanges.next() : null;

        @Nullable
        private Range lastUnchanged = myUnchanged.hasNext() ? myUnchanged.next() : null;

        @Override
        public boolean hasNext() {
            return lastChanged != null || lastUnchanged != null;
        }

        @Override
        public Pair<Range, Boolean> next() {
            boolean equals;
            if (lastChanged == null) {
                equals = true;
            } else if (lastUnchanged == null) {
                equals = false;
            } else {
                equals = lastUnchanged.start1 < lastChanged.start1 || lastUnchanged.start2 < lastChanged.start2;
            }
            if (equals) {
                Range range = lastUnchanged;
                lastUnchanged = myUnchanged.hasNext() ? myUnchanged.next() : null;
                //noinspection ConstantConditions
                return Pair.create(range, true);
            } else {
                Range range = lastChanged;
                lastChanged = myChanges.hasNext() ? myChanges.next() : null;
                return Pair.create(range, false);
            }
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : Iterator(java.util.Iterator) Range(com.intellij.diff.util.Range) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with Range

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

the class ChunkOptimizer method build.

@NotNull
public FairDiffIterable build() {
    for (Range range : myIterable.iterateUnchanged()) {
        myRanges.add(range);
        processLastRanges();
    }
    return fair(createUnchanged(myRanges, myData1.size(), myData2.size()));
}
Also used : Range(com.intellij.diff.util.Range) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with Range

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

the class ChunkOptimizer method processLastRanges.

private void processLastRanges() {
    // nothing to do
    if (myRanges.size() < 2)
        return;
    Range range1 = myRanges.get(myRanges.size() - 2);
    Range range2 = myRanges.get(myRanges.size() - 1);
    if (range1.end1 != range2.start1 && range1.end2 != range2.start2) {
        // it means that given DiffIterable is not LCS (because we can build a smaller one). This should not happen.
        return;
    }
    int count1 = range1.end1 - range1.start1;
    int count2 = range2.end1 - range2.start1;
    int equalForward = expandForward(myData1, myData2, range1.end1, range1.end2, range1.end1 + count2, range1.end2 + count2);
    int equalBackward = expandBackward(myData1, myData2, range2.start1 - count1, range2.start2 - count1, range2.start1, range2.start2);
    // nothing to do
    if (equalForward == 0 && equalBackward == 0)
        return;
    // merge chunks left [A]B[B] -> [AB]B
    if (equalForward == count2) {
        myRanges.remove(myRanges.size() - 1);
        myRanges.remove(myRanges.size() - 1);
        myRanges.add(new Range(range1.start1, range1.end1 + count2, range1.start2, range1.end2 + count2));
        processLastRanges();
        return;
    }
    // merge chunks right [A]A[B] -> A[AB]
    if (equalBackward == count1) {
        myRanges.remove(myRanges.size() - 1);
        myRanges.remove(myRanges.size() - 1);
        myRanges.add(new Range(range2.start1 - count1, range2.end1, range2.start2 - count1, range2.end2));
        processLastRanges();
        return;
    }
    Side touchSide = Side.fromLeft(range1.end1 == range2.start1);
    int shift = getShift(touchSide, equalForward, equalBackward, range1, range2);
    if (shift != 0) {
        myRanges.remove(myRanges.size() - 1);
        myRanges.remove(myRanges.size() - 1);
        myRanges.add(new Range(range1.start1, range1.end1 + shift, range1.start2, range1.end2 + shift));
        myRanges.add(new Range(range2.start1 + shift, range2.end1, range2.start2 + shift, range2.end2));
    }
}
Also used : Side(com.intellij.diff.util.Side) Range(com.intellij.diff.util.Range)

Example 5 with Range

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

the class ComparisonManagerImpl method correctIgnoredRangesSecondStep.

@NotNull
private static FairDiffIterable correctIgnoredRangesSecondStep(@NotNull FairDiffIterable iterable, @NotNull List<Line> lines1, @NotNull List<Line> lines2, @NotNull BitSet ignored1, @NotNull BitSet ignored2) {
    DiffIterableUtil.ChangeBuilder builder = new DiffIterableUtil.ChangeBuilder(lines1.size(), lines2.size());
    for (Range range : iterable.iterateUnchanged()) {
        int count = range.end1 - range.start1;
        for (int i = 0; i < count; i++) {
            int index1 = range.start1 + i;
            int index2 = range.start2 + i;
            if (areIgnoredEqualLines(lines1.get(index1), lines2.get(index2), ignored1, ignored2)) {
                builder.markEqual(index1, index2);
            }
        }
    }
    return fair(builder.finish());
}
Also used : TextRange(com.intellij.openapi.util.TextRange) MergeRange(com.intellij.diff.util.MergeRange) Range(com.intellij.diff.util.Range) DiffIterableUtil(com.intellij.diff.comparison.iterables.DiffIterableUtil) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Range (com.intellij.diff.util.Range)26 NotNull (org.jetbrains.annotations.NotNull)21 MergeRange (com.intellij.diff.util.MergeRange)8 ArrayList (java.util.ArrayList)7 TextRange (com.intellij.openapi.util.TextRange)5 FairDiffIterable (com.intellij.diff.comparison.iterables.FairDiffIterable)3 Iterator (java.util.Iterator)3 DiffTooBigException (com.intellij.diff.comparison.DiffTooBigException)2 DiffIterable (com.intellij.diff.comparison.iterables.DiffIterable)2 DiffIterableUtil (com.intellij.diff.comparison.iterables.DiffIterableUtil)2 DiffUtil (com.intellij.diff.util.DiffUtil)2 IntPair (com.intellij.diff.util.IntPair)2 TIntArrayList (gnu.trove.TIntArrayList)2 Nullable (org.jetbrains.annotations.Nullable)2 WordBlock (com.intellij.diff.comparison.LineFragmentSplitter.WordBlock)1 DiffIterableUtil.fair (com.intellij.diff.comparison.iterables.DiffIterableUtil.fair)1 com.intellij.diff.fragments (com.intellij.diff.fragments)1 DiffFragment (com.intellij.diff.fragments.DiffFragment)1 Side (com.intellij.diff.util.Side)1 Logger (com.intellij.openapi.diagnostic.Logger)1