Search in sources :

Example 26 with GrVariableDeclaration

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

the class GroovyCompletionUtil method isTupleVarNameWithoutTypeDeclared.

public static boolean isTupleVarNameWithoutTypeDeclared(PsiElement position) {
    PsiElement parent = position.getParent();
    PsiElement pparent = parent.getParent();
    return parent instanceof GrVariable && ((GrVariable) parent).getNameIdentifierGroovy() == position && ((GrVariable) parent).getTypeElementGroovy() == null && pparent instanceof GrVariableDeclaration && ((GrVariableDeclaration) pparent).isTuple();
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Example 27 with GrVariableDeclaration

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

the class PsiImplUtil method removeVariable.

public static void removeVariable(GrVariable variable) {
    final GrVariableDeclaration varDecl = (GrVariableDeclaration) variable.getParent();
    final List<GrVariable> variables = Arrays.asList(varDecl.getVariables());
    if (!variables.contains(variable)) {
        throw new IllegalArgumentException();
    }
    final PsiElement parent = varDecl.getParent();
    final ASTNode owner = parent.getNode();
    if (variables.size() == 1 && owner != null) {
        PsiElement next = varDecl.getNextSibling();
        //noinspection ConstantConditions
        while (next != null && next.getNode() != null && next.getNode().getElementType() == GroovyTokenTypes.mSEMI) {
            PsiElement tmpNext = next.getNextSibling();
            //noinspection ConstantConditions
            next.delete();
            next = tmpNext;
        }
        removeNewLineAfter(varDecl);
        varDecl.delete();
        return;
    }
    variable.delete();
}
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)

Example 28 with GrVariableDeclaration

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

the class GrVariableBaseImpl method setInitializerGroovy.

@Override
public void setInitializerGroovy(GrExpression initializer) {
    if (getParent() instanceof GrVariableDeclaration && ((GrVariableDeclaration) getParent()).isTuple()) {
        throw new UnsupportedOperationException("don't invoke 'setInitializer()' for tuple declaration");
    }
    GrExpression oldInitializer = getInitializerGroovy();
    if (initializer == null) {
        if (oldInitializer != null) {
            oldInitializer.delete();
            PsiElement assign = findChildByType(GroovyTokenTypes.mASSIGN);
            if (assign != null) {
                assign.delete();
            }
        }
        return;
    }
    if (oldInitializer != null) {
        oldInitializer.replaceWithExpression(initializer, true);
    } else {
        getNode().addLeaf(TokenType.WHITE_SPACE, " ", null);
        getNode().addLeaf(GroovyTokenTypes.mASSIGN, "=", null);
        addAfter(initializer, getLastChild());
    }
}
Also used : GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Example 29 with GrVariableDeclaration

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

the class GrVariableBaseImpl method getInitializerGroovy.

@Override
@Nullable
public GrExpression getInitializerGroovy() {
    final PsiElement parent = getParent();
    if (parent instanceof GrVariableDeclaration && ((GrVariableDeclaration) parent).isTuple()) {
        final GrVariableDeclaration tuple = (GrVariableDeclaration) parent;
        final GrExpression initializer = tuple.getTupleInitializer();
        if (initializer instanceof GrListOrMap) {
            final GrListOrMap listOrMap = (GrListOrMap) initializer;
            final GrExpression[] initializers = listOrMap.getInitializers();
            final int varNumber = ArrayUtil.indexOf(tuple.getVariables(), this);
            if (initializers.length < varNumber + 1)
                return null;
            return initializers[varNumber];
        }
    }
    return GroovyPsiElementImpl.findExpressionChild(this);
}
Also used : GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 30 with GrVariableDeclaration

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

the class GrSetStrongTypeIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, final Editor editor) throws IncorrectOperationException {
    PsiElement parent = element.getParent();
    PsiElement elementToBuildTemplate;
    GrVariable[] variables;
    if (parent instanceof GrVariable && parent.getParent() instanceof GrVariableDeclaration) {
        variables = ((GrVariableDeclaration) parent.getParent()).getVariables();
        elementToBuildTemplate = parent.getParent();
    } else if (parent instanceof GrVariable && parent.getParent() instanceof GrForInClause) {
        variables = new GrVariable[] { (GrVariable) parent };
        elementToBuildTemplate = parent.getParent().getParent();
    } else if (parent instanceof GrVariableDeclaration) {
        variables = ((GrVariableDeclaration) parent).getVariables();
        elementToBuildTemplate = parent;
    } else if (parent instanceof GrParameter && parent.getParent() instanceof GrParameterList) {
        variables = new GrVariable[] { (GrVariable) parent };
        elementToBuildTemplate = parent.getParent().getParent();
    } else if (parent instanceof GrVariable) {
        variables = new GrVariable[] { ((GrVariable) parent) };
        elementToBuildTemplate = parent;
    } else {
        return;
    }
    ArrayList<TypeConstraint> types = new ArrayList<>();
    if (parent.getParent() instanceof GrForInClause) {
        types.add(SupertypeConstraint.create(PsiUtil.extractIteratedType((GrForInClause) parent.getParent())));
    } else {
        for (GrVariable variable : variables) {
            GrExpression initializer = variable.getInitializerGroovy();
            if (initializer != null) {
                PsiType type = initializer.getType();
                if (type != null) {
                    types.add(SupertypeConstraint.create(type));
                }
            }
            if (variable instanceof GrParameter) {
                final PsiParameter parameter = (PsiParameter) variable;
                final PsiType type = getClosureParameterType(parameter);
                if (type != null) {
                    types.add(SupertypeConstraint.create(type));
                }
            }
        }
    }
    final String originalText = elementToBuildTemplate.getText();
    final TypeInfo typeInfo = getOrCreateTypeElement(parent, elementToBuildTemplate);
    final PsiElement replaceElement = typeInfo.elementToReplace;
    TypeConstraint[] constraints = types.toArray(new TypeConstraint[types.size()]);
    ChooseTypeExpression chooseTypeExpression = new ChooseTypeExpression(constraints, element.getManager(), replaceElement.getResolveScope());
    TemplateBuilderImpl builder = new TemplateBuilderImpl(elementToBuildTemplate);
    builder.replaceElement(replaceElement, chooseTypeExpression);
    final Document document = editor.getDocument();
    final RangeMarker rangeMarker = document.createRangeMarker(elementToBuildTemplate.getTextRange());
    rangeMarker.setGreedyToRight(true);
    rangeMarker.setGreedyToLeft(true);
    final PsiElement afterPostprocess = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(elementToBuildTemplate);
    final Template template = builder.buildTemplate();
    TextRange range = afterPostprocess.getTextRange();
    document.deleteString(range.getStartOffset(), range.getEndOffset());
    TemplateManager templateManager = TemplateManager.getInstance(project);
    templateManager.startTemplate(editor, template, new TemplateEditingAdapter() {

        @Override
        public void templateFinished(Template template, boolean brokenOff) {
            if (brokenOff) {
                ApplicationManager.getApplication().runWriteAction(() -> {
                    if (rangeMarker.isValid()) {
                        document.replaceString(rangeMarker.getStartOffset(), rangeMarker.getEndOffset(), originalText);
                        editor.getCaretModel().moveToOffset(rangeMarker.getStartOffset() + typeInfo.originalOffset);
                    }
                });
            }
        }
    });
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) ArrayList(java.util.ArrayList) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) Document(com.intellij.openapi.editor.Document) ChooseTypeExpression(org.jetbrains.plugins.groovy.template.expressions.ChooseTypeExpression) Template(com.intellij.codeInsight.template.Template) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) TemplateManager(com.intellij.codeInsight.template.TemplateManager) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType) TemplateEditingAdapter(com.intellij.codeInsight.template.TemplateEditingAdapter) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) TextRange(com.intellij.openapi.util.TextRange) RangeMarker(com.intellij.openapi.editor.RangeMarker) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) PsiParameter(com.intellij.psi.PsiParameter) GrForInClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause) TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint)

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