Search in sources :

Example 1 with GrVariableDeclaration

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

the class GroovyBlockGenerator method calculateAlignments.

private void calculateAlignments(List<ASTNode> children, boolean classLevel) {
    List<GrStatement> currentGroup = null;
    boolean spock = true;
    for (ASTNode child : children) {
        PsiElement psi = child.getPsi();
        if (psi instanceof GrLabeledStatement) {
            alignGroup(currentGroup, spock, classLevel);
            currentGroup = ContainerUtil.newArrayList();
            spock = true;
        } else if (currentGroup != null && spock && isTablePart(psi)) {
            currentGroup.add((GrStatement) psi);
        } else if (psi instanceof GrVariableDeclaration) {
            GrVariable[] variables = ((GrVariableDeclaration) psi).getVariables();
            if (variables.length > 0) {
                if (!classLevel || currentGroup == null || fieldGroupEnded(psi) || spock) {
                    alignGroup(currentGroup, spock, classLevel);
                    currentGroup = ContainerUtil.newArrayList();
                    spock = false;
                }
                currentGroup.add((GrStatement) psi);
            }
        } else {
            if (shouldSkip(classLevel, psi))
                continue;
            alignGroup(currentGroup, spock, classLevel);
            currentGroup = null;
        }
    }
    if (currentGroup != null) {
        alignGroup(currentGroup, spock, classLevel);
    }
}
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) GrLabeledStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrLabeledStatement) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 2 with GrVariableDeclaration

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

the class GrVariableJoinLinesHandler method tryJoinStatements.

@Override
public int tryJoinStatements(@NotNull GrStatement first, @NotNull GrStatement second) {
    if (first instanceof GrVariableDeclaration && !((GrVariableDeclaration) first).isTuple() && second instanceof GrAssignmentExpression) {
        final GrExpression lvalue = ((GrAssignmentExpression) second).getLValue();
        final GrExpression rValue = ((GrAssignmentExpression) second).getRValue();
        if (lvalue instanceof GrReferenceExpression && rValue != null) {
            final PsiElement resolved = ((GrReferenceExpression) lvalue).resolve();
            if (ArrayUtil.contains(resolved, ((GrVariableDeclaration) first).getVariables())) {
                assert resolved instanceof GrVariable;
                if (((GrVariable) resolved).getInitializerGroovy() == null) {
                    ((GrVariable) resolved).setInitializerGroovy(rValue);
                    second.delete();
                    GrExpression newInitializer = ((GrVariable) resolved).getInitializerGroovy();
                    assert newInitializer != null;
                    return newInitializer.getTextRange().getEndOffset();
                }
            }
        }
    }
    return CANNOT_JOIN;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 3 with GrVariableDeclaration

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

the class GrIntroduceVariableHandler method addVariable.

private static GrVariable addVariable(@NotNull GrIntroduceContext context, @NotNull GroovyIntroduceVariableSettings settings) {
    GrStatement anchor = findAnchor(context, settings.replaceAllOccurrences());
    PsiElement parent = anchor.getParent();
    assert parent instanceof GrStatementOwner;
    GrVariableDeclaration generated = generateDeclaration(context, settings);
    GrStatement declaration = ((GrStatementOwner) parent).addStatementBefore(generated, anchor);
    declaration = (GrStatement) JavaCodeStyleManager.getInstance(context.getProject()).shortenClassReferences(declaration);
    return ((GrVariableDeclaration) declaration).getVariables()[0];
}
Also used : GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 4 with GrVariableDeclaration

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

the class GrIntroduceVariableHandler method generateDeclaration.

@NotNull
private static GrVariableDeclaration generateDeclaration(@NotNull GrIntroduceContext context, @NotNull GroovyIntroduceVariableSettings settings) {
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(context.getProject());
    final String[] modifiers = settings.isDeclareFinal() ? new String[] { PsiModifier.FINAL } : null;
    final GrVariableDeclaration declaration = factory.createVariableDeclaration(modifiers, "foo", settings.getSelectedType(), settings.getName());
    generateInitializer(context, declaration.getVariables()[0]);
    return declaration;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with GrVariableDeclaration

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

the class GrSetStrongTypeIntention method getOrCreateTypeElement.

@NotNull
private static TypeInfo getOrCreateTypeElement(@NotNull PsiElement parent, @NotNull PsiElement elementToBuildTemplateOn) {
    GrModifierList modifierList = getModifierList(parent);
    if (modifierList != null && modifierList.hasModifierProperty(GrModifier.DEF) && modifierList.getModifiers().length == 1) {
        PsiElement modifier = modifierList.getModifier(GrModifier.DEF);
        LOG.assertTrue(modifier != null);
        int modifierOffset = modifier.getTextRange().getEndOffset() - elementToBuildTemplateOn.getTextRange().getStartOffset();
        return new TypeInfo(modifier, modifierOffset);
    } else {
        int nameElementOffset;
        final PsiClassType typeToUse = TypesUtil.createType("Abc", parent);
        if (elementToBuildTemplateOn instanceof GrVariableDeclaration) {
            GrVariableDeclaration decl = (GrVariableDeclaration) elementToBuildTemplateOn;
            decl.setType(typeToUse);
            nameElementOffset = decl.getModifierList().getTextRange().getEndOffset() - elementToBuildTemplateOn.getTextRange().getStartOffset();
        } else {
            GrVariable var = (GrVariable) parent;
            var.setType(typeToUse);
            nameElementOffset = var.getNameIdentifierGroovy().getTextRange().getStartOffset() - elementToBuildTemplateOn.getTextRange().getStartOffset();
        }
        return new TypeInfo(getTypeElement(parent), nameElementOffset);
    }
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) PsiClassType(com.intellij.psi.PsiClassType) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) PsiElement(com.intellij.psi.PsiElement) SupertypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.SupertypeConstraint) TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint) NotNull(org.jetbrains.annotations.NotNull)

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