From 402139a8070c2822505cce767c90d769d4290594 Mon Sep 17 00:00:00 2001 From: pmbittner Date: Mon, 14 Oct 2024 12:48:03 +0200 Subject: [PATCH] feat: flags for TextDiffToTikz + better defaults --- .../internal/TextDiffToTikz.java | 35 +++++++++++++------ 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/variantsync/diffdetective/internal/TextDiffToTikz.java b/src/main/java/org/variantsync/diffdetective/internal/TextDiffToTikz.java index 2bbccc934..a080e3c61 100644 --- a/src/main/java/org/variantsync/diffdetective/internal/TextDiffToTikz.java +++ b/src/main/java/org/variantsync/diffdetective/internal/TextDiffToTikz.java @@ -18,10 +18,13 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; -public class TextDiffToTikz { +public record TextDiffToTikz(boolean collapseMultipleCodeLines, boolean ignoreEmptyLines) { public static String[] UNICODE_PROP_SYMBOLS = new String[]{"¬", "∧", "∨", "⇒", "⇔"}; /** @@ -40,6 +43,12 @@ public static void main(String[] args) throws IOException, DiffParseException { return; } + final Path fileToConvert = Path.of(args[0]); + if (!Files.exists(fileToConvert)) { + Logger.error("Path {} does not exist!", fileToConvert); + return; + } + final GraphvizExporter.LayoutAlgorithm layout; if (args.length < 2) { layout = GraphvizExporter.LayoutAlgorithm.DOT; @@ -47,30 +56,36 @@ public static void main(String[] args) throws IOException, DiffParseException { layout = GraphvizExporter.LayoutAlgorithm.valueOf(args[1].toUpperCase()); } - final Path fileToConvert = Path.of(args[0]); - if (!Files.exists(fileToConvert)) { - Logger.error("Path {} does not exist!", fileToConvert); - return; + // FIXME: Use a dedicated argument parser in the future. + boolean collapseMultipleCodeLines = false; + boolean ignoreEmptyLines = true; + List flags = new ArrayList<>(Arrays.asList(args).subList(2, args.length)); + if (flags.contains("-c") || flags.contains("--collapse-blocks")) { + collapseMultipleCodeLines = true; + } + if (flags.contains("-e") || flags.contains("--with-empty-lines")) { + ignoreEmptyLines = false; } + TextDiffToTikz me = new TextDiffToTikz(collapseMultipleCodeLines, ignoreEmptyLines); if (Files.isDirectory(fileToConvert)) { Logger.info("Processing directory " + fileToConvert); for (Path file : FileUtils.listAllFilesRecursively(fileToConvert)) { if (FileUtils.hasExtension(file, ".diff")) { - textDiff2Tikz(file, layout); + me.textDiff2Tikz(file, layout); } } } else { - textDiff2Tikz(fileToConvert, layout); + me.textDiff2Tikz(fileToConvert, layout); } } - public static void textDiff2Tikz(Path fileToConvert, GraphvizExporter.LayoutAlgorithm layout) throws IOException, DiffParseException { + public void textDiff2Tikz(Path fileToConvert, GraphvizExporter.LayoutAlgorithm layout) throws IOException, DiffParseException { Logger.info("Converting file " + fileToConvert); Logger.info("Using layout " + layout.getExecutableName()); final Path targetFile = fileToConvert.resolveSibling(fileToConvert.getFileName() + ".tikz"); - final VariationDiff d = VariationDiff.fromFile(fileToConvert, new VariationDiffParseOptions(true, true)); + final VariationDiff d = VariationDiff.fromFile(fileToConvert, new VariationDiffParseOptions(collapseMultipleCodeLines, ignoreEmptyLines)); final String tikz = exportAsTikz(d, layout); IO.write(targetFile, tikz); Logger.info("Wrote file " + targetFile); @@ -79,7 +94,7 @@ public static void textDiff2Tikz(Path fileToConvert, GraphvizExporter.LayoutAlgo public static String exportAsTikz(final VariationDiff variationDiff, GraphvizExporter.LayoutAlgorithm layout) throws IOException { // Export the test case var tikzOutput = new ByteArrayOutputStream(); - new TikzExporter(format).exportVariationDiff(variationDiff, layout, tikzOutput); + new TikzExporter<>(format).exportVariationDiff(variationDiff, layout, tikzOutput); return tikzOutput.toString(); }