Search in sources :

Example 16 with Range

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

the class ComparisonManagerImpl method trimIgnoredChanges.

@Nullable
private static LineFragment trimIgnoredChanges(@NotNull LineFragment fragment, @NotNull List<Line> lines1, @NotNull List<Line> lines2, @NotNull BitSet ignored1, @NotNull BitSet ignored2) {
    // trim ignored lines
    Range range = TrimUtil.trimExpandList(lines1, lines2, fragment.getStartLine1(), fragment.getStartLine2(), fragment.getEndLine1(), fragment.getEndLine2(), (line1, line2) -> areIgnoredEqualLines(line1, line2, ignored1, ignored2), line -> isIgnoredLine(line, ignored1), line -> isIgnoredLine(line, ignored2));
    int startLine1 = range.start1;
    int startLine2 = range.start2;
    int endLine1 = range.end1;
    int endLine2 = range.end2;
    if (startLine1 == endLine1 && startLine2 == endLine2)
        return null;
    IntPair offsets1 = getOffsets(lines1, startLine1, endLine1);
    IntPair offsets2 = getOffsets(lines2, startLine2, endLine2);
    int startOffset1 = offsets1.val1;
    int endOffset1 = offsets1.val2;
    int startOffset2 = offsets2.val1;
    int endOffset2 = offsets2.val2;
    List<DiffFragment> newInner = null;
    if (fragment.getInnerFragments() != null) {
        int shift1 = startOffset1 - fragment.getStartOffset1();
        int shift2 = startOffset2 - fragment.getStartOffset2();
        int newCount1 = endOffset1 - startOffset1;
        int newCount2 = endOffset2 - startOffset2;
        newInner = ContainerUtil.mapNotNull(fragment.getInnerFragments(), it -> {
            int start1 = DiffUtil.bound(it.getStartOffset1() - shift1, 0, newCount1);
            int start2 = DiffUtil.bound(it.getStartOffset2() - shift2, 0, newCount2);
            int end1 = DiffUtil.bound(it.getEndOffset1() - shift1, 0, newCount1);
            int end2 = DiffUtil.bound(it.getEndOffset2() - shift2, 0, newCount2);
            TextRange range1 = trimIgnoredRange(start1, end1, ignored1, startOffset1);
            TextRange range2 = trimIgnoredRange(start2, end2, ignored2, startOffset2);
            if (range1.isEmpty() && range2.isEmpty())
                return null;
            return new DiffFragmentImpl(range1.getStartOffset(), range1.getEndOffset(), range2.getStartOffset(), range2.getEndOffset());
        });
        if (newInner.isEmpty())
            return null;
    }
    return new LineFragmentImpl(startLine1, endLine1, startLine2, endLine2, startOffset1, endOffset1, startOffset2, endOffset2, newInner);
}
Also used : com.intellij.diff.fragments(com.intellij.diff.fragments) StringUtil(com.intellij.openapi.util.text.StringUtil) TextRange(com.intellij.openapi.util.TextRange) MergeRange(com.intellij.diff.util.MergeRange) ContainerUtil(com.intellij.util.containers.ContainerUtil) CharSequenceSubSequence(com.intellij.util.text.CharSequenceSubSequence) FairDiffIterable(com.intellij.diff.comparison.iterables.FairDiffIterable) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) Range(com.intellij.diff.util.Range) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) List(java.util.List) FilesTooBigForDiffException(com.intellij.util.diff.FilesTooBigForDiffException) DiffUtil(com.intellij.diff.util.DiffUtil) IntPair(com.intellij.diff.util.IntPair) BitSet(java.util.BitSet) Logger(com.intellij.openapi.diagnostic.Logger) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) DiffIterableUtil.fair(com.intellij.diff.comparison.iterables.DiffIterableUtil.fair) DiffIterable(com.intellij.diff.comparison.iterables.DiffIterable) Consumer(com.intellij.util.Consumer) DiffIterableUtil(com.intellij.diff.comparison.iterables.DiffIterableUtil) TextRange(com.intellij.openapi.util.TextRange) TextRange(com.intellij.openapi.util.TextRange) MergeRange(com.intellij.diff.util.MergeRange) Range(com.intellij.diff.util.Range) IntPair(com.intellij.diff.util.IntPair) Nullable(org.jetbrains.annotations.Nullable)

Example 17 with Range

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

the class ComparisonManagerImpl method convertIntoLineFragments.

@NotNull
public static List<LineFragment> convertIntoLineFragments(@NotNull List<Line> lines1, @NotNull List<Line> lines2, @NotNull FairDiffIterable changes) {
    List<LineFragment> fragments = new ArrayList<>();
    for (Range ch : changes.iterateChanges()) {
        IntPair offsets1 = getOffsets(lines1, ch.start1, ch.end1);
        IntPair offsets2 = getOffsets(lines2, ch.start2, ch.end2);
        fragments.add(new LineFragmentImpl(ch.start1, ch.end1, ch.start2, ch.end2, offsets1.val1, offsets1.val2, offsets2.val1, offsets2.val2));
    }
    return fragments;
}
Also used : ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) MergeRange(com.intellij.diff.util.MergeRange) Range(com.intellij.diff.util.Range) IntPair(com.intellij.diff.util.IntPair) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with Range

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

the class LineFragmentSplitter method run.

// indexes here are a bit tricky
// -1 - the beginning of file, words.size() - end of file, everything in between - InlineChunks (words or newlines)
@NotNull
public List<WordBlock> run() {
    boolean hasEqualWords = false;
    for (Range range : myIterable.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 (isNewline(myWords1, index1) && isNewline(myWords2, index2)) {
                // split by matched newlines
                addLineChunk(index1, index2, hasEqualWords);
                hasEqualWords = false;
            } else {
                if (isFirstInLine(myWords1, index1) && isFirstInLine(myWords2, index2)) {
                    // split by matched first word in line
                    addLineChunk(index1 - 1, index2 - 1, hasEqualWords);
                    hasEqualWords = false;
                }
                // TODO: split by 'last word in line' + 'last word in whole sequence' ?
                hasEqualWords = true;
            }
        }
    }
    addLineChunk(myWords1.size(), myWords2.size(), hasEqualWords);
    if (pendingChunk != null)
        myResult.add(pendingChunk.block);
    return myResult;
}
Also used : Range(com.intellij.diff.util.Range) NotNull(org.jetbrains.annotations.NotNull)

Example 19 with Range

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

the class LineFragmentSplitter method createChunk.

@NotNull
private PendingChunk createChunk(int start1, int start2, int end1, int end2, boolean hasEqualWords) {
    int startOffset1 = getOffset(myWords1, myText1, start1);
    int startOffset2 = getOffset(myWords2, myText2, start2);
    int endOffset1 = getOffset(myWords1, myText1, end1);
    int endOffset2 = getOffset(myWords2, myText2, end2);
    start1 = Math.max(0, start1 + 1);
    start2 = Math.max(0, start2 + 1);
    end1 = Math.min(end1 + 1, myWords1.size());
    end2 = Math.min(end2 + 1, myWords2.size());
    WordBlock block = new WordBlock(new Range(start1, end1, start2, end2), new Range(startOffset1, endOffset1, startOffset2, endOffset2));
    return new PendingChunk(block, hasEqualWords, hasWordsInside(block), isEqualsIgnoreWhitespace(block));
}
Also used : Range(com.intellij.diff.util.Range) NotNull(org.jetbrains.annotations.NotNull)

Example 20 with Range

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

the class ChangeDiffIterableBase method changes.

@NotNull
@Override
public Iterator<Range> changes() {
    return new Iterator<Range>() {

        @NotNull
        private final ChangeIterable myIterable = createChangeIterable();

        @Override
        public boolean hasNext() {
            return myIterable.valid();
        }

        @Override
        public Range next() {
            Range range = new Range(myIterable.getStart1(), myIterable.getEnd1(), myIterable.getStart2(), myIterable.getEnd2());
            myIterable.next();
            return range;
        }
    };
}
Also used : Iterator(java.util.Iterator) Range(com.intellij.diff.util.Range) 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