Improved comment placement (newlines, whitespace)

This commit is contained in:
Roel Spilker committed 2009-11-29 16:44:05 +01:00
1 parent f0585f4ed4
commit 3cf45e932b
8 files changed
+94 -44

No files matched your search

+13 -7
View File
@@ -22,22 +22,28 @@
package lombok.delombok;
public final class Comment {
public enum StartConnection {
START_OF_LINE,
ON_NEXT_LINE,
DIRECT_AFTER_PREVIOUS,
AFTER_PREVIOUS
}
final int pos;
final int prevEndPos;
final String content;
final int endPos;
final boolean newLine;
final StartConnection start;
final boolean newLineAfter;
public Comment(int prevEndPos, int pos, int endPos, String content, boolean newLine) {
public Comment(int prevEndPos, int pos, int endPos, String content, StartConnection start, boolean newLineAfter) {
this.pos = pos;
this.prevEndPos = prevEndPos;
this.endPos = endPos;
this.content = content;
this.newLine = newLine;
}
public boolean isConnected() {
return !newLine && prevEndPos == pos;
this.start = start;
this.newLineAfter = newLineAfter;
}
public String summary() {
@@ -23,6 +23,7 @@ package lombok.delombok;
import java.nio.CharBuffer;
import lombok.delombok.Comment.StartConnection;
import lombok.delombok.CommentPreservingParser.Comments;
import com.sun.tools.javac.parser.Scanner;
@@ -79,17 +80,32 @@ public class CommentCollectingScanner extends Scanner {
protected void processComment(CommentStyle style) {
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);
int endPos = endPos();
String content = new String(getRawCharacters(pos, endPos));
boolean lineBreakAfter = isNewLine(getRawCharacters(endPos, endPos + 1)[0]);
StartConnection start = determineStartConnection(prevEndPos, pos);
Comment comment = new Comment(prevEndPos, pos, endPos, content, start, lineBreakAfter);
comments.add(comment);
}
private boolean containsNewLine(int from, int to) {
for (char c : getRawCharacters(from, to)) {
if (c == '\n' || c == '\r') {
return true;
private StartConnection determineStartConnection(int from, int to) {
if (from == to) {
return StartConnection.DIRECT_AFTER_PREVIOUS;
}
char[] between = getRawCharacters(from, to);
if (isNewLine(between[between.length - 1])) {
return StartConnection.START_OF_LINE;
}
for (char c : between) {
if (isNewLine(c)) {
return StartConnection.ON_NEXT_LINE;
}
}
return false;
return StartConnection.AFTER_PREVIOUS;
}
private boolean isNewLine(char c) {
return c == '\n' || c == '\r';
}
}
@@ -101,8 +101,8 @@ public class CommentPreservingParser {
static class Comments {
List<Comment> comments = List.nil();
void add(int prevEndPos, int pos, int endPos, String content, boolean newLine) {
comments = comments.append(new Comment(prevEndPos, pos, endPos, content, newLine));
void add(Comment comment) {
comments = comments.append(comment);
}
}
@@ -38,6 +38,8 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import lombok.delombok.Comment.StartConnection;
import com.sun.source.tree.Tree;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Symbol;
@@ -154,6 +156,7 @@ public class PrettyCommentsPrinter extends JCTree.Visitor {
private List<Comment> comments;
private final JCCompilationUnit cu;
private boolean newLine = true;
private boolean indent = false;
private boolean inParams = false;
public PrettyCommentsPrinter(Writer out, JCCompilationUnit cu, List<Comment> comments) {
@@ -168,13 +171,16 @@ public class PrettyCommentsPrinter extends JCTree.Visitor {
private void consumeComments(int till) throws IOException {
boolean shouldIndent = !newLine;
while (comments.nonEmpty() && comments.head.pos < till) {
if (newLine) {
align();
}
print(comments.head.content);
println();
boolean found = false;
Comment head = comments.head;
while (comments.nonEmpty() && head.pos < till) {
found = true;
printComment(head);
comments = comments.tail;
head = comments.head;
}
if (found && !newLine && !indent) {
print(" ");
}
if (newLine && shouldIndent) {
align();
@@ -186,18 +192,7 @@ public class PrettyCommentsPrinter extends JCTree.Visitor {
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);
printComment(head);
comments = comments.tail;
head = comments.head;
}
@@ -206,6 +201,39 @@ public class PrettyCommentsPrinter extends JCTree.Visitor {
}
}
private void printComment(Comment comment) throws IOException {
prepareComment(comment.start);
print(comment.content);
if (comment.newLineAfter) {
println();
}
}
private void prepareComment(StartConnection start) throws IOException {
switch (start) {
case DIRECT_AFTER_PREVIOUS:
break;
case AFTER_PREVIOUS:
if (!newLine && !indent) {
print(" ");
}
break;
case START_OF_LINE:
if (!newLine) {
println();
}
break;
case ON_NEXT_LINE:
if (!newLine && !indent) {
println();
}
if (!indent) {
align();
}
break;
}
}
/** The output stream on which trees are printed.
*/
Writer out;
@@ -227,6 +255,7 @@ public class PrettyCommentsPrinter extends JCTree.Visitor {
*/
void align() throws IOException {
newLine = false;
indent = true;
for (int i = 0; i < lmargin; i++) out.write("\t");
}
@@ -264,6 +293,7 @@ public class PrettyCommentsPrinter extends JCTree.Visitor {
*/
public void print(Object s) throws IOException {
newLine = false;
indent = false;
out.write(Convert.escapeUnicode(s.toString()));
}
@@ -271,6 +301,7 @@ public class PrettyCommentsPrinter extends JCTree.Visitor {
*/
public void println() throws IOException {
newLine = true;
indent = false;
out.write(lineSep);
}
@@ -306,7 +337,7 @@ public class PrettyCommentsPrinter extends JCTree.Visitor {
tree.accept(this);
int endPos = endPos(tree);
consumeTrailingComments(endPos);
consumeComments(endPos);
// consumeComments(endPos);
}
} catch (UncheckedIOException ex) {
IOException e = new IOException(ex.getMessage());
+1 -2
View File
@@ -1,7 +1,6 @@
import java.util.*;
public class Cast {
public void test(List<?> list) {
RandomAccess r = (/*before*/
RandomAccess /*after*/)list;
RandomAccess r = (/*before*/ RandomAccess /*after*/)list;
}
}
@@ -1,4 +1,5 @@
// Cool Comments
public class WithComments {
// Also inside the body
// On start of line
}
@@ -1,4 +1,5 @@
// Cool Comments
public class WithComments {
// Also inside the body
// On start of line
}
@@ -1,13 +1,9 @@
/* cmt */
/* cmt2 */
/* cmt3 */
/*bla */
/* cmt *//* cmt2 */ /* cmt3 */ /*bla */
public class CommentsInterspersed {
/** javadoc for field */
private int x;
/* bla2 */
private String test = "foo"; //$NON-NLS-1$
/* bla2 */ private String test = "foo"; //$NON-NLS-1$
/** Javadoc on method */
public native void gwtTest(); /*-{