From 54c30d98c06277f42d2098d4f0731580b9f095e9 Mon Sep 17 00:00:00 2001 From: Roel Spilker Date: Wed, 20 Nov 2013 00:55:44 +0100 Subject: [Pretty] Delombok prettyprinter now adheres to whitspace formatting rules --- src/delombok/lombok/delombok/Delombok.java | 2 +- src/delombok/lombok/delombok/DelombokResult.java | 20 +++++++++++++++----- .../lombok/delombok/PrettyCommentsPrinter.java | 9 +++++---- 3 files changed, 21 insertions(+), 10 deletions(-) (limited to 'src/delombok') diff --git a/src/delombok/lombok/delombok/Delombok.java b/src/delombok/lombok/delombok/Delombok.java index f24e73aa..fb1098b5 100644 --- a/src/delombok/lombok/delombok/Delombok.java +++ b/src/delombok/lombok/delombok/Delombok.java @@ -471,7 +471,7 @@ public class Delombok { callFlowMethodOnJavaCompiler(delegate, care); for (JCCompilationUnit unit : roots) { - DelombokResult result = new DelombokResult(catcher.getComments(unit), unit, force || options.isChanged(unit)); + DelombokResult result = new DelombokResult(catcher.getComments(unit), unit, force || options.isChanged(unit), formatPrefs); if (verbose) feedback.printf("File: %s [%s]\n", unit.sourcefile.getName(), result.isChanged() ? "delomboked" : "unchanged"); Writer rawWriter; if (presetWriter != null) rawWriter = presetWriter; diff --git a/src/delombok/lombok/delombok/DelombokResult.java b/src/delombok/lombok/delombok/DelombokResult.java index 866082d6..52e47e02 100644 --- a/src/delombok/lombok/delombok/DelombokResult.java +++ b/src/delombok/lombok/delombok/DelombokResult.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.io.Writer; import java.util.Date; import java.util.List; +import java.util.Map; import javax.tools.JavaFileObject; @@ -36,18 +37,20 @@ public class DelombokResult { private final List comments; private final JCCompilationUnit compilationUnit; private final boolean changed; + private final Map formatPreferences; - public DelombokResult(List comments, JCCompilationUnit compilationUnit, boolean changed) { + public DelombokResult(List comments, JCCompilationUnit compilationUnit, boolean changed, Map formatPreferences) { this.comments = comments; this.compilationUnit = compilationUnit; this.changed = changed; + this.formatPreferences = formatPreferences; } public void print(Writer out) throws IOException { if (!changed) { - JavaFileObject sourceFile = compilationUnit.getSourceFile(); - if (sourceFile != null) { - out.write(sourceFile.getCharContent(true).toString()); + CharSequence content = getContent(); + if (content != null) { + out.write(content.toString()); return; } } @@ -60,7 +63,14 @@ public class DelombokResult { if (comments instanceof com.sun.tools.javac.util.List) comments_ = (com.sun.tools.javac.util.List) comments; else comments_ = com.sun.tools.javac.util.List.from(comments.toArray(new CommentInfo[0])); - compilationUnit.accept(new PrettyCommentsPrinter(out, compilationUnit, comments_)); + FormatPreferences preferences = new FormatPreferenceScanner().scan(formatPreferences, getContent()); + compilationUnit.accept(new PrettyCommentsPrinter(out, compilationUnit, comments_, preferences)); + } + + private CharSequence getContent() throws IOException { + JavaFileObject sourceFile = compilationUnit.getSourceFile(); + if (sourceFile == null) return null; + return sourceFile.getCharContent(true); } public boolean isChanged() { diff --git a/src/delombok/lombok/delombok/PrettyCommentsPrinter.java b/src/delombok/lombok/delombok/PrettyCommentsPrinter.java index 5284d691..cfe8b6c4 100644 --- a/src/delombok/lombok/delombok/PrettyCommentsPrinter.java +++ b/src/delombok/lombok/delombok/PrettyCommentsPrinter.java @@ -187,12 +187,13 @@ public class PrettyCommentsPrinter extends JCTree.Visitor { // This flag is set just before printing the vardef and cleared when printing its modifiers. private boolean suppressFinalAndSemicolonsInTry = false; - private final String indentation = "\t"; + private final FormatPreferences formatPreferences; - public PrettyCommentsPrinter(Writer out, JCCompilationUnit cu, List comments) { + public PrettyCommentsPrinter(Writer out, JCCompilationUnit cu, List comments, FormatPreferences preferences) { this.out = out; this.comments = comments; this.cu = cu; + this.formatPreferences = preferences; } private int endPos(JCTree tree) { @@ -303,7 +304,7 @@ public class PrettyCommentsPrinter extends JCTree.Visitor { onNewLine = false; aligned = true; needsAlign = false; - for (int i = 0; i < lmargin; i++) out.write(indentation); + for (int i = 0; i < lmargin; i++) out.write(formatPreferences.indent()); } /** Increase left margin by indentation width. @@ -466,7 +467,7 @@ public class PrettyCommentsPrinter extends JCTree.Visitor { } private boolean suppressAlignmentForEmptyLines(JCTree tree) { - return startsWithNewLine(tree); + return !formatPreferences.fillEmpties() && startsWithNewLine(tree); } private boolean startsWithNewLine(JCTree tree) { -- cgit