mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[patch]: fix problems with last line when applying patch; tests added
This commit is contained in:
+22
-18
@@ -511,11 +511,16 @@ public class GenericPatchApplier {
|
||||
@NotNull IntPair contextOffsetInPatchSteps) {
|
||||
// cut last lines but not the very first
|
||||
final List<String> list = value.getList();
|
||||
//last line should be taken from includeConsumer even it seems to be equal with base context line( they can differ with line separator)
|
||||
boolean eofHunkAndLastLineShouldBeChanged =
|
||||
containsLastLine(range) && splitHunk != null && splitHunk.getContextAfter().isEmpty() && !splitHunk.getAfterAll().isEmpty();
|
||||
int cnt = list.size() - 1;
|
||||
int i = range.getEndOffset();
|
||||
for (; i > range.getStartOffset() && cnt >= 0; i--, cnt--) {
|
||||
if (! list.get(cnt).equals(myLines.get(i))) {
|
||||
break;
|
||||
if (!eofHunkAndLastLineShouldBeChanged) {
|
||||
for (; i > range.getStartOffset() && cnt >= 0; i--, cnt--) {
|
||||
if (!list.get(cnt).equals(myLines.get(i))) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
int endSize = list.size();
|
||||
@@ -526,8 +531,9 @@ public class GenericPatchApplier {
|
||||
int j = range.getStartOffset();
|
||||
|
||||
if (endSize > 0) {
|
||||
for (; j < range.getEndOffset() && cntStart < list.size(); j++, cntStart++) {
|
||||
if (! list.get(cntStart).equals(myLines.get(j))) {
|
||||
int lastProcessedIndex = eofHunkAndLastLineShouldBeChanged ? list.size() - 1 : list.size();
|
||||
for (; j < range.getEndOffset() && cntStart < lastProcessedIndex; j++, cntStart++) {
|
||||
if (!list.get(cntStart).equals(myLines.get(j))) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1041,19 +1047,17 @@ public class GenericPatchApplier {
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
// put not bind into the beginning
|
||||
for (SplitHunk hunk : myNotBound) {
|
||||
linesToSb(sb, hunk.getAfterAll());
|
||||
linesToSb(sb, hunk.getAfterAll(), true);
|
||||
}
|
||||
iterateTransformations(range -> {
|
||||
linesToSb(sb, myLines.subList(range.getStartOffset(), range.getEndOffset() + 1));
|
||||
if (containsLastLine(range) && myBaseFileEndsWithNewLine) {
|
||||
sb.append('\n');
|
||||
}
|
||||
List<String> baseLineslist = myLines.subList(range.getStartOffset(), range.getEndOffset() + 1);
|
||||
boolean withLineBreak = !containsLastLine(range) || myBaseFileEndsWithNewLine;
|
||||
linesToSb(sb, baseLineslist, withLineBreak);
|
||||
}, range -> {
|
||||
final MyAppliedData appliedData = myTransformations.get(range);
|
||||
linesToSb(sb, appliedData.getList());
|
||||
if (containsLastLine(range) && !mySuppressNewLineInEnd) {
|
||||
sb.append('\n');
|
||||
}
|
||||
List<String> list = appliedData.getList();
|
||||
boolean withLineBreak = !containsLastLine(range) || !mySuppressNewLineInEnd;
|
||||
linesToSb(sb, list, withLineBreak);
|
||||
});
|
||||
return sb.toString();
|
||||
}
|
||||
@@ -1062,11 +1066,11 @@ public class GenericPatchApplier {
|
||||
return range.getEndOffset() == myLines.size() - 1;
|
||||
}
|
||||
|
||||
private static void linesToSb(final StringBuilder sb, final List<String> list) {
|
||||
if (sb.length() > 0 && !list.isEmpty()) {
|
||||
sb.append("\n");
|
||||
}
|
||||
private static void linesToSb(final StringBuilder sb, final List<String> list, boolean withEndLineBreak) {
|
||||
StringUtil.join(list, "\n", sb);
|
||||
if (!list.isEmpty() && withEndLineBreak) {
|
||||
sb.append('\n');
|
||||
}
|
||||
}
|
||||
|
||||
// indexes are passed inclusive
|
||||
|
||||
Reference in New Issue
Block a user