Example usage for org.eclipse.jdt.core.dom.rewrite ASTRewrite ASTRewrite

List of usage examples for org.eclipse.jdt.core.dom.rewrite ASTRewrite ASTRewrite

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom.rewrite ASTRewrite ASTRewrite.

Prototype

protected ASTRewrite(AST ast) 

Source Link

Document

Internal constructor.

Usage

From source file:org.eclipse.objectteams.otdt.internal.ui.assist.CompletionAdaptor.CreateMethodMappingCompletionProposal.java

License:Open Source License

/** Create a rewrite that additionally removes typed fragment if needed. 
      *  That fragment will not be represented by an AST-node, that could be removed.
      *///from   www  .ja v a  2  s.  co  m
    ASTRewrite createRewrite(AST ast) {
        if (fLength == 0)
            return ASTRewrite.create(ast);

        // the typed prefix will have to be deleted:
        final TextEdit delete = new DeleteEdit(fReplaceStart, fLength);

        // return a custom rewrite that additionally deletes typed fragment
        return new ASTRewrite(ast) {
            @Override
            public TextEdit rewriteAST() throws JavaModelException, IllegalArgumentException {
                TextEdit edits = super.rewriteAST();
                if (edits instanceof MultiTextEdit) {
                    MultiTextEdit multi = (MultiTextEdit) edits;
                    multi.addChild(delete);
                }
                return edits;
            }
        };
    }

From source file:org.eclipse.objectteams.otdt.internal.ui.assist.CompletionAdaptor.MethodMappingCompletionProposal.java

License:Open Source License

@Override
    protected ASTRewrite getRewrite() throws CoreException {
        ICompilationUnit iCU = getCompilationUnit();
        CompilationUnit unit = ASTResolving.createQuickFixAST(iCU, null);
        ImportRewrite importRewrite = createImportRewrite(unit);

        // find enclosing mapping and type:
        AbstractMethodMappingDeclaration partialMapping = null;
        ASTNode node = NodeFinder.perform(unit, fReplaceStart, 0);
        while (node != null && !(node instanceof AbstractTypeDeclaration)) {
            if (partialMapping == null && (node instanceof AbstractMethodMappingDeclaration))
                partialMapping = (AbstractMethodMappingDeclaration) node;
            node = node.getParent();/*from w  w w .  ja v  a  2 s.  c  o m*/
        }

        if (node != null) {
            AbstractTypeDeclaration declaration = ((AbstractTypeDeclaration) node);
            ChildListPropertyDescriptor bodyProperty = declaration.getBodyDeclarationsProperty();

            // find role and base type bindings:
            ITypeBinding roleBinding = declaration.resolveBinding();
            ITypeBinding baseBinding = null;
            if (roleBinding != null) {
                baseBinding = roleBinding.getBaseClass();
            } else if (declaration instanceof RoleTypeDeclaration) {
                baseBinding = ((RoleTypeDeclaration) declaration).getBaseClassType().resolveBinding();
            }
            if (baseBinding == null) {
                OTDTUIPlugin.getDefault().getLog().log(new Status(Status.ERROR,
                        "org.eclipse.objectteams.otdt.jdt.ui", "could not resolve type bindings")); //$NON-NLS-1$ //$NON-NLS-2$
                return null;
            }

            // create and setup the rewrite:
            ASTRewrite rewrite = createRewrite(unit.getAST());
            rewrite.setToOTJ();
            if (setupRewrite(iCU, rewrite, importRewrite, roleBinding, baseBinding, node, partialMapping,
                    bodyProperty))
                return rewrite;

            // rewriting was not successful, use the original replace string from the CompletionProposal:
            return new ASTRewrite(unit.getAST()) {
                @Override
                public TextEdit rewriteAST() {
                    return new ReplaceEdit(fReplaceStart, fLength, fReplacementString);
                }
            };
        }
        return null;
    }