Search in sources :

Example 6 with DiffFragment

use of com.intellij.openapi.diff.ex.DiffFragment in project intellij-community by JetBrains.

the class Util method getText.

@NotNull
public static DiffString getText(@NotNull DiffFragment[] fragments, @NotNull FragmentSide side) {
    DiffString[] data = new DiffString[fragments.length];
    for (int i = 0; i < fragments.length; i++) {
        DiffFragment fragment = fragments[i];
        data[i] = side.getText(fragment);
    }
    return DiffString.concatenate(data);
}
Also used : DiffFragment(com.intellij.openapi.diff.ex.DiffFragment) DiffString(com.intellij.openapi.diff.impl.string.DiffString) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with DiffFragment

use of com.intellij.openapi.diff.ex.DiffFragment in project intellij-community by JetBrains.

the class Util method transformHeadInsert.

@NotNull
private static DiffFragment[] transformHeadInsert(@NotNull DiffFragment[] fragments, @NotNull FragmentSide side) {
    // transforms {abc}abcd into a{bca}bcd
    if (fragments.length >= 2) {
        DiffFragment first = fragments[0];
        DiffFragment second = fragments[1];
        if (first == null || second == null) {
            return fragments;
        }
        if (side.getText(first) != null) {
            return fragments;
        }
        DiffString rightText = side.getOtherText(first);
        DiffString secondText = side.getText(second);
        if (!Comparing.equal(side.getOtherText(second), secondText)) {
            return fragments;
        }
        if (secondText.charAt(0) == rightText.charAt(0)) {
            List<DiffFragment> result = new ArrayList<DiffFragment>();
            result.add(side.createFragment(rightText.substring(0, 1), rightText.substring(0, 1), false));
            result.add(side.createFragment(null, DiffString.concatenate(rightText.substring(1), secondText.substring(0, 1)), true));
            result.add(side.createFragment(secondText.substring(1), secondText.substring(1), second.isModified()));
            result.addAll(Arrays.asList(fragments).subList(2, fragments.length));
            return result.toArray(new DiffFragment[result.size()]);
        }
    }
    return fragments;
}
Also used : DiffFragment(com.intellij.openapi.diff.ex.DiffFragment) DiffString(com.intellij.openapi.diff.impl.string.DiffString) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with DiffFragment

use of com.intellij.openapi.diff.ex.DiffFragment in project intellij-community by JetBrains.

the class Util method concatenate.

@NotNull
public static DiffFragment concatenate(@NotNull DiffFragment[] line, int from, int to) {
    DiffString[] data1 = new DiffString[to - from];
    DiffString[] data2 = new DiffString[to - from];
    boolean isEqual = true;
    for (int i = 0; i < to - from; i++) {
        DiffFragment fragment = line[from + i];
        isEqual &= fragment.isEqual();
        data1[i] = fragment.getText1();
        data2[i] = fragment.getText2();
    }
    DiffString text1 = notEmptyContent(DiffString.concatenate(data1));
    DiffString text2 = notEmptyContent(DiffString.concatenate(data2));
    return isEqual ? DiffFragment.unchanged(text1, text2) : new DiffFragment(text1, text2);
}
Also used : DiffFragment(com.intellij.openapi.diff.ex.DiffFragment) DiffString(com.intellij.openapi.diff.impl.string.DiffString) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with DiffFragment

use of com.intellij.openapi.diff.ex.DiffFragment in project intellij-community by JetBrains.

the class Util method splitByUnchangedLines.

@NotNull
public static DiffFragment[][] splitByUnchangedLines(@NotNull DiffFragment[] fragments) {
    List2D result = new List2D();
    for (int i = 0; i < fragments.length; i++) {
        DiffFragment fragment = fragments[i];
        if (!fragment.isEqual()) {
            result.add(fragment);
            continue;
        }
        DiffString text1 = fragment.getText1();
        DiffString text2 = fragment.getText2();
        assert text1 != null;
        assert text2 != null;
        if (StringUtil.endsWithChar(text1, '\n') && StringUtil.endsWithChar(text2, '\n')) {
            result.add(fragment);
            result.newRow();
            continue;
        }
        while (true) {
            int newLine1 = text1.indexOf('\n');
            int newLine2 = text2.indexOf('\n');
            if (newLine1 == -1 || newLine2 == -1) {
                result.add(DiffFragment.unchanged(text1, text2));
                break;
            }
            result.add(DiffFragment.unchanged(text1.substring(0, newLine1 + 1), text2.substring(0, newLine2 + 1)));
            result.newRow();
            text1 = text1.substring(newLine1 + 1);
            text2 = text2.substring(newLine2 + 1);
            int length1 = text1.length();
            int length2 = text2.length();
            if (length1 == 0 || length2 == 0) {
                if (length1 != 0 || length2 != 0)
                    result.add(DiffFragment.unchanged(text1, text2));
                break;
            }
        }
    }
    return result.toArray();
}
Also used : DiffFragment(com.intellij.openapi.diff.ex.DiffFragment) DiffString(com.intellij.openapi.diff.impl.string.DiffString) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with DiffFragment

use of com.intellij.openapi.diff.ex.DiffFragment in project intellij-community by JetBrains.

the class Util method cutFirst.

@NotNull
public static DiffFragment[] cutFirst(@NotNull DiffFragment[] fragments) {
    fragments = transformHeadInsert(fragments, FragmentSide.SIDE1);
    fragments = transformHeadInsert(fragments, FragmentSide.SIDE2);
    int nullCount = 0;
    for (int sideIndex = 0; sideIndex < 2; sideIndex++) {
        FragmentSide side = FragmentSide.fromIndex(sideIndex);
        for (int i = 0; i < fragments.length; i++) {
            DiffFragment fragment = fragments[i];
            if (fragment == null)
                continue;
            DiffString text = side.getText(fragment);
            if (text == null || text.isEmpty())
                continue;
            text = text.length() > 1 ? text.substring(1) : null;
            DiffString otherText = side.getOtherText(fragment);
            if (otherText == null && text == null) {
                fragments[i] = null;
                nullCount++;
            } else
                fragments[i] = side.createFragment(text, otherText, fragment.isModified());
            break;
        }
    }
    if (nullCount == 0)
        return fragments;
    DiffFragment[] result = new DiffFragment[fragments.length - nullCount];
    int dstIndex = 0;
    for (int i = 0; i < fragments.length; i++) {
        DiffFragment fragment = fragments[i];
        if (fragment == null)
            continue;
        result[dstIndex] = fragment;
        dstIndex++;
    }
    return result;
}
Also used : DiffFragment(com.intellij.openapi.diff.ex.DiffFragment) DiffString(com.intellij.openapi.diff.impl.string.DiffString) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

DiffFragment (com.intellij.openapi.diff.ex.DiffFragment)55 DiffString (com.intellij.openapi.diff.impl.string.DiffString)15 NotNull (org.jetbrains.annotations.NotNull)13 FragmentSide (com.intellij.openapi.diff.impl.highlighting.FragmentSide)3 LineFragment (com.intellij.openapi.diff.impl.fragments.LineFragment)2 TextRange (com.intellij.openapi.util.TextRange)2 FilesTooBigForDiffException (com.intellij.util.diff.FilesTooBigForDiffException)2 Fragment (com.intellij.openapi.diff.impl.fragments.Fragment)1 DiffCorrection (com.intellij.openapi.diff.impl.processing.DiffCorrection)1 DiffFragmentsProcessor (com.intellij.openapi.diff.impl.processing.DiffFragmentsProcessor)1 Document (com.intellij.openapi.editor.Document)1 VcsException (com.intellij.openapi.vcs.VcsException)1 Diff (com.intellij.util.diff.Diff)1 ArrayList (java.util.ArrayList)1