mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[patch]: IDEA-158750 "Apply Patch" adds spurious newline at the end of file, tests added
* existence of new line should be inherited by result from base content (before) if no hunks override it; * if some hunk overrides last line - it should be inherited from this hunk (after content);
This commit is contained in:
+15
-4
@@ -43,6 +43,7 @@ public class GenericPatchApplier {
|
||||
private final TreeMap<TextRange, MyAppliedData> myTransformations;
|
||||
private final List<String> myLines;
|
||||
private final List<PatchHunk> myHunks;
|
||||
private final boolean myBaseFileEndsWithNewLine;
|
||||
private boolean myHadAlreadyAppliedMet;
|
||||
|
||||
private final ArrayList<SplitHunk> myNotBound;
|
||||
@@ -61,6 +62,7 @@ public class GenericPatchApplier {
|
||||
debug("GenericPatchApplier created, hunks: " + hunks.size());
|
||||
myLines = new ArrayList<String>();
|
||||
Collections.addAll(myLines, LineTokenizer.tokenize(text, false));
|
||||
myBaseFileEndsWithNewLine = StringUtil.endsWithLineBreak(text);
|
||||
myHunks = hunks;
|
||||
final Comparator<TextRange> textRangeComparator =
|
||||
(o1, o2) -> new Integer(o1.getStartOffset()).compareTo(new Integer(o2.getStartOffset()));
|
||||
@@ -1041,16 +1043,25 @@ public class GenericPatchApplier {
|
||||
for (SplitHunk hunk : myNotBound) {
|
||||
linesToSb(sb, hunk.getAfterAll());
|
||||
}
|
||||
iterateTransformations(range -> linesToSb(sb, myLines.subList(range.getStartOffset(), range.getEndOffset() + 1)), range -> {
|
||||
iterateTransformations(range -> {
|
||||
linesToSb(sb, myLines.subList(range.getStartOffset(), range.getEndOffset() + 1));
|
||||
if (containsLastLine(range) && myBaseFileEndsWithNewLine) {
|
||||
sb.append('\n');
|
||||
}
|
||||
}, range -> {
|
||||
final MyAppliedData appliedData = myTransformations.get(range);
|
||||
linesToSb(sb, appliedData.getList());
|
||||
if (containsLastLine(range) && !mySuppressNewLineInEnd) {
|
||||
sb.append('\n');
|
||||
}
|
||||
});
|
||||
if (! mySuppressNewLineInEnd) {
|
||||
sb.append('\n');
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private boolean containsLastLine(@NotNull TextRange range) {
|
||||
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");
|
||||
|
||||
Reference in New Issue
Block a user