Search in sources :

Example 21 with TextEditGroup

use of org.eclipse.text.edits.TextEditGroup in project che by eclipse.

the class InlineTempRefactoring method removeTemp.

private void removeTemp(CompilationUnitRewrite cuRewrite) {
    VariableDeclaration variableDeclaration = getVariableDeclaration();
    TextEditGroup groupDesc = cuRewrite.createGroupDescription(RefactoringCoreMessages.InlineTempRefactoring_remove_edit_name);
    ASTNode parent = variableDeclaration.getParent();
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    TightSourceRangeComputer sourceRangeComputer = new TightSourceRangeComputer();
    rewrite.setTargetSourceRangeComputer(sourceRangeComputer);
    if (parent instanceof VariableDeclarationStatement && ((VariableDeclarationStatement) parent).fragments().size() == 1) {
        sourceRangeComputer.addTightSourceNode(parent);
        rewrite.remove(parent, groupDesc);
    } else {
        sourceRangeComputer.addTightSourceNode(variableDeclaration);
        rewrite.remove(variableDeclaration, groupDesc);
    }
}
Also used : TightSourceRangeComputer(org.eclipse.jdt.internal.corext.refactoring.util.TightSourceRangeComputer) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 22 with TextEditGroup

use of org.eclipse.text.edits.TextEditGroup in project che by eclipse.

the class ExtractTempRefactoring method insertAt.

private void insertAt(ASTNode target, Statement declaration) {
    ASTRewrite rewrite = fCURewrite.getASTRewrite();
    TextEditGroup groupDescription = fCURewrite.createGroupDescription(RefactoringCoreMessages.ExtractTempRefactoring_declare_local_variable);
    ASTNode parent = target.getParent();
    StructuralPropertyDescriptor locationInParent = target.getLocationInParent();
    while (locationInParent != Block.STATEMENTS_PROPERTY && locationInParent != SwitchStatement.STATEMENTS_PROPERTY) {
        if (locationInParent == IfStatement.THEN_STATEMENT_PROPERTY || locationInParent == IfStatement.ELSE_STATEMENT_PROPERTY || locationInParent == ForStatement.BODY_PROPERTY || locationInParent == EnhancedForStatement.BODY_PROPERTY || locationInParent == DoStatement.BODY_PROPERTY || locationInParent == WhileStatement.BODY_PROPERTY) {
            // create intermediate block if target was the body property of a control statement:
            Block replacement = rewrite.getAST().newBlock();
            ListRewrite replacementRewrite = rewrite.getListRewrite(replacement, Block.STATEMENTS_PROPERTY);
            replacementRewrite.insertFirst(declaration, null);
            replacementRewrite.insertLast(rewrite.createMoveTarget(target), null);
            rewrite.replace(target, replacement, groupDescription);
            return;
        }
        target = parent;
        parent = parent.getParent();
        locationInParent = target.getLocationInParent();
    }
    ListRewrite listRewrite = rewrite.getListRewrite(parent, (ChildListPropertyDescriptor) locationInParent);
    listRewrite.insertBefore(declaration, target, groupDescription);
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)

Example 23 with TextEditGroup

use of org.eclipse.text.edits.TextEditGroup in project che by eclipse.

the class ExtractTempRefactoring method addReplaceExpressionWithTemp.

private void addReplaceExpressionWithTemp() throws JavaModelException {
    IASTFragment[] fragmentsToReplace = retainOnlyReplacableMatches(getMatchingFragments());
    //TODO: should not have to prune duplicates here...
    ASTRewrite rewrite = fCURewrite.getASTRewrite();
    HashSet<IASTFragment> seen = new HashSet<IASTFragment>();
    for (int i = 0; i < fragmentsToReplace.length; i++) {
        IASTFragment fragment = fragmentsToReplace[i];
        if (!seen.add(fragment))
            continue;
        SimpleName tempName = fCURewrite.getAST().newSimpleName(fTempName);
        TextEditGroup description = fCURewrite.createGroupDescription(RefactoringCoreMessages.ExtractTempRefactoring_replace);
        fragment.replace(rewrite, tempName, description);
        if (fLinkedProposalModel != null)
            fLinkedProposalModel.getPositionGroup(KEY_NAME, true).addPosition(rewrite.track(tempName), false);
    }
}
Also used : IASTFragment(org.eclipse.jdt.internal.corext.dom.fragments.IASTFragment) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup) HashSet(java.util.HashSet)

Example 24 with TextEditGroup

use of org.eclipse.text.edits.TextEditGroup in project che by eclipse.

the class ExtractConstantRefactoring method replaceExpressionsWithConstant.

private void replaceExpressionsWithConstant() throws JavaModelException {
    ASTRewrite astRewrite = fCuRewrite.getASTRewrite();
    AST ast = astRewrite.getAST();
    IASTFragment[] fragmentsToReplace = getFragmentsToReplace();
    for (int i = 0; i < fragmentsToReplace.length; i++) {
        IASTFragment fragment = fragmentsToReplace[i];
        ASTNode node = fragment.getAssociatedNode();
        boolean inTypeDeclarationAnnotation = isInTypeDeclarationAnnotation(node);
        if (inTypeDeclarationAnnotation && JdtFlags.VISIBILITY_STRING_PRIVATE == getVisibility())
            continue;
        SimpleName ref = ast.newSimpleName(fConstantName);
        Name replacement = ref;
        boolean qualifyReference = qualifyReferencesWithDeclaringClassName();
        if (!qualifyReference) {
            qualifyReference = inTypeDeclarationAnnotation;
        }
        if (qualifyReference) {
            replacement = ast.newQualifiedName(ast.newSimpleName(getContainingTypeBinding().getName()), ref);
        }
        TextEditGroup description = fCuRewrite.createGroupDescription(RefactoringCoreMessages.ExtractConstantRefactoring_replace);
        fragment.replace(astRewrite, replacement, description);
        if (fLinkedProposalModel != null)
            fLinkedProposalModel.getPositionGroup(KEY_NAME, true).addPosition(astRewrite.track(ref), false);
    }
}
Also used : AST(org.eclipse.jdt.core.dom.AST) IASTFragment(org.eclipse.jdt.internal.corext.dom.fragments.IASTFragment) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name)

Example 25 with TextEditGroup

use of org.eclipse.text.edits.TextEditGroup in project che by eclipse.

the class AbstractSerialVersionOperation method rewriteAST.

/**
	 * {@inheritDoc}
	 */
@Override
public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel positionGroups) throws CoreException {
    final ASTRewrite rewrite = cuRewrite.getASTRewrite();
    VariableDeclarationFragment fragment = null;
    for (int i = 0; i < fNodes.length; i++) {
        final ASTNode node = fNodes[i];
        final AST ast = node.getAST();
        fragment = ast.newVariableDeclarationFragment();
        fragment.setName(ast.newSimpleName(NAME_FIELD));
        final FieldDeclaration declaration = ast.newFieldDeclaration(fragment);
        declaration.setType(ast.newPrimitiveType(PrimitiveType.LONG));
        declaration.modifiers().addAll(ASTNodeFactory.newModifiers(ast, Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL));
        if (!addInitializer(fragment, node))
            continue;
        if (fragment.getInitializer() != null) {
            final TextEditGroup editGroup = createTextEditGroup(FixMessages.SerialVersion_group_description, cuRewrite);
            if (node instanceof AbstractTypeDeclaration)
                rewrite.getListRewrite(node, ((AbstractTypeDeclaration) node).getBodyDeclarationsProperty()).insertAt(declaration, 0, editGroup);
            else if (node instanceof AnonymousClassDeclaration)
                rewrite.getListRewrite(node, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY).insertAt(declaration, 0, editGroup);
            else if (node instanceof ParameterizedType) {
                final ParameterizedType type = (ParameterizedType) node;
                final ASTNode parent = type.getParent();
                if (parent instanceof ClassInstanceCreation) {
                    final ClassInstanceCreation creation = (ClassInstanceCreation) parent;
                    final AnonymousClassDeclaration anonymous = creation.getAnonymousClassDeclaration();
                    if (anonymous != null)
                        rewrite.getListRewrite(anonymous, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY).insertAt(declaration, 0, editGroup);
                }
            } else
                Assert.isTrue(false);
            addLinkedPositions(rewrite, fragment, positionGroups);
        }
        final String comment = CodeGeneration.getFieldComment(fUnit, declaration.getType().toString(), NAME_FIELD, StubUtility.getLineDelimiterUsed(fUnit));
        if (comment != null && comment.length() > 0) {
            final Javadoc doc = (Javadoc) rewrite.createStringPlaceholder(comment, ASTNode.JAVADOC);
            declaration.setJavadoc(doc);
        }
    }
    if (fragment == null)
        return;
    positionGroups.setEndPosition(rewrite.track(fragment));
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) AST(org.eclipse.jdt.core.dom.AST) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) Javadoc(org.eclipse.jdt.core.dom.Javadoc) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Aggregations

TextEditGroup (org.eclipse.text.edits.TextEditGroup)34 ASTNode (org.eclipse.jdt.core.dom.ASTNode)12 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)12 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)11 TextEdit (org.eclipse.text.edits.TextEdit)10 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)6 AST (org.eclipse.jdt.core.dom.AST)6 CategorizedTextEditGroup (org.eclipse.ltk.core.refactoring.CategorizedTextEditGroup)6 SimpleName (org.eclipse.jdt.core.dom.SimpleName)5 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)4 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)4 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)4 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)4 CompilationUnitChange (org.eclipse.jdt.core.refactoring.CompilationUnitChange)4 ArrayList (java.util.ArrayList)3 IFile (org.eclipse.core.resources.IFile)3 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)3 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)3 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)3 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)3