Skip to content

Commit 9de8a26

Browse files
committed
refactor: remove duplicated tree unparsing code
1 parent 68d8131 commit 9de8a26

4 files changed

Lines changed: 19 additions & 64 deletions

File tree

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

Lines changed: 5 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package org.variantsync.diffdetective.variation;
22

33
import java.io.IOException;
4-
import java.util.List;
5-
import java.util.Stack;
6-
import java.util.function.Function;
74
import java.util.stream.Collectors;
85

96
import org.eclipse.jgit.diff.DiffAlgorithm.SupportedAlgorithm;
@@ -12,81 +9,33 @@
129
import org.variantsync.diffdetective.variation.diff.VariationDiff;
1310
import org.variantsync.diffdetective.variation.diff.construction.JGitDiff;
1411
import org.variantsync.diffdetective.variation.tree.VariationTree;
15-
import org.variantsync.diffdetective.variation.tree.VariationTreeNode;
1612

1713
public class VariationUnparser {
1814
/**
1915
* Unparse {@link VariationTree}s into a {@link String}.
2016
*
2117
* @param tree that is unparsed
22-
* @param linesToLabel a function that converts lists of lines into labels
2318
* @return the unparsed variation tree
2419
* @param <L> the type of labels of the tree
2520
*/
26-
public static <L extends Label> String unparseTree(VariationTree<L> tree, Function<List<String>, L> linesToLabel) {
27-
if (!tree.root().getChildren().isEmpty()) {
28-
StringBuilder result = new StringBuilder();
29-
Stack<VariationTreeNode<L>> stack = new Stack<>();
30-
for (int i = tree.root().getChildren().size() - 1; i >= 0; i--) {
31-
stack.push(tree.root().getChildren().get(i));
32-
}
33-
while (!stack.empty()) {
34-
VariationTreeNode<L> node = stack.pop();
35-
if (node.isIf()) {
36-
stack.push(new VariationTreeNode<>(NodeType.ARTIFACT, null, null,
37-
linesToLabel.apply(node.getEndIf())));
38-
}
39-
for (String line : node.getLabel().getLines()) {
40-
result.append(line);
41-
result.append("\n");
42-
}
43-
for (int i = node.getChildren().size() - 1; i >= 0; i--) {
44-
stack.push(node.getChildren().get(i));
45-
}
46-
}
47-
return result.substring(0, result.length() - 1);
48-
} else {
49-
return "";
50-
}
51-
}
52-
53-
/**
54-
* Unparse {@link VariationTree}s into a {@link String}.
55-
*
56-
* @param tree that is unparsed
57-
* @param linesToLabel a function that converts lists of lines into labels
58-
* @return the unparsed variation tree
59-
*/
60-
public static String unparseTree(VariationTree<DiffLinesLabel> tree) {
61-
return unparseTree(tree, DiffLinesLabel::withInvalidLineNumbers);
21+
public static <L extends Label> String unparseTree(VariationTree<L> tree) {
22+
return tree.unparse();
6223
}
6324

6425
/**
6526
* Unparse {@link VariationDiff}s into a {@link String}.
6627
*
6728
* @param diff that is unparsed
68-
* @param linesToLabel a function that converts lists of lines into labels
6929
* @return the unparsed variation diff
7030
* @param <L> the type of labels of the tree
7131
* @throws IOException
7232
*/
73-
public static <L extends Label> String unparseDiff(VariationDiff<L> diff, Function<List<String>, L> linesToLabel) throws IOException {
74-
String tree1 = unparseTree(diff.project(Time.BEFORE), linesToLabel);
75-
String tree2 = unparseTree(diff.project(Time.AFTER), linesToLabel);
33+
public static <L extends Label> String unparseDiff(VariationDiff<L> diff) throws IOException {
34+
String tree1 = unparseTree(diff.project(Time.BEFORE));
35+
String tree2 = unparseTree(diff.project(Time.AFTER));
7636
return JGitDiff.textDiff(tree1, tree2, SupportedAlgorithm.MYERS);
7737
}
7838

79-
/**
80-
* Unparse {@link VariationDiff}s into a {@link String}.
81-
*
82-
* @param diff that is unparsed
83-
* @return the unparsed variation diff
84-
* @throws IOException
85-
*/
86-
public static String unparseDiff(VariationDiff<DiffLinesLabel> diff) throws IOException {
87-
return unparseDiff(diff, DiffLinesLabel::withInvalidLineNumbers);
88-
}
89-
9039
/**
9140
* Extract the state of the diffed text before or after {@code diff}.
9241
*

src/main/java/org/variantsync/diffdetective/variation/diff/view/DiffView.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,7 @@ public static <L extends Label> VariationDiff<DiffLinesLabel> naive(final Variat
114114
final Map<VariationTreeNode<L>, Projection<L>> invCopyMemory = CollectionUtils.invert(copyMemory, HashMap::new);
115115
TreeView.treeInline(treeView.root(), v -> inView.test(t, invCopyMemory.get(v)));
116116

117-
final StringBuilder b = new StringBuilder();
118-
treeView.root().printSourceCode(b);
119-
projectionViewText[i] = b.toString();
117+
projectionViewText[i] = treeView.unparse();
120118
}
121119

122120
return naive(d, rho, projectionViewText);

src/main/java/org/variantsync/diffdetective/variation/tree/VariationNode.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -574,20 +574,22 @@ public void assertConsistency() {
574574
*
575575
* <p>This method assumes that all labels of this subtree represent source code lines.
576576
*/
577-
public void printSourceCode(final StringBuilder output) {
577+
public void unparse(final StringBuilder output) {
578578
for (final String line : getLabel().getLines()) {
579579
output.append(line);
580580
output.append(StringUtils.LINEBREAK);
581581
}
582582

583583
for (final var child : getChildren()) {
584-
child.printSourceCode(output);
584+
child.unparse(output);
585585
}
586586

587587
// Add #endif after macro
588-
if (isIf() && !isRoot()) {
589-
output.append("#endif");
590-
output.append(StringUtils.LINEBREAK);
588+
if (getEndIf() != null) {
589+
for (final String line : getEndIf()) {
590+
output.append(line);
591+
output.append(StringUtils.LINEBREAK);
592+
}
591593
}
592594
}
593595

src/main/java/org/variantsync/diffdetective/variation/tree/VariationTree.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@ public VariationTree<L> deepCopy(final Map<VariationTreeNode<L>, VariationTreeNo
207207
return new VariationTree<>(root.deepCopy(oldToNew), this.source);
208208
}
209209

210+
public String unparse() {
211+
var result = new StringBuilder();
212+
root().unparse(result);
213+
return result.toString();
214+
}
215+
210216
@Override
211217
public String toString() {
212218
return "variation tree from " + source;

0 commit comments

Comments
 (0)