[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [cdt-dev] Two Phase Refactoring
|
Hi Krishna,
Basically, if you are working in the AST level, i.e., without exposing any
UI, Sergey's suggestion should work. This is what I am working too at the
moment.
Have a look in this minimal (toy) example. I hope it helps you.
protected void addNewFunction(String name){
try {
//find node of interest
IASTNode node = ASTUtilities.findNodeInTree(mainAST, 1,
Class.forName("org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator"),
"getName", "test");
if (node != null && node instanceof IASTFunctionDeclarator){
//get the rewriter
ASTRewrite rewriter = ASTRewrite.create(mainAST);
//get the factory
ICPPNodeFactory nodeFactory =
(ICPPNodeFactory)mainAST.getASTNodeFactory();
//create a new ASTFunctionDefinition
IASTNode n = nodeFactory.newFunctionDefinition(
((IASTFunctionDefinition)node.getParent()).getDeclSpecifier().copy(CopyStyle.withoutLocations),
nodeFactory.newFunctionDeclarator(nodeFactory.newName(name)),
((IASTFunctionDefinition)node.getParent()).getBody().copy(CopyStyle.withoutLocations)
);
//specify its position in the ast
rewriter.insertBefore(node.getParent(),
node.getParent().getChildren()[0], n, null);
//ask the rewriter to suggest a change
Change c = rewriter.rewriteAST();
//make the change
c.perform(new NullProgressMonitor());
}
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
--
View this message in context: http://eclipse.1072660.n5.nabble.com/Two-Phase-Refactoring-tp186513p186549.html
Sent from the Eclipse CDT - Development mailing list archive at Nabble.com.