Skip to content

Commit fbd6f3c

Browse files
committed
refactor: refactor VariationUnparser.undiff
This kind of includes a breaking change: Keeping trailing empty lines and interpreting them as unchanged trailing empty lines. Although multiple empty lines should never occur in well formatted diffs (because each line contains at least the diff symbol), this is likely not intended and thus a bug. Note that preventing a new line in the special case of an empty output seems unnecessary but was explicitly done in the old version so the refactored version does this as well.
1 parent 9c91b08 commit fbd6f3c

1 file changed

Lines changed: 13 additions & 25 deletions

File tree

src/main/java/org/variantsync/diffdetective/variation/VariationUnparser.java

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import java.util.List;
55
import java.util.Stack;
66
import java.util.function.Function;
7+
import java.util.stream.Collectors;
8+
79
import org.eclipse.jgit.diff.DiffAlgorithm.SupportedAlgorithm;
10+
import org.variantsync.diffdetective.variation.diff.DiffType;
811
import org.variantsync.diffdetective.variation.diff.Time;
912
import org.variantsync.diffdetective.variation.diff.VariationDiff;
1013
import org.variantsync.diffdetective.variation.diff.construction.JGitDiff;
@@ -92,30 +95,15 @@ public static String variationDiffUnparser(VariationDiff<DiffLinesLabel> diff) t
9295
* @return the state before or after the diff
9396
*/
9497
public static String undiff(String diff, Time time) {
95-
if (diff.isEmpty()) {
96-
return "";
97-
} else {
98-
StringBuilder result = new StringBuilder();
99-
String[] textSplit = diff.split("\n");
100-
char zeichen;
101-
if (Time.AFTER == time) {
102-
zeichen = '-';
103-
} else {
104-
zeichen = '+';
105-
}
106-
for (String line : textSplit) {
107-
if (line.isEmpty()) {
108-
result.append(line);
109-
result.append("\n");
110-
} else if (line.charAt(0) != zeichen) {
111-
result.append(line.substring(1));
112-
result.append("\n");
113-
}
114-
}
115-
if (result.isEmpty()) {
116-
return "";
117-
}
118-
return result.toString();
119-
}
98+
String excludedDiffSymbol = DiffType.thatExistsOnlyAt(time.other()).symbol;
99+
100+
String result = diff
101+
.lines()
102+
.filter(line -> !line.startsWith(excludedDiffSymbol))
103+
// TODO assumes that all diff symbols are exactly 1 char long
104+
.map(line -> line.isEmpty() ? "" : line.substring(1))
105+
.collect(Collectors.joining("\n"));
106+
107+
return result.isEmpty() ? "" : result + "\n";
120108
}
121109
}

0 commit comments

Comments
 (0)