mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-14 04:16:19 +00:00
Minor refactoring + added testcase for for-loop
This commit is contained in:
1 parent
6cd3b8a8ca
commit
f22021ca78
6 files changed
+54
-24
No files matched your search
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
// Cool Comments
|
||||
|
||||
public class WithComments {
|
||||
// Also inside the body
|
||||
// Also inside the body
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user