You didn’t receive my reply, did you?
 
Otherwise, I wrote this:
“””
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: Dienstag, 18. Februar 2020 00:48
To: cdt-dev@xxxxxxxxxxx
Subject: Re: [cdt-dev] how to add a statement into an empty .CPP file
 
 
 
really hope someone can give a detail guide.
 
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());
 
    }