Search in sources :

Example 6 with LinkedProposalPositionGroup

use of org.eclipse.jdt.ls.core.internal.corext.fix.LinkedProposalPositionGroup in project eclipse.jdt.ls by eclipse.

the class ExtractTempRefactoring method createTempDeclaration.

private VariableDeclarationStatement createTempDeclaration(Expression initializer) throws CoreException {
    AST ast = fCURewrite.getAST();
    VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment();
    vdf.setName(ast.newSimpleName(fTempName));
    vdf.setInitializer(initializer);
    VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf);
    if (fDeclareFinal) {
        vds.modifiers().add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD));
    }
    vds.setType(createTempType());
    if (fLinkedProposalModel != null) {
        ASTRewrite rewrite = fCURewrite.getASTRewrite();
        LinkedProposalPositionGroup nameGroup = fLinkedProposalModel.getPositionGroup(KEY_NAME, true);
        nameGroup.addPosition(rewrite.track(vdf.getName()), true);
        String[] nameSuggestions = guessTempNames();
        if (nameSuggestions.length > 0 && !nameSuggestions[0].equals(fTempName)) {
            nameGroup.addProposal(fTempName, nameSuggestions.length + 1);
        }
        for (int i = 0; i < nameSuggestions.length; i++) {
            nameGroup.addProposal(nameSuggestions[i], nameSuggestions.length - i);
        }
    }
    return vds;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) LinkedProposalPositionGroup(org.eclipse.jdt.ls.core.internal.corext.fix.LinkedProposalPositionGroup)

Aggregations

LinkedProposalPositionGroup (org.eclipse.jdt.ls.core.internal.corext.fix.LinkedProposalPositionGroup)6 AST (org.eclipse.jdt.core.dom.AST)3 ASTNode (org.eclipse.jdt.core.dom.ASTNode)3 Expression (org.eclipse.jdt.core.dom.Expression)3 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)3 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)3 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)2 Type (org.eclipse.jdt.core.dom.Type)2 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)2 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)2 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)2 TextEditGroup (org.eclipse.text.edits.TextEditGroup)2 ArrayList (java.util.ArrayList)1 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)1 Assignment (org.eclipse.jdt.core.dom.Assignment)1 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)1 CastExpression (org.eclipse.jdt.core.dom.CastExpression)1 ChildListPropertyDescriptor (org.eclipse.jdt.core.dom.ChildListPropertyDescriptor)1 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)1 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)1