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);
}
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;
}
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);
}
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();
}
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;
}
Aggregations