|
| 1 | +package org.variantsync.diffdetective.variation.diff; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.Reader; |
| 5 | + |
| 6 | +import org.apache.commons.io.IOUtils; |
| 7 | +import org.eclipse.jgit.diff.DiffAlgorithm; |
| 8 | +import org.variantsync.diffdetective.diff.result.DiffParseException; |
| 9 | +import org.variantsync.diffdetective.util.CompositeSource; |
| 10 | +import org.variantsync.diffdetective.util.Source; |
| 11 | +import org.variantsync.diffdetective.variation.DiffLinesLabel; |
| 12 | +import org.variantsync.diffdetective.variation.diff.construction.GumTreeDiff; |
| 13 | +import org.variantsync.diffdetective.variation.diff.construction.JGitDiff; |
| 14 | +import org.variantsync.diffdetective.variation.diff.parse.VariationDiffParseOptions; |
| 15 | +import org.variantsync.diffdetective.variation.diff.parse.VariationDiffParser; // For Javadoc |
| 16 | +import org.variantsync.diffdetective.variation.tree.VariationTree; |
| 17 | + |
| 18 | +import com.github.gumtreediff.matchers.Matcher; |
| 19 | +import com.github.gumtreediff.matchers.Matchers; |
| 20 | + |
| 21 | +/** |
| 22 | + * A collection of differs that create variation diffs. |
| 23 | + * @see Differ |
| 24 | + * @see VariationDiff |
| 25 | + */ |
| 26 | +public class Differs { |
| 27 | + /** |
| 28 | + * Returns a differ that {@link VariationDiffParser#createVariationDiff parses} a |
| 29 | + * {@link VariationDiff} after {@link JGitDiff#textDiff diffing} with |
| 30 | + * {@link DiffAlgorithm.SupportedAlgorithm one of JGit's differ}. |
| 31 | + * |
| 32 | + * @param lineDiffAlgorithm the Git diffing algorithm to use |
| 33 | + * @param parseOptions options for parsing the {@link VariationDiff} |
| 34 | + * @return the diffed {@link VariationDiff} |
| 35 | + */ |
| 36 | + public static LineDiffer lineDiffer(DiffAlgorithm.SupportedAlgorithm lineDiffAlgorithm, VariationDiffParseOptions parseOptions) { |
| 37 | + return new LineDiffer() { |
| 38 | + @Override |
| 39 | + public String diffLines(Reader before, Reader after) throws IOException { |
| 40 | + return JGitDiff.textDiff(IOUtils.toString(before), IOUtils.toString(after), lineDiffAlgorithm); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public String diffLines(String before, String after) throws IOException { |
| 45 | + return JGitDiff.textDiff(before, after, lineDiffAlgorithm); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public VariationDiffParseOptions getParseOptions() { |
| 50 | + return parseOptions; |
| 51 | + } |
| 52 | + }; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Calls {@link lineDiffer(DiffAlgorithm.SupportedAlgorithm, VariationDiffParseOptions)} with |
| 57 | + * {@link DiffAlgorithm.SupportedAlgorithm.MYERS} and {@link VariationDiffParseOptions.Default}. |
| 58 | + */ |
| 59 | + public static LineDiffer lineDiffer() { |
| 60 | + return lineDiffer(DiffAlgorithm.SupportedAlgorithm.MYERS, VariationDiffParseOptions.Default); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Returns a differ that {@link VariationTree#fromFile parses} two variation trees and then |
| 65 | + * {@link GumTreeDiff#diffUsingMatching diffs} these variation trees. |
| 66 | + * |
| 67 | + * @param parseOptions options for parsing the {@link VariationTree}s |
| 68 | + * @param matcher the matcher used for diffing the {@link VariationTree}s |
| 69 | + * @return the diffed {@link VariationDiff} |
| 70 | + */ |
| 71 | + public static TreeDiffer<DiffLinesLabel> treeDiffer(VariationDiffParseOptions parseOptions, Matcher matcher) { |
| 72 | + return new TreeDiffer<DiffLinesLabel>() { |
| 73 | + @Override |
| 74 | + public VariationDiff<DiffLinesLabel> diffTrees(VariationTree<DiffLinesLabel> before, VariationTree<DiffLinesLabel> after) { |
| 75 | + return GumTreeDiff.diffUsingMatching(before, after, matcher); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public VariationDiffParseOptions getParseOptions() { |
| 80 | + return parseOptions; |
| 81 | + } |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Calls {@link treeDiffer(VariationDiffParseOptions, Matcher)} with {@link |
| 87 | + * VariationDiffParseOptions.Default} and {@link Matchers.getInstance().getMatcher()}. |
| 88 | + */ |
| 89 | + public static TreeDiffer<DiffLinesLabel> treeDiffer() { |
| 90 | + return treeDiffer(VariationDiffParseOptions.Default, Matchers.getInstance().getMatcher()); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Returns a differ that first parses a {@link VariationDiff} like {@link lineDiffer} and then |
| 95 | + * {@link GumTreeDiff#improveMatching improves} the represented matching. |
| 96 | + * |
| 97 | + * @param lineDiffAlgorithm the Git diffing algorithm to use |
| 98 | + * @param parseOptions options for parsing the {@link VariationDiff} |
| 99 | + * @param matcher the matcher used for diffing the variation trees |
| 100 | + * @return the diffed {@link VariationDiff} |
| 101 | + */ |
| 102 | + public static LineDiffer hybrid(DiffAlgorithm.SupportedAlgorithm lineDiffAlgorithm, VariationDiffParseOptions parseOptions, Matcher matcher) { |
| 103 | + return new LineDiffer() { |
| 104 | + @Override |
| 105 | + public String diffLines(Reader before, Reader after) throws IOException { |
| 106 | + return JGitDiff.textDiff(IOUtils.toString(before), IOUtils.toString(after), lineDiffAlgorithm); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public VariationDiffParseOptions getParseOptions() { |
| 111 | + return parseOptions; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public VariationDiff<DiffLinesLabel> diff(Reader before, Reader after, Source beforeSource, Source afterSource) throws IOException, DiffParseException { |
| 116 | + VariationDiff<DiffLinesLabel> vd = LineDiffer.super.diff(before, after, beforeSource, afterSource); |
| 117 | + return new VariationDiff<>( |
| 118 | + GumTreeDiff.improveMatching(vd.getRoot(), matcher), |
| 119 | + new CompositeSource("GumTreeDiff.improveMatching", vd.getSource()) |
| 120 | + ); |
| 121 | + }; |
| 122 | + }; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Calls {@link treeDiffer(VariationDiffParseOptions, Matcher)} with {@link |
| 127 | + * DiffAlgorithm.SupportedAlgorithm.MYERS}, {@link VariationDiffParseOptions.Default}, and |
| 128 | + * {@link Matchers.getInstance().getMatcher()}. |
| 129 | + */ |
| 130 | + public static LineDiffer hybrid() { |
| 131 | + return hybrid(DiffAlgorithm.SupportedAlgorithm.MYERS, VariationDiffParseOptions.Default, Matchers.getInstance().getMatcher()); |
| 132 | + } |
| 133 | +} |
0 commit comments