diff options
author | Roel Spilker <r.spilker@gmail.com> | 2013-11-20 00:55:44 +0100 |
---|---|---|
committer | Reinier Zwitserloot <reinier@zwitserloot.com> | 2013-12-11 22:40:48 +0100 |
commit | 54c30d98c06277f42d2098d4f0731580b9f095e9 (patch) | |
tree | e72bbd49c9df1d14ae2785915f80bd62279dfedb /src/delombok | |
parent | 749baeaaed83edec855db5ac14d273743f8e8142 (diff) | |
download | lombok-54c30d98c06277f42d2098d4f0731580b9f095e9.tar.gz lombok-54c30d98c06277f42d2098d4f0731580b9f095e9.tar.bz2 lombok-54c30d98c06277f42d2098d4f0731580b9f095e9.zip |
[Pretty] Delombok prettyprinter now adheres to whitspace formatting rules
Diffstat (limited to 'src/delombok')
-rw-r--r-- | src/delombok/lombok/delombok/Delombok.java | 2 | ||||
-rw-r--r-- | src/delombok/lombok/delombok/DelombokResult.java | 20 | ||||
-rw-r--r-- | src/delombok/lombok/delombok/PrettyCommentsPrinter.java | 9 |
3 files changed, 21 insertions, 10 deletions
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<CommentInfo> comments; private final JCCompilationUnit compilationUnit; private final boolean changed; + private final Map<String, String> formatPreferences; - public DelombokResult(List<CommentInfo> comments, JCCompilationUnit compilationUnit, boolean changed) { + public DelombokResult(List<CommentInfo> comments, JCCompilationUnit compilationUnit, boolean changed, Map<String, String> 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<CommentInfo>) 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<CommentInfo> comments) { + public PrettyCommentsPrinter(Writer out, JCCompilationUnit cu, List<CommentInfo> 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) { |