Skip to content

Commit

Permalink
Better integrate lexical preservation, and use it for the code genera…
Browse files Browse the repository at this point in the history
…tors
  • Loading branch information
matozoid committed Sep 23, 2017
1 parent eece549 commit dd0f96d
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 152 deletions.
@@ -1,9 +1,10 @@
package com.github.javaparser.generator.core; package com.github.javaparser.generator.core;


import com.github.javaparser.JavaParser;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.generator.core.node.*; import com.github.javaparser.generator.core.node.*;
import com.github.javaparser.generator.core.visitor.*; import com.github.javaparser.generator.core.visitor.*;
import com.github.javaparser.printer.PrettyPrinter; import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter;
import com.github.javaparser.printer.PrettyPrinterConfiguration;
import com.github.javaparser.utils.SourceRoot; import com.github.javaparser.utils.SourceRoot;


import java.nio.file.Path; import java.nio.file.Path;
Expand All @@ -18,8 +19,13 @@ public static void main(String[] args) throws Exception {
throw new RuntimeException("Need 1 parameter: the JavaParser source checkout root directory."); throw new RuntimeException("Need 1 parameter: the JavaParser source checkout root directory.");
} }
final Path root = Paths.get(args[0], "..", "javaparser-core", "src", "main", "java"); final Path root = Paths.get(args[0], "..", "javaparser-core", "src", "main", "java");
final SourceRoot sourceRoot = new SourceRoot(root); final SourceRoot sourceRoot = new SourceRoot(root)
sourceRoot.setPrinter(new PrettyPrinter(new PrettyPrinterConfiguration().setEndOfLineCharacter("\n"))::print); .setPrinter(LexicalPreservingPrinter::print)
.setJavaParser(new JavaParser(
new ParserConfiguration()
.setStoreTokens(false)
.setAttributeComments(false)
.setLexicalPreservationEnabled(true)));


new CoreGenerator().run(sourceRoot); new CoreGenerator().run(sourceRoot);


Expand Down
Expand Up @@ -39,6 +39,7 @@
import com.github.javaparser.ast.type.Type; import com.github.javaparser.ast.type.Type;
import com.github.javaparser.ast.validator.ProblemReporter; import com.github.javaparser.ast.validator.ProblemReporter;
import com.github.javaparser.javadoc.Javadoc; import com.github.javaparser.javadoc.Javadoc;
import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter;


import java.io.*; import java.io.*;
import java.nio.charset.Charset; import java.nio.charset.Charset;
Expand Down Expand Up @@ -135,6 +136,9 @@ public <N extends Node> ParseResult<N> parse(ParseStart<N> start, Provider provi
final CommentsCollection comments = parser.getCommentsCollection(); final CommentsCollection comments = parser.getCommentsCollection();
commentsInserter.insertComments(resultNode, comments.copy().getComments()); commentsInserter.insertComments(resultNode, comments.copy().getComments());
} }
if(configuration.isLexicalPreservationEnabled()){
LexicalPreservingPrinter.setup(resultNode);
}


configuration.getValidator().accept(resultNode, new ProblemReporter(parser.problems)); configuration.getValidator().accept(resultNode, new ProblemReporter(parser.problems));
parser.problems.sort(PROBLEM_BY_BEGIN_POSITION); parser.problems.sort(PROBLEM_BY_BEGIN_POSITION);
Expand Down
Expand Up @@ -37,6 +37,7 @@ public class ParserConfiguration {
private boolean attributeComments = true; private boolean attributeComments = true;
private boolean doNotAssignCommentsPrecedingEmptyLines = true; private boolean doNotAssignCommentsPrecedingEmptyLines = true;
private boolean doNotConsiderAnnotationsAsNodeStartForCodeAttribution = false; private boolean doNotConsiderAnnotationsAsNodeStartForCodeAttribution = false;
private boolean lexicalPreservationEnabled = false;
private int tabSize = 1; private int tabSize = 1;
private Validator validator = new Java8Validator(); private Validator validator = new Java8Validator();


Expand Down Expand Up @@ -106,4 +107,18 @@ public ParserConfiguration setValidator(Validator validator) {
this.validator = validator; this.validator = validator;
return this; return this;
} }

/**
* Disabled by default.
* When this is enabled, LexicalPreservingPrinter.print can be used to reproduce
* the original formatting of the file.
*/
public ParserConfiguration setLexicalPreservationEnabled(boolean lexicalPreservationEnabled) {
this.lexicalPreservationEnabled = lexicalPreservationEnabled;
return this;
}

public boolean isLexicalPreservationEnabled() {
return lexicalPreservationEnabled;
}
} }
Expand Up @@ -86,18 +86,29 @@ public static <N extends Node> Pair<ParseResult<N>, LexicalPreservingPrinter> se
return new Pair<>(parseResult, lexicalPreservingPrinter); return new Pair<>(parseResult, lexicalPreservingPrinter);
} }


public static void setup(Node node) { /**
* Prepares the node so it can be used in the print methods.
* The correct order is:
* <ol>
* <li>Parse some code</li>
* <li>Call this setup method on the result</li>
* <li>Make changes to the AST as desired</li>
* <li>Use one of the print methods on this class to print out the original source code with your changes added</li>
* </ol>
* @return the node passed as a parameter for your convenience.
*/
public static <N extends Node> N setup(N node) {
assertNotNull(node); assertNotNull(node);


node.getTokenRange().ifPresent(r -> { node.getTokenRange().ifPresent(r -> {
// Store initial text
storeInitialText(node); storeInitialText(node);


// Setup observer // Setup observer
AstObserver observer = createObserver(); AstObserver observer = createObserver();


node.registerForSubtree(observer); node.registerForSubtree(observer);
}); });
return node;
} }


// //
Expand Down
Expand Up @@ -21,13 +21,10 @@


package com.github.javaparser.printer.lexicalpreservation; package com.github.javaparser.printer.lexicalpreservation;


import com.github.javaparser.ParseResult; import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseStart;
import com.github.javaparser.Providers;
import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node; import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.expr.Expression; import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.utils.Pair;
import org.junit.Before; import org.junit.Before;


import java.io.IOException; import java.io.IOException;
Expand All @@ -37,28 +34,20 @@


public abstract class AbstractLexicalPreservingTest { public abstract class AbstractLexicalPreservingTest {


protected LexicalPreservingPrinter lpp;
protected CompilationUnit cu; protected CompilationUnit cu;
protected Expression expression; protected Expression expression;


@Before @Before
public void setup() { public void setup() {
lpp = null;
cu = null; cu = null;
} }


protected void considerCode(String code) { protected void considerCode(String code) {
Pair<ParseResult<CompilationUnit>, LexicalPreservingPrinter> res = LexicalPreservingPrinter.setup( cu = LexicalPreservingPrinter.setup(JavaParser.parse(code));
ParseStart.COMPILATION_UNIT, Providers.provider(code));
cu = res.a.getResult().get();
lpp = res.b;
} }


protected void considerExpression(String code) { protected void considerExpression(String code) {
Pair<ParseResult<Expression>, LexicalPreservingPrinter> res = LexicalPreservingPrinter.setup( expression = LexicalPreservingPrinter.setup(JavaParser.parseExpression(code));
ParseStart.EXPRESSION, Providers.provider(code));
expression = res.a.getResult().get();
lpp = res.b;
} }


protected String considerExample(String resourceName) throws IOException { protected String considerExample(String resourceName) throws IOException {
Expand All @@ -73,17 +62,17 @@ protected String readExample(String resourceName) throws IOException {


protected void assertTransformed(String exampleName, Node node) throws IOException { protected void assertTransformed(String exampleName, Node node) throws IOException {
String expectedCode = readExample(exampleName + "_expected"); String expectedCode = readExample(exampleName + "_expected");
String actualCode = lpp.print(node); String actualCode = LexicalPreservingPrinter.print(node);
assertEquals(expectedCode, actualCode); assertEquals(expectedCode, actualCode);
} }


protected void assertUnchanged(String exampleName) throws IOException { protected void assertUnchanged(String exampleName) throws IOException {
String code = considerExample(exampleName + "_original"); String code = considerExample(exampleName + "_original");
assertEquals(code, lpp.print(cu != null ? cu : expression)); assertEquals(code, LexicalPreservingPrinter.print(cu != null ? cu : expression));
} }


protected void assertTransformedToString(String expectedPartialCode, Node node) { protected void assertTransformedToString(String expectedPartialCode, Node node) {
String actualCode = lpp.print(node); String actualCode = LexicalPreservingPrinter.print(node);
assertEquals(expectedPartialCode, actualCode); assertEquals(expectedPartialCode, actualCode);
} }


Expand Down

0 comments on commit dd0f96d

Please sign in to comment.