Skip to content

Commit 92b9247

Browse files
eugen-shulimovibbem
authored andcommitted
feat: create an unparser for variation diffs
1 parent e9fb7c5 commit 92b9247

1 file changed

Lines changed: 73 additions & 18 deletions

File tree

Lines changed: 73 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,85 @@
11
package org.variantsync.diffdetective.variation;
22

3+
import java.io.IOException;
4+
import java.util.List;
35
import java.util.Stack;
6+
import java.util.function.Function;
7+
import org.eclipse.jgit.diff.DiffAlgorithm.SupportedAlgorithm;
8+
import org.variantsync.diffdetective.variation.diff.Time;
9+
import org.variantsync.diffdetective.variation.diff.VariationDiff;
10+
import org.variantsync.diffdetective.variation.diff.construction.JGitDiff;
411
import org.variantsync.diffdetective.variation.tree.VariationTree;
512
import org.variantsync.diffdetective.variation.tree.VariationTreeNode;
613

714
public class VariationUnparser {
8-
public static String variationTreeUnparser(VariationTree<? extends Label> tree) {
9-
StringBuilder result = new StringBuilder();
10-
Stack<VariationTreeNode<? extends Label>> stack = new Stack<>();
11-
for (int i = tree.root().getChildren().size() - 1; i >= 0; i--) {
12-
stack.push(tree.root().getChildren().get(i));
13-
}
14-
while (!stack.empty()) {
15-
VariationTreeNode<? extends Label> node = stack.pop();
16-
for (String line : node.getLabel().getLines()) {
17-
result.append(line);
18-
result.append("\n");
19-
}
20-
if (node.isIf()) {
21-
stack.push(new VariationTreeNode<>(NodeType.ARTIFACT, null, null,
22-
DiffLinesLabel.withInvalidLineNumbers(node.getEndIf())));
15+
/**
16+
* Unparse VariationTrees to Text/String
17+
*
18+
* @param tree VariationTree, that be unparsed
19+
* @param linesToLabel Function, that return list of String and has a Class T
20+
* @return String, the result of unparsing
21+
* @param <T> that implements Label
22+
*/
23+
public static <T extends Label> String variationTreeUnparser(VariationTree<T> tree, Function<List<String>, T> linesToLabel) {
24+
if (!tree.root().getChildren().isEmpty()) {
25+
StringBuilder result = new StringBuilder();
26+
Stack<VariationTreeNode<T>> stack = new Stack<>();
27+
for (int i = tree.root().getChildren().size() - 1; i >= 0; i--) {
28+
stack.push(tree.root().getChildren().get(i));
2329
}
24-
for (int i = node.getChildren().size() - 1; i >= 0; i--) {
25-
stack.push(node.getChildren().get(i));
30+
while (!stack.empty()) {
31+
VariationTreeNode<T> node = stack.pop();
32+
for (String line : node.getLabel().getLines()) {
33+
result.append(line);
34+
result.append("\n");
35+
}
36+
if (node.isIf()) {
37+
stack.push(new VariationTreeNode<>(NodeType.ARTIFACT, null, null,
38+
linesToLabel.apply(node.getEndIf())));
39+
}
40+
for (int i = node.getChildren().size() - 1; i >= 0; i--) {
41+
stack.push(node.getChildren().get(i));
42+
}
2643
}
44+
return result.substring(0, result.length() - 1);
45+
} else {
46+
return "";
2747
}
28-
return result.substring(0, result.length() - 1);
48+
}
49+
50+
/**
51+
* Unparse VariationTrees to Text/String
52+
*
53+
* @param tree VariationTree, that be unparsed
54+
* @return String, the result of unparsing
55+
*/
56+
public static String variationTreeUnparser(VariationTree<DiffLinesLabel> tree) {
57+
return variationTreeUnparser(tree, DiffLinesLabel::withInvalidLineNumbers);
58+
}
59+
60+
/**
61+
* Unparse VariationDiffs to Text/String
62+
*
63+
* @param diff VariationDiff, that be unparsed
64+
* @param linesToLabel Function, that return list of String and has a Class T
65+
* @return String, the result of unparsing
66+
* @param <T> that implements Label
67+
* @throws IOException
68+
*/
69+
public static <T extends Label> String variationDiffUnparser(VariationDiff<T> diff, Function<List<String>, T> linesToLabel) throws IOException {
70+
String tree1 = variationTreeUnparser(diff.project(Time.BEFORE), linesToLabel);
71+
String tree2 = variationTreeUnparser(diff.project(Time.AFTER), linesToLabel);
72+
return JGitDiff.textDiff(tree1, tree2, SupportedAlgorithm.MYERS);
73+
}
74+
75+
/**
76+
* Unparse VariationDiffs to Text/String
77+
*
78+
* @param diff VariationDiff, that be unparsed
79+
* @return String, the result of unparsing
80+
* @throws IOException
81+
*/
82+
public static String variationDiffUnparser(VariationDiff<DiffLinesLabel> diff) throws IOException {
83+
return variationDiffUnparser(diff, DiffLinesLabel::withInvalidLineNumbers);
2984
}
3085
}

0 commit comments

Comments
 (0)