Hello, I'm parsing a Java file and creating the same Java file with same changes. However, the LOC (line of code) is changing.
For instance the following statement:
/** length of the initial token (content-)buffer */
private static final int INITIAL_TOKEN_LENGTH = 50;
Will be parsed and changed to:
/**
* length of the initial token (content-)buffer
*/
private static final int INITIAL_TOKEN_LENGTH = 50;
Please, note that now, JavaParse transformed the comment into 3 LOC. Is it possible to force it to not perform this and keep the comment into one LOC?
thanks
Hi! What you're seeing is the pretty printer. If you want to keep the same formatting, try the lexical preserving printer. There's Javadoc for it, and you can check out the book.
Many thanks for your response. However, I need to change some nodes .. that is why I have used JavaParser. Do you think lexical preserving printer can still be used in this case? Thanks in advance.
Yes, that is its task :-) Give the book a read - it's short and you can get it for free if you want.
Hello guys,
I've tried this code:
Pair<ParseResult<CompilationUnit>, LexicalPreservingPrinter> result =
LexicalPreservingPrinter.setup(ParseStart.COMPILATION_UNIT,
new StringProvider(compilationUnit.toString()));
CompilationUnit cuNew = result.a.getResult().get();
LexicalPreservingPrinter lpp = result.b;
System.out.println(lpp.print(cuNew));
The instance named compilationUnit is the guy that I have changed. However, after using LexicalPreservingPrinter using the print method space and comments are formatted in a different way from the original compilatioUnit. Any clue how to fix this?
Thanks in advance.
Hey @rdurelli - open a new issue with details on what you got and what you expected.