mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-19 02:14:30 +00:00
[Pretty] Delombok prettyprinter now adheres to whitspace formatting rules
This commit is contained in:
1 parent
749baeaaed
commit
54c30d98c0
3 files changed
+21
-10
No files matched your search
@@ -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;
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in new issue
Block a user