Search in sources :

Example 1 with DiffIterable

use of com.intellij.diff.comparison.iterables.DiffIterable in project intellij-community by JetBrains.

the class ByWord method compareAndSplit.

@NotNull
public static List<LineBlock> compareAndSplit(@NotNull CharSequence text1, @NotNull CharSequence text2, @NotNull ComparisonPolicy policy, @NotNull ProgressIndicator indicator) {
    indicator.checkCanceled();
    // TODO: figure out, what do we exactly want from 'Split' logic
    // -- it is used for trimming of ignored blocks. So we want whitespace-only leading/trailing lines to be separate block.
    // -- old approach: split by matched '\n's
    // TODO: other approach could lead to better results:
    // * Compare words-only
    // * prefer big chunks
    // -- here we can try to minimize number of matched pairs 'pair[i]' and 'pair[i+1]' such that
    //    containsNewline(pair[i].left .. pair[i+1].left) XOR containsNewline(pair[i].right .. pair[i+1].right) == true
    //    ex: "A X C" - "A Y C \n M C" - do not match with last 'C'
    //    ex: "A \n" - "A B \n \n" - do not match with last '\n'
    //    Try some greedy approach ?
    // * split into blocks
    // -- squash blocks with too small unchanged words count (1 matched word out of 40 - is a bad reason to create new block)
    // * match adjustment punctuation
    // * match adjustment whitespaces ('\n' are matched here)
    List<InlineChunk> words1 = getInlineChunks(text1);
    List<InlineChunk> words2 = getInlineChunks(text2);
    FairDiffIterable wordChanges = diff(words1, words2, indicator);
    wordChanges = optimizeWordChunks(text1, text2, words1, words2, wordChanges, indicator);
    List<WordBlock> wordBlocks = new LineFragmentSplitter(text1, text2, words1, words2, wordChanges, indicator).run();
    List<LineBlock> lineBlocks = new ArrayList<>(wordBlocks.size());
    for (WordBlock block : wordBlocks) {
        Range offsets = block.offsets;
        Range words = block.words;
        CharSequence subtext1 = text1.subSequence(offsets.start1, offsets.end1);
        CharSequence subtext2 = text2.subSequence(offsets.start2, offsets.end2);
        List<InlineChunk> subwords1 = words1.subList(words.start1, words.end1);
        List<InlineChunk> subwords2 = words2.subList(words.start2, words.end2);
        FairDiffIterable subiterable = fair(subiterable(wordChanges, words.start1, words.end1, words.start2, words.end2));
        FairDiffIterable delimitersIterable = matchAdjustmentDelimiters(subtext1, subtext2, subwords1, subwords2, subiterable, offsets.start1, offsets.start2, indicator);
        DiffIterable iterable = matchAdjustmentWhitespaces(subtext1, subtext2, delimitersIterable, policy, indicator);
        List<DiffFragment> fragments = convertIntoDiffFragments(iterable);
        int newlines1 = countNewlines(subwords1);
        int newlines2 = countNewlines(subwords2);
        lineBlocks.add(new LineBlock(fragments, offsets, newlines1, newlines2));
    }
    return lineBlocks;
}
Also used : WordBlock(com.intellij.diff.comparison.LineFragmentSplitter.WordBlock) ArrayList(java.util.ArrayList) MergingCharSequence(com.intellij.util.text.MergingCharSequence) FairDiffIterable(com.intellij.diff.comparison.iterables.FairDiffIterable) MergeRange(com.intellij.diff.util.MergeRange) Range(com.intellij.diff.util.Range) FairDiffIterable(com.intellij.diff.comparison.iterables.FairDiffIterable) DiffIterable(com.intellij.diff.comparison.iterables.DiffIterable) DiffFragment(com.intellij.diff.fragments.DiffFragment) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with DiffIterable

use of com.intellij.diff.comparison.iterables.DiffIterable in project intellij-community by JetBrains.

the class ByWord method compare.

@NotNull
public static List<DiffFragment> compare(@NotNull CharSequence text1, @NotNull List<InlineChunk> words1, @NotNull CharSequence text2, @NotNull List<InlineChunk> words2, @NotNull ComparisonPolicy policy, @NotNull ProgressIndicator indicator) {
    FairDiffIterable wordChanges = diff(words1, words2, indicator);
    wordChanges = optimizeWordChunks(text1, text2, words1, words2, wordChanges, indicator);
    FairDiffIterable delimitersIterable = matchAdjustmentDelimiters(text1, text2, words1, words2, wordChanges, indicator);
    DiffIterable iterable = matchAdjustmentWhitespaces(text1, text2, delimitersIterable, policy, indicator);
    return convertIntoDiffFragments(iterable);
}
Also used : FairDiffIterable(com.intellij.diff.comparison.iterables.FairDiffIterable) FairDiffIterable(com.intellij.diff.comparison.iterables.FairDiffIterable) DiffIterable(com.intellij.diff.comparison.iterables.DiffIterable) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

DiffIterable (com.intellij.diff.comparison.iterables.DiffIterable)2 FairDiffIterable (com.intellij.diff.comparison.iterables.FairDiffIterable)2 NotNull (org.jetbrains.annotations.NotNull)2 WordBlock (com.intellij.diff.comparison.LineFragmentSplitter.WordBlock)1 DiffFragment (com.intellij.diff.fragments.DiffFragment)1 MergeRange (com.intellij.diff.util.MergeRange)1 Range (com.intellij.diff.util.Range)1 MergingCharSequence (com.intellij.util.text.MergingCharSequence)1 ArrayList (java.util.ArrayList)1