Search in sources :

Example 1 with IASTFragment

use of org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IASTFragment in project eclipse.jdt.ls by eclipse.

the class ExtractConstantRefactoring method getFragmentsToReplace.

private IASTFragment[] getFragmentsToReplace() throws JavaModelException {
    List<IASTFragment> toReplace = new ArrayList<>();
    if (fReplaceAllOccurrences) {
        Iterator<ASTNode> replacementScope = getReplacementScope();
        while (replacementScope.hasNext()) {
            ASTNode scope = replacementScope.next();
            IASTFragment[] allMatches = ASTFragmentFactory.createFragmentForFullSubtree(scope).getSubFragmentsMatching(getSelectedExpression());
            IASTFragment[] replaceableMatches = retainOnlyReplacableMatches(allMatches);
            for (int i = 0; i < replaceableMatches.length; i++) {
                toReplace.add(replaceableMatches[i]);
            }
        }
    } else if (canReplace(getSelectedExpression())) {
        toReplace.add(getSelectedExpression());
    }
    return toReplace.toArray(new IASTFragment[toReplace.size()]);
}
Also used : IASTFragment(org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IASTFragment) ArrayList(java.util.ArrayList) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 2 with IASTFragment

use of org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IASTFragment in project eclipse.jdt.ls 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<>();
    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.ls.core.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 3 with IASTFragment

use of org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IASTFragment in project eclipse.jdt.ls 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.ls.core.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 4 with IASTFragment

use of org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IASTFragment in project eclipse.jdt.ls by eclipse.

the class ExtractTempRefactoring method getSelectedExpression.

private IExpressionFragment getSelectedExpression() throws JavaModelException {
    if (fSelectedExpression != null) {
        return fSelectedExpression;
    }
    IASTFragment selectedFragment = ASTFragmentFactory.createFragmentForSourceRange(new SourceRange(fSelectionStart, fSelectionLength), fCompilationUnitNode, fCu);
    if (selectedFragment instanceof IExpressionFragment && !Checks.isInsideJavadoc(selectedFragment.getAssociatedNode())) {
        fSelectedExpression = (IExpressionFragment) selectedFragment;
    } else if (selectedFragment != null) {
        if (selectedFragment.getAssociatedNode() instanceof ExpressionStatement) {
            ExpressionStatement exprStatement = (ExpressionStatement) selectedFragment.getAssociatedNode();
            Expression expression = exprStatement.getExpression();
            fSelectedExpression = (IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(expression);
        } else if (selectedFragment.getAssociatedNode() instanceof Assignment) {
            Assignment assignment = (Assignment) selectedFragment.getAssociatedNode();
            fSelectedExpression = (IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(assignment);
        }
    }
    if (fSelectedExpression != null && Checks.isEnumCase(fSelectedExpression.getAssociatedExpression().getParent())) {
        fSelectedExpression = null;
    }
    return fSelectedExpression;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) IASTFragment(org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IASTFragment) IExpressionFragment(org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IExpressionFragment) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) SourceRange(org.eclipse.jdt.core.SourceRange)

Example 5 with IASTFragment

use of org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IASTFragment in project eclipse.jdt.ls by eclipse.

the class ExtractTempRefactoring method createAndInsertTempDeclaration.

private void createAndInsertTempDeclaration() throws CoreException {
    Expression initializer = getSelectedExpression().createCopyTarget(fCURewrite.getASTRewrite(), true);
    VariableDeclarationStatement vds = createTempDeclaration(initializer);
    boolean insertAtSelection;
    if (!fReplaceAllOccurrences) {
        insertAtSelection = true;
    } else {
        IASTFragment[] replacableMatches = retainOnlyReplacableMatches(getMatchingFragments());
        insertAtSelection = replacableMatches.length == 0 || replacableMatches.length == 1 && replacableMatches[0].getAssociatedNode().equals(getSelectedExpression().getAssociatedExpression());
    }
    if (insertAtSelection) {
        insertAt(getSelectedExpression().getAssociatedNode(), vds);
        return;
    }
    ASTNode[] firstReplaceNodeParents = getParents(getFirstReplacedExpression().getAssociatedNode());
    ASTNode[] commonPath = findDeepestCommonSuperNodePathForReplacedNodes();
    Assert.isTrue(commonPath.length <= firstReplaceNodeParents.length);
    ASTNode deepestCommonParent = firstReplaceNodeParents[commonPath.length - 1];
    if (deepestCommonParent instanceof Block) {
        insertAt(firstReplaceNodeParents[commonPath.length], vds);
    } else {
        insertAt(deepestCommonParent, vds);
    }
}
Also used : IASTFragment(org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IASTFragment) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) Block(org.eclipse.jdt.core.dom.Block)

Aggregations

IASTFragment (org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IASTFragment)6 ASTNode (org.eclipse.jdt.core.dom.ASTNode)4 CastExpression (org.eclipse.jdt.core.dom.CastExpression)2 Expression (org.eclipse.jdt.core.dom.Expression)2 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)2 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)2 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)2 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)2 SimpleName (org.eclipse.jdt.core.dom.SimpleName)2 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)2 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)2 TextEditGroup (org.eclipse.text.edits.TextEditGroup)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 SourceRange (org.eclipse.jdt.core.SourceRange)1 AST (org.eclipse.jdt.core.dom.AST)1 Assignment (org.eclipse.jdt.core.dom.Assignment)1 Block (org.eclipse.jdt.core.dom.Block)1 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)1 Name (org.eclipse.jdt.core.dom.Name)1