Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Empty lines remove when using ASTParser
Empty lines remove when using ASTParser [message #1403445] Wed, 23 July 2014 17:21
Pedro Quintas is currently offline Pedro QuintasFriend
Messages: 1
Registered: July 2014
Junior Member
Hi guys,

I'm trying to use ASTParser to make a merge between two Java Files.
But when I parse the file, all empty line are deleted. I allready try to use the "DefaultCodeFormatterConstants.FORMATTER_NUMBER_OF_EMPTY_LINES_TO_PRESERVE" option, but no matter I do all the empty line in the Source code are deleted.

For example:

Original Code
-------------
...
if ( x == 2 ) {
System.out.print("2 ...");
}

if ( x == 3 ) {
System.out.print("3 ---");
}

...

Final Code After apply ASTParser
--------------------------------
...
if ( x == 2 ) {
System.out.print("2 ...");
}
if ( x == 3 ) {
System.out.print("3 ---");
}
...

Diff: Empty line between the first "if" and de second "if", and between the last "}" and the last "..."

This is the code that I'm using (+/-):

ASTParser astParser = ASTParser.newParser(AST.JLS3);
astParser.setSource(newJavaSource.toCharArray());
CompilationUnit cu = (CompilationUnit) astParser.createAST(null);
NewJavaFileVisitor newVisitor = new NewJavaFileVisitor();
cu.accept(newVisitor);
...

The "NewJavaFileVisitor" class extends ASTVisitor and has implemented the following visit methods:

public boolean visit(FieldDeclaration node)
public boolean visit(MethodDeclaration node)
public boolean visit(TypeDeclaration node)
public boolean visit(CompilationUnit node)

This class store the methods found in a Array "List<ASTNode> newNodes". The ASTNode that are store in this array, don't have the empty lines.


Any ideias how to solve my problem?

Thanks,
Kintas


Previous Topic:Have JDT compile the git index
Next Topic:Problem Closing objects from object dropdown
Goto Forum:
  


Current Time: Sat Jul 27 05:21:18 GMT 2024

Powered by FUDForum. Page generated in 0.03057 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top