Search in sources :

Example 16 with GrVariableDeclaration

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

the class GrDocCommentUtil method findDocComment.

@Nullable
public static GrDocComment findDocComment(GrDocCommentOwner owner) {
    if (owner.getFirstChild() instanceof GrDocComment) {
        return ((GrDocComment) owner.getFirstChild());
    }
    PsiElement element = owner instanceof GrVariable && owner.getParent() instanceof GrVariableDeclaration ? owner.getParent() : owner;
    element = skipWhiteSpacesAndStopOnDoc(element, false);
    if (element instanceof GrDocComment)
        return (GrDocComment) element;
    return null;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) PsiElement(com.intellij.psi.PsiElement) GroovyDocPsiElement(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GroovyDocPsiElement) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment) Nullable(org.jetbrains.annotations.Nullable)

Example 17 with GrVariableDeclaration

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

the class GrVariableBaseImpl method delete.

@Override
public void delete() throws IncorrectOperationException {
    PsiElement parent = getParent();
    PsiElement prev = PsiUtil.getPrevNonSpace(this);
    PsiElement next = PsiUtil.getNextNonSpace(this);
    ASTNode parentNode = parent.getNode();
    assert parentNode != null;
    super.delete();
    if (prev != null && prev.getNode() != null && prev.getNode().getElementType() == GroovyTokenTypes.mCOMMA) {
        prev.delete();
    } else if (next instanceof LeafPsiElement && next.getNode() != null && next.getNode().getElementType() == GroovyTokenTypes.mCOMMA) {
        next.delete();
    }
    if (parent instanceof GrVariableDeclaration && ((GrVariableDeclaration) parent).getVariables().length == 0) {
        parent.delete();
    }
}
Also used : GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) ASTNode(com.intellij.lang.ASTNode) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Example 18 with GrVariableDeclaration

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

the class GrVariableBaseImpl method setType.

@Override
public void setType(@Nullable PsiType type) {
    final GrVariableDeclaration variableDeclaration = getDeclaration();
    if (variableDeclaration == null)
        return;
    final GrTypeElement typeElement = variableDeclaration.getTypeElementGroovyForVariable(this);
    if (type == null) {
        if (typeElement != null) {
            if (!variableDeclaration.isTuple() && variableDeclaration.getModifierList().getModifiers().length == 0) {
                variableDeclaration.getModifierList().setModifierProperty(GrModifier.DEF, true);
            }
            typeElement.delete();
        }
        return;
    }
    type = TypesUtil.unboxPrimitiveTypeWrapper(type);
    GrTypeElement newTypeElement;
    try {
        newTypeElement = GroovyPsiElementFactory.getInstance(getProject()).createTypeElement(type);
    } catch (IncorrectOperationException e) {
        LOG.error(e);
        return;
    }
    if (typeElement == null) {
        newTypeElement = (GrTypeElement) getParent().addBefore(newTypeElement, this);
    } else {
        newTypeElement = (GrTypeElement) typeElement.replace(newTypeElement);
    }
    JavaCodeStyleManager.getInstance(getProject()).shortenClassReferences(newTypeElement);
}
Also used : GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 19 with GrVariableDeclaration

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

the class GrVariableBaseImpl method getTypeElementGroovy.

@Override
@Nullable
public GrTypeElement getTypeElementGroovy() {
    T stub = getStub();
    if (stub != null) {
        return stub.getTypeElement();
    }
    PsiElement parent = getParent();
    if (parent instanceof GrVariableDeclaration) {
        return ((GrVariableDeclaration) parent).getTypeElementGroovyForVariable(this);
    }
    return findChildByClass(GrTypeElement.class);
}
Also used : GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 20 with GrVariableDeclaration

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

the class GrTypeDefinitionImpl method add.

@Override
public PsiElement add(@NotNull PsiElement psiElement) throws IncorrectOperationException {
    final GrTypeDefinitionBody body = getBody();
    if (body == null)
        throw new IncorrectOperationException("Class must have body");
    final PsiElement lBrace = body.getLBrace();
    if (lBrace == null)
        throw new IncorrectOperationException("No left brace");
    PsiMember member = getAnyMember(psiElement);
    PsiElement anchor = member != null ? getDefaultAnchor(body, member) : null;
    if (anchor == null) {
        anchor = lBrace.getNextSibling();
    }
    if (anchor != null) {
        ASTNode node = anchor.getNode();
        assert node != null;
        if (GroovyTokenTypes.mSEMI.equals(node.getElementType())) {
            anchor = anchor.getNextSibling();
        }
        if (psiElement instanceof GrField) {
            //add field with modifiers which are in its parent
            int i = ArrayUtilRt.find(((GrVariableDeclaration) psiElement.getParent()).getVariables(), psiElement);
            psiElement = body.addBefore(psiElement.getParent(), anchor);
            GrVariable[] vars = ((GrVariableDeclaration) psiElement).getVariables();
            for (int j = 0; j < vars.length; j++) {
                if (i != j)
                    vars[i].delete();
            }
            psiElement = vars[i];
        } else {
            psiElement = body.addBefore(psiElement, anchor);
        }
    } else {
        psiElement = body.add(psiElement);
    }
    return psiElement;
}
Also used : GrTypeDefinitionBody(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) ASTNode(com.intellij.lang.ASTNode) IncorrectOperationException(com.intellij.util.IncorrectOperationException) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

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