aboutsummaryrefslogtreecommitdiff
path: root/src/delombok
diff options
context:
space:
mode:
Diffstat (limited to 'src/delombok')
-rw-r--r--src/delombok/lombok/delombok/Comment.java12
-rw-r--r--src/delombok/lombok/delombok/CommentCollectingScanner.java16
-rw-r--r--src/delombok/lombok/delombok/CommentPreservingParser.java29
-rw-r--r--src/delombok/lombok/delombok/PrettyCommentsPrinter.java37
4 files changed, 79 insertions, 15 deletions
diff --git a/src/delombok/lombok/delombok/Comment.java b/src/delombok/lombok/delombok/Comment.java
index c264733f..55a5c46d 100644
--- a/src/delombok/lombok/delombok/Comment.java
+++ b/src/delombok/lombok/delombok/Comment.java
@@ -23,11 +23,21 @@ package lombok.delombok;
public final class Comment {
final int pos;
+ final int prevEndPos;
final String content;
+ final int endPos;
+ final boolean newLine;
- public Comment(int pos, String content) {
+ public Comment(int prevEndPos, int pos, int endPos, String content, boolean newLine) {
this.pos = pos;
+ this.prevEndPos = prevEndPos;
+ this.endPos = endPos;
this.content = content;
+ this.newLine = newLine;
+ }
+
+ public boolean isConnected() {
+ return !newLine && prevEndPos == pos;
}
public String summary() {
diff --git a/src/delombok/lombok/delombok/CommentCollectingScanner.java b/src/delombok/lombok/delombok/CommentCollectingScanner.java
index 6f593c7f..c38aef24 100644
--- a/src/delombok/lombok/delombok/CommentCollectingScanner.java
+++ b/src/delombok/lombok/delombok/CommentCollectingScanner.java
@@ -80,6 +80,20 @@ public class CommentCollectingScanner extends Scanner {
@Override
protected void processComment(CommentStyle style) {
- comments.add(pos(), new String(getRawCharacters(pos(), endPos())));
+ int prevEndPos = prevEndPos();
+ int pos = pos();
+ boolean newLine = containsNewLine(prevEndPos, pos);
+ String content = new String(getRawCharacters(pos, endPos()));
+ comments.add(prevEndPos, pos, endPos(), content, newLine);
+ }
+
+
+ private boolean containsNewLine(int from, int to) {
+ for (char c : getRawCharacters(from, to)) {
+ if (c == '\n' || c == '\r') {
+ return true;
+ }
+ }
+ return false;
}
} \ No newline at end of file
diff --git a/src/delombok/lombok/delombok/CommentPreservingParser.java b/src/delombok/lombok/delombok/CommentPreservingParser.java
index 51df5ed1..9fd8778c 100644
--- a/src/delombok/lombok/delombok/CommentPreservingParser.java
+++ b/src/delombok/lombok/delombok/CommentPreservingParser.java
@@ -24,11 +24,13 @@ package lombok.delombok;
import java.io.IOException;
import java.io.Writer;
import java.util.Collections;
+import java.util.Date;
import javax.annotation.processing.Messager;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
+import javax.tools.JavaFileObject;
import javax.tools.Diagnostic.Kind;
import lombok.javac.JavacTransformer;
@@ -52,7 +54,7 @@ public class CommentPreservingParser {
this.encoding = encoding;
}
- public ParseResult parseFile(String fileName) throws IOException {
+ public ParseResult parse(String fileName, boolean forceProcessing) throws IOException {
Context context = new Context();
Options.instance(context).put(OptionName.ENCODING, encoding);
@@ -74,9 +76,8 @@ public class CommentPreservingParser {
@SuppressWarnings("deprecation")
JCCompilationUnit cu = compiler.parse(fileName);
- new JavacTransformer(messager).transform(context, Collections.singleton(cu));
-
- return new ParseResult(comments.comments, cu);
+ boolean changed = new JavacTransformer(messager).transform(context, Collections.singleton(cu));
+ return new ParseResult(comments.comments, cu, forceProcessing || changed);
}
private static final Messager messager = new Messager() {
@@ -100,21 +101,33 @@ public class CommentPreservingParser {
static class Comments {
List<Comment> comments = List.nil();
- void add(int pos, String content) {
- comments = comments.append(new Comment(pos, content));
+ void add(int prevEndPos, int pos, int endPos, String content, boolean newLine) {
+ comments = comments.append(new Comment(prevEndPos, pos, endPos, content, newLine));
}
}
public static class ParseResult {
private final List<Comment> comments;
private final JCCompilationUnit compilationUnit;
+ private final boolean changed;
- private ParseResult(List<Comment> comments, JCCompilationUnit compilationUnit) {
+ private ParseResult(List<Comment> comments, JCCompilationUnit compilationUnit, boolean changed) {
this.comments = comments;
this.compilationUnit = compilationUnit;
+ this.changed = changed;
}
- public void print(Writer out) {
+ public void print(Writer out) throws IOException {
+ if (!changed) {
+ JavaFileObject sourceFile = compilationUnit.getSourceFile();
+ if (sourceFile != null) {
+ out.write(sourceFile.getCharContent(true).toString());
+ System.out.println(out.toString());
+ return;
+ }
+ }
+
+ out.write("// Generated by delombok at " + new Date() + "\n");
compilationUnit.accept(new PrettyCommentsPrinter(out, compilationUnit, comments));
}
}
diff --git a/src/delombok/lombok/delombok/PrettyCommentsPrinter.java b/src/delombok/lombok/delombok/PrettyCommentsPrinter.java
index 2fb62e53..611dd417 100644
--- a/src/delombok/lombok/delombok/PrettyCommentsPrinter.java
+++ b/src/delombok/lombok/delombok/PrettyCommentsPrinter.java
@@ -179,7 +179,32 @@ public class PrettyCommentsPrinter extends JCTree.Visitor {
align();
}
}
-
+
+ private void consumeTrailingComments(int from) throws IOException {
+ boolean shouldIndent = !newLine;
+ Comment head = comments.head;
+ while (comments.nonEmpty() && head.prevEndPos == from) {
+ from = head.endPos;
+ if (!head.isConnected()) {
+ if (head.newLine) {
+ if (!newLine) {
+ println();
+ align();
+ }
+ }
+ else {
+ print(" ");
+ }
+ }
+ print(head.content);
+ comments = comments.tail;
+ head = comments.head;
+ }
+ if (newLine && shouldIndent) {
+ align();
+ }
+ }
+
/** The output stream on which trees are printed.
*/
Writer out;
@@ -277,15 +302,17 @@ public class PrettyCommentsPrinter extends JCTree.Visitor {
if (tree == null) print("/*missing*/");
else {
consumeComments(tree.pos);
- tree.accept(this);
- consumeComments(endPos(tree));
+ tree.accept(this);
+ int endPos = endPos(tree);
+ consumeTrailingComments(endPos);
+ consumeComments(endPos);
}
} catch (UncheckedIOException ex) {
IOException e = new IOException(ex.getMessage());
e.initCause(ex);
throw e;
} finally {
- this.prec = prevPrec;
+ this.prec = prevPrec;
}
}
@@ -301,7 +328,7 @@ public class PrettyCommentsPrinter extends JCTree.Visitor {
public void printStat(JCTree tree) throws IOException {
printExpr(tree, TreeInfo.notExpression);
}
-
+
/** Derived visitor method: print list of expression trees, separated by given string.
* @param sep the separator string
*/