Search in sources :

Example 6 with DiffString

use of com.intellij.openapi.diff.impl.string.DiffString 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 7 with DiffString

use of com.intellij.openapi.diff.impl.string.DiffString 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)

Example 8 with DiffString

use of com.intellij.openapi.diff.impl.string.DiffString in project intellij-community by JetBrains.

the class Util method unite.

@NotNull
public static DiffFragment unite(@NotNull DiffFragment fragment1, @NotNull DiffFragment fragment2) {
    LOG.assertTrue(isSameType(fragment1, fragment2));
    if (!fragment1.isOneSide()) {
        DiffString unitedText1 = DiffString.concatenateNullable(fragment1.getText1(), fragment2.getText1());
        DiffString unitedText2 = DiffString.concatenateNullable(fragment1.getText2(), fragment2.getText2());
        LOG.assertTrue(fragment1.isEqual() == fragment2.isEqual());
        return fragment1.isEqual() ? DiffFragment.unchanged(unitedText1, unitedText2) : new DiffFragment(unitedText1, unitedText2);
    }
    FragmentSide side = FragmentSide.chooseSide(fragment1);
    return side.createFragment(DiffString.concatenateNullable(side.getText(fragment1), side.getText(fragment2)), null, fragment1.isModified());
}
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 DiffString

use of com.intellij.openapi.diff.impl.string.DiffString in project intellij-community by JetBrains.

the class ByWord method buildFragments.

@NotNull
@Override
public DiffFragment[] buildFragments(@NotNull DiffString text1, @NotNull DiffString text2) throws FilesTooBigForDiffException {
    Word[] words1 = buildWords(text1, myComparisonPolicy);
    Word[] words2 = buildWords(text2, myComparisonPolicy);
    Diff.Change change = Diff.buildChanges(words1, words2);
    change = Util.concatEquals(change, words1, words2);
    if (Math.max(countNotWhitespaces(words1), countNotWhitespaces(words2)) > 0 && countEqual(change, words1, words2) == 0)
        return new DiffFragment[] { myComparisonPolicy.createFragment(text1, text2) };
    FragmentBuilder result = new FragmentBuilder(words1, words2, myComparisonPolicy, text1, text2);
    FragmentBuilder.Version version1 = result.getVersion1();
    FragmentBuilder.Version version2 = result.getVersion2();
    while (change != null) {
        if (change.line0 > version1.getCurrentWordIndex()) {
            processEquals(change.line0, change.line1, result);
        }
        if (change.inserted == 0) {
            processOneside(version1, change.deleted);
        } else if (change.deleted == 0) {
            processOneside(version2, change.inserted);
        } else {
            DiffString prefix1 = version1.getCurrentWordPrefix();
            DiffString prefix2 = version2.getCurrentWordPrefix();
            if (!prefix1.isEmpty() || !prefix2.isEmpty())
                result.add(myComparisonPolicy.createFragment(prefix1, prefix2));
            result.addChangedWords(change.deleted, change.inserted);
        }
        change = change.link;
    }
    processEquals(words1.length, words2.length, result);
    result.addTails();
    DiffFragment[] fragments = result.getFragments();
    DiffFragment firstFragment = fragments[0];
    if (firstFragment.isEmpty()) {
        DiffFragment[] newFragments = new DiffFragment[fragments.length - 1];
        System.arraycopy(fragments, 1, newFragments, 0, newFragments.length);
        fragments = newFragments;
    }
    return fragments;
}
Also used : DiffFragment(com.intellij.openapi.diff.ex.DiffFragment) Diff(com.intellij.util.diff.Diff) DiffString(com.intellij.openapi.diff.impl.string.DiffString) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with DiffString

use of com.intellij.openapi.diff.impl.string.DiffString in project intellij-community by JetBrains.

the class DiffFragment method appendText2.

public void appendText2(@Nullable DiffString str) {
    if (str == null)
        return;
    if (myText2 instanceof DiffStringBuilder) {
        ((DiffStringBuilder) myText2).append(str);
        return;
    }
    if (myText2 instanceof DiffString) {
        DiffString text2 = (DiffString) myText2;
        if (DiffString.canInplaceConcatenate(text2, str)) {
            myText2 = DiffString.concatenate(text2, str);
        } else {
            DiffStringBuilder builder = new DiffStringBuilder(text2.length() + str.length());
            builder.append(text2);
            builder.append(str);
            myText2 = builder;
        }
        return;
    }
    throw new IllegalStateException("Bad DiffFragment: " + (myText2 != null ? myText2.getClass() : "null"));
}
Also used : DiffString(com.intellij.openapi.diff.impl.string.DiffString) DiffStringBuilder(com.intellij.openapi.diff.impl.string.DiffStringBuilder)

Aggregations

DiffString (com.intellij.openapi.diff.impl.string.DiffString)20 DiffFragment (com.intellij.openapi.diff.ex.DiffFragment)15 NotNull (org.jetbrains.annotations.NotNull)10 FragmentSide (com.intellij.openapi.diff.impl.highlighting.FragmentSide)2 DiffStringBuilder (com.intellij.openapi.diff.impl.string.DiffStringBuilder)2 Fragment (com.intellij.openapi.diff.impl.fragments.Fragment)1 LineFragment (com.intellij.openapi.diff.impl.fragments.LineFragment)1 Document (com.intellij.openapi.editor.Document)1 TextRange (com.intellij.openapi.util.TextRange)1 Diff (com.intellij.util.diff.Diff)1 FilesTooBigForDiffException (com.intellij.util.diff.FilesTooBigForDiffException)1 ArrayList (java.util.ArrayList)1