Hi Ming Cheng
You need to actually insert/replace/remove a node through the rewriter in order to see something happening. Just creating a new break statement with a factory will not be represented
anywhere. For example you could create a new declaration, through the node factory, and insert it at the beginning of the translation unit:
r.insertBefore(ast, null, newDeclaration, null);
You might be able to find other applications of the ASTRewrite infrastructure in the refactoring part of the UI plug-in. I hope this helps.
Thomas
From: cdt-dev-bounces@xxxxxxxxxxx <cdt-dev-bounces@xxxxxxxxxxx>
On Behalf Of Ming Cheng
Sent: Montag, 17. Februar 2020 08:38
To: cdt-dev@xxxxxxxxxxx
Subject: [cdt-dev] how to add a statement into an empty .CPP file
private void buildMainFunction(IFile file) {
IASTTranslationUnit ast = CurrentGui.getInstance().getGui().getAST(file);
ASTRewrite r = ASTRewrite.create( ast );
IASTBreakStatement breakStatement = ast.getASTNodeFactory().newBreakStatement();
Change c = r.rewriteAST();
c.perform(new NullProgressMonitor());
} catch (CoreException e) {
log.error(e.getMessage());
}
PS. file was created successfully. But after this buildMainFunction() call, the file is still empty. If I try ASTRewrite replace(), my program will complain org.umlet.plugin
null.
@Override
public IASTTranslationUnit getAST(IFile file) {
ITranslationUnit tu= (ITranslationUnit) CoreModel.getDefault().create(file);
IASTTranslationUnit ast = tu.getAST();
} catch (CoreException e) {
log.error(e.getMessage());
}