|
| 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 VariabilityAwareDiffer |
| 24 | + * @see VariationDiff |
| 25 | + */ |
| 26 | +public class VariabilityAwareDiffers { |
| 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 VariabilityAwareTextDiffer JGit(DiffAlgorithm.SupportedAlgorithm lineDiffAlgorithm, VariationDiffParseOptions parseOptions) { |
| 37 | + return new VariabilityAwareTextDiffer() { |
| 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 JGit(DiffAlgorithm.SupportedAlgorithm, VariationDiffParseOptions)} with |
| 57 | + * {@link DiffAlgorithm.SupportedAlgorithm#MYERS} and {@link VariationDiffParseOptions#Default}. |
| 58 | + */ |
| 59 | + public static VariabilityAwareTextDiffer JGitMyers() { |
| 60 | + return JGit(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 VariabilityAwareTreeDiffer<DiffLinesLabel> GumTree(VariationDiffParseOptions parseOptions, Matcher matcher) { |
| 72 | + return new VariabilityAwareTreeDiffer<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 GumTree(VariationDiffParseOptions, Matcher)} with {@link |
| 87 | + * VariationDiffParseOptions#Default} and {@link Matchers#getInstance {@code |
| 88 | + * Matchers.getInstance().getMatcher()}}. |
| 89 | + */ |
| 90 | + public static VariabilityAwareTreeDiffer<DiffLinesLabel> GumTree() { |
| 91 | + return GumTree(VariationDiffParseOptions.Default, Matchers.getInstance().getMatcher()); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Returns a differ that first parses a {@link VariationDiff} like {@link JGit} and then |
| 96 | + * {@link GumTreeDiff#improveMatching improves} the represented matching. |
| 97 | + * |
| 98 | + * @param lineDiffAlgorithm the Git diffing algorithm to use |
| 99 | + * @param parseOptions options for parsing the {@link VariationDiff} |
| 100 | + * @param matcher the matcher used for diffing the variation trees |
| 101 | + * @return the diffed {@link VariationDiff} |
| 102 | + */ |
| 103 | + public static VariabilityAwareTextDiffer JGitGumTreeHybrid(DiffAlgorithm.SupportedAlgorithm lineDiffAlgorithm, VariationDiffParseOptions parseOptions, Matcher matcher) { |
| 104 | + return new VariabilityAwareTextDiffer() { |
| 105 | + @Override |
| 106 | + public String diffLines(Reader before, Reader after) throws IOException { |
| 107 | + return JGitDiff.textDiff(IOUtils.toString(before), IOUtils.toString(after), lineDiffAlgorithm); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public VariationDiffParseOptions getParseOptions() { |
| 112 | + return parseOptions; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public VariationDiff<DiffLinesLabel> diff(Reader before, Reader after, Source beforeSource, Source afterSource) throws IOException, DiffParseException { |
| 117 | + VariationDiff<DiffLinesLabel> vd = VariabilityAwareTextDiffer.super.diff(before, after, beforeSource, afterSource); |
| 118 | + return new VariationDiff<>( |
| 119 | + GumTreeDiff.improveMatching(vd.getRoot(), matcher), |
| 120 | + new CompositeSource("GumTreeDiff.improveMatching", vd.getSource()) |
| 121 | + ); |
| 122 | + }; |
| 123 | + }; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Calls {@link JGitGumTreeHybrid(DiffAlgorithm.SupportedAlgorithm, VariationDiffParseOptions, |
| 128 | + * Matcher)} with {@link DiffAlgorithm.SupportedAlgorithm#MYERS}, {@link |
| 129 | + * VariationDiffParseOptions#Default}, and {@link Matchers#getInstance {@code |
| 130 | + * Matchers.getInstance().getMatcher()}}. |
| 131 | + */ |
| 132 | + public static VariabilityAwareTextDiffer JGitMyersGumTreeHybrid() { |
| 133 | + return JGitGumTreeHybrid(DiffAlgorithm.SupportedAlgorithm.MYERS, VariationDiffParseOptions.Default, Matchers.getInstance().getMatcher()); |
| 134 | + } |
| 135 | +} |
0 commit comments