Search in sources :

Example 31 with GrVariableDeclaration

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.

the class GrSplitDeclarationIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    if (!(element instanceof GrVariableDeclaration))
        return;
    GrVariableDeclaration declaration = (GrVariableDeclaration) element;
    GrVariable[] variables = declaration.getVariables();
    if (variables.length == 1) {
        processSingleVar(project, declaration, variables[0]);
    } else if (variables.length > 1) {
        if (!declaration.isTuple() || declaration.getTupleInitializer() instanceof GrListOrMap) {
            processMultipleVars(project, declaration);
        } else {
            processTuple(project, declaration);
        }
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)

Example 32 with GrVariableDeclaration

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.

the class GrSplitDeclarationIntention method createVarDeclaration.

private static GrStatement createVarDeclaration(Project project, GrVariable variable, String modifiers, boolean isTuple) {
    StringBuilder builder = new StringBuilder();
    builder.append(modifiers).append(' ');
    GrTypeElement typeElement = variable.getTypeElementGroovy();
    if (typeElement != null) {
        builder.append(typeElement.getText()).append(' ');
    }
    builder.append(variable.getName());
    GrExpression initializer = variable.getInitializerGroovy();
    if (initializer != null) {
        builder.append('=').append(initializer.getText());
    }
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    GrVariableDeclaration decl = (GrVariableDeclaration) factory.createStatementFromText(builder);
    if (isTuple && (variable.getDeclaredType() != null || decl.getModifierList().getModifiers().length > 1)) {
        decl.getModifierList().setModifierProperty(GrModifier.DEF, false);
    }
    return decl;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)

Example 33 with GrVariableDeclaration

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.

the class ConvertMethodToClosureIntention method execute.

private static void execute(final GrMethod method, final Collection<GrReferenceExpression> usagesToConvert) {
    ApplicationManager.getApplication().runWriteAction(() -> {
        GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(method.getProject());
        StringBuilder builder = new StringBuilder(method.getTextLength());
        String modifiers = method.getModifierList().getText();
        if (modifiers.trim().isEmpty()) {
            modifiers = GrModifier.DEF;
        }
        builder.append(modifiers).append(' ');
        builder.append(method.getName()).append("={");
        builder.append(method.getParameterList().getText()).append(" ->");
        final GrOpenBlock block = method.getBlock();
        builder.append(block.getText().substring(1));
        final GrVariableDeclaration variableDeclaration = GroovyPsiElementFactory.getInstance(method.getProject()).createFieldDeclarationFromText(builder.toString());
        method.replace(variableDeclaration);
        for (GrReferenceExpression element : usagesToConvert) {
            final PsiElement qualifier = element.getQualifier();
            final StringBuilder text = new StringBuilder(qualifier.getText());
            element.setQualifier(null);
            text.append('.').append(element.getText());
            element.replace(factory.createExpressionFromText(text.toString()));
        }
    });
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 34 with GrVariableDeclaration

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.

the class GrCreateFieldForParameterIntention method performRefactoring.

@Override
protected void performRefactoring(Project project, PsiClass targetClass, PsiMethod method, PsiParameter myParameter, PsiType type, String fieldName, boolean methodStatic, boolean isFinal) {
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    if (targetClass.findFieldByName(fieldName, false) == null) {
        String[] modifiers = getModifiers(methodStatic, isFinal);
        GrVariableDeclaration fieldDeclaration = factory.createFieldDeclaration(modifiers, fieldName, null, type);
        GrVariableDeclaration inserted = (GrVariableDeclaration) targetClass.add(fieldDeclaration);
        JavaCodeStyleManager.getInstance(project).shortenClassReferences(inserted);
    }
    GrOpenBlock block = ((GrMethod) method).getBlock();
    if (block == null)
        return;
    GrAssignmentExpression assignment = createAssignment(targetClass, myParameter, fieldName, methodStatic, factory);
    GrStatement anchor = getAnchor(block);
    GrStatement statement = block.addStatementBefore(assignment, anchor);
    JavaCodeStyleManager.getInstance(project).shortenClassReferences(statement);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 35 with GrVariableDeclaration

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.

the class GroovyBlockGenerator method alignVariableDeclarations.

private void alignVariableDeclarations(List<GrStatement> group, boolean classLevel) {
    AlignmentProvider.Aligner typeElement = myAlignmentProvider.createAligner(true);
    AlignmentProvider.Aligner varName = myAlignmentProvider.createAligner(true);
    AlignmentProvider.Aligner eq = myAlignmentProvider.createAligner(true);
    for (GrStatement statement : group) {
        GrVariableDeclaration varDeclaration = (GrVariableDeclaration) statement;
        GrVariable[] variables = varDeclaration.getVariables();
        for (GrVariable variable : variables) {
            varName.append(variable.getNameIdentifierGroovy());
        }
        if (classLevel && myContext.getSettings().ALIGN_GROUP_FIELD_DECLARATIONS) {
            typeElement.append(varDeclaration.getTypeElementGroovy());
            ASTNode current_eq = variables[variables.length - 1].getNode().findChildByType(GroovyTokenTypes.mASSIGN);
            if (current_eq != null) {
                eq.append(current_eq.getPsi());
            }
        }
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) ASTNode(com.intellij.lang.ASTNode) AlignmentProvider(org.jetbrains.plugins.groovy.formatter.AlignmentProvider) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Aggregations

GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)43 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)19 PsiElement (com.intellij.psi.PsiElement)15 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)9 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)9 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)9 Nullable (org.jetbrains.annotations.Nullable)7 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)7 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)7 GrTypeElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement)7 NotNull (org.jetbrains.annotations.NotNull)6 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)6 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)6 ASTNode (com.intellij.lang.ASTNode)5 TextRange (com.intellij.openapi.util.TextRange)4 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)4 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)4 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)4 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)4 Template (com.intellij.codeInsight.template.Template)3