diff options
-rw-r--r-- | src/delombok/lombok/delombok/CommentPreservingParser.java | 30 | ||||
-rw-r--r-- | src/delombok/lombok/delombok/PrettyCommentsPrinter.java | 10 | ||||
-rw-r--r-- | test/delombok/resource/after/ForLoop.java | 13 | ||||
-rw-r--r-- | test/delombok/resource/after/WithComments.java | 2 | ||||
-rw-r--r-- | test/delombok/resource/before/ForLoop.java | 12 | ||||
-rw-r--r-- | test/delombok/src/lombok/delombok/TestSourceFiles.java | 11 |
6 files changed, 54 insertions, 24 deletions
diff --git a/src/delombok/lombok/delombok/CommentPreservingParser.java b/src/delombok/lombok/delombok/CommentPreservingParser.java index c85124b5..19219d93 100644 --- a/src/delombok/lombok/delombok/CommentPreservingParser.java +++ b/src/delombok/lombok/delombok/CommentPreservingParser.java @@ -25,29 +25,43 @@ import java.io.IOException; import java.io.Writer; import com.sun.tools.javac.main.JavaCompiler; +import com.sun.tools.javac.main.OptionName; import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; import com.sun.tools.javac.util.Context; import com.sun.tools.javac.util.List; +import com.sun.tools.javac.util.Options; public class CommentPreservingParser { - private final JavaCompiler compiler; - private final Context context; - public CommentPreservingParser(Context context) { - this.context = context; + private final String encoding; + + public CommentPreservingParser() { + this("utf-8"); + } + + public CommentPreservingParser(String encoding) { + this.encoding = encoding; + } + + public ParseResult parseFile(String fileName) throws IOException { + Context context = new Context(); + + Options.instance(context).put(OptionName.ENCODING, encoding); + CommentCollectingScanner.Factory.preRegister(context); - compiler = new JavaCompiler(context) { + + JavaCompiler compiler = new JavaCompiler(context) { @Override protected boolean keepComments() { return true; } }; compiler.genEndPos = true; - } - - public ParseResult parseFile(String fileName) throws IOException { + Comments comments = new Comments(); context.put(Comments.class, comments); + + comments.comments = List.nil(); @SuppressWarnings("deprecation") JCCompilationUnit cu = compiler.parse(fileName); return new ParseResult(comments.comments, cu); diff --git a/src/delombok/lombok/delombok/PrettyCommentsPrinter.java b/src/delombok/lombok/delombok/PrettyCommentsPrinter.java index c6b58a31..2fb62e53 100644 --- a/src/delombok/lombok/delombok/PrettyCommentsPrinter.java +++ b/src/delombok/lombok/delombok/PrettyCommentsPrinter.java @@ -184,10 +184,6 @@ public class PrettyCommentsPrinter extends JCTree.Visitor { */ Writer out; - /** Indentation width (can be reassigned from outside). - */ - public int width = 4; - /** The current left margin. */ int lmargin = 0; @@ -205,19 +201,19 @@ public class PrettyCommentsPrinter extends JCTree.Visitor { */ void align() throws IOException { newLine = false; - for (int i = 0; i < lmargin; i++) out.write(" "); + for (int i = 0; i < lmargin; i++) out.write("\t"); } /** Increase left margin by indentation width. */ void indent() { - lmargin = lmargin + width; + lmargin++; } /** Decrease left margin by indentation width. */ void undent() { - lmargin = lmargin - width; + lmargin--; } /** Enter a new precedence level. Emit a `(' if new precedence level diff --git a/test/delombok/resource/after/ForLoop.java b/test/delombok/resource/after/ForLoop.java new file mode 100644 index 00000000..609549b7 --- /dev/null +++ b/test/delombok/resource/after/ForLoop.java @@ -0,0 +1,13 @@ + +public class ForLoop { + + public static void main(String[] args) { + // before loop + for (int i = 0; i < 10; i++) { + // start of block + System.out.println(i); + // end of block + } + // after loop + } +} diff --git a/test/delombok/resource/after/WithComments.java b/test/delombok/resource/after/WithComments.java index 0f9ddb9f..59cc97c4 100644 --- a/test/delombok/resource/after/WithComments.java +++ b/test/delombok/resource/after/WithComments.java @@ -1,5 +1,5 @@ // Cool Comments public class WithComments { - // Also inside the body + // Also inside the body }
\ No newline at end of file diff --git a/test/delombok/resource/before/ForLoop.java b/test/delombok/resource/before/ForLoop.java new file mode 100644 index 00000000..2bed7f9b --- /dev/null +++ b/test/delombok/resource/before/ForLoop.java @@ -0,0 +1,12 @@ +public class ForLoop { + + public static void main(String[] args) { + // before loop + for (int i = 0; i < 10; i++) { + // start of block + System.out.println(i); + // end of block + } + // after loop + } +} diff --git a/test/delombok/src/lombok/delombok/TestSourceFiles.java b/test/delombok/src/lombok/delombok/TestSourceFiles.java index 90c9b543..374eeb8b 100644 --- a/test/delombok/src/lombok/delombok/TestSourceFiles.java +++ b/test/delombok/src/lombok/delombok/TestSourceFiles.java @@ -21,6 +21,8 @@ */ package lombok.delombok; +import static org.junit.Assert.fail; + import java.io.BufferedReader; import java.io.File; import java.io.FileReader; @@ -31,11 +33,6 @@ import lombok.delombok.CommentPreservingParser.ParseResult; import org.junit.BeforeClass; import org.junit.Test; -import static org.junit.Assert.*; - -import com.sun.tools.javac.main.OptionName; -import com.sun.tools.javac.util.Context; -import com.sun.tools.javac.util.Options; public class TestSourceFiles { @@ -48,9 +45,7 @@ public class TestSourceFiles { @BeforeClass public static void init() { - Context c = new Context(); - Options.instance(c).put(OptionName.ENCODING, "utf-8"); - parser = new CommentPreservingParser(c); + parser = new CommentPreservingParser(); } @Test |