Search in sources :

Example 21 with GrVariableDeclaration

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

the class OldReferencesResolver method inlineParam.

private PsiElement inlineParam(PsiElement newExpr, GrExpression actualArg, PsiParameter parameter) {
    if (myParamsToNotInline.contains(parameter))
        return newExpr;
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(myProject);
    if (myExpr instanceof GrClosableBlock) {
        int count = 0;
        for (PsiReference reference : ReferencesSearch.search(parameter, new LocalSearchScope(myParameterInitializer))) {
            count++;
            if (count > 1)
                break;
        }
        if (count > 1) {
            myParamsToNotInline.add(parameter);
            final PsiType type;
            if (parameter instanceof GrParameter) {
                type = ((GrParameter) parameter).getDeclaredType();
            } else {
                type = parameter.getType();
            }
            final GrVariableDeclaration declaration = factory.createVariableDeclaration(ArrayUtil.EMPTY_STRING_ARRAY, actualArg, type, parameter.getName());
            final GrStatement[] statements = ((GrClosableBlock) myExpr).getStatements();
            GrStatement anchor = statements.length > 0 ? statements[0] : null;
            return ((GrClosableBlock) myExpr).addStatementBefore(declaration, anchor);
        }
    }
    int copyingSafetyLevel = GroovyRefactoringUtil.verifySafeCopyExpression(actualArg);
    if (copyingSafetyLevel == RefactoringUtil.EXPR_COPY_PROHIBITED) {
        actualArg = factory.createExpressionFromText(getTempVar(actualArg));
    }
    newExpr = newExpr.replace(actualArg);
    return newExpr;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 22 with GrVariableDeclaration

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

the class GrVariableDeclarationElementType method shouldCreateStub.

@Override
public boolean shouldCreateStub(ASTNode node) {
    PsiElement parent = SharedImplUtil.getParent(node);
    if (parent instanceof GrTypeDefinitionBody) {
        // store fields
        return true;
    }
    if (PsiTreeUtil.getParentOfType(parent, GrTypeDefinition.class) != null) {
        // do not store variable declarations within classes, as they are not scripts
        return false;
    }
    PsiElement psi = node.getPsi();
    if (!(psi instanceof GrVariableDeclaration) || ((GrVariableDeclaration) psi).getModifierList().getRawAnnotations().length == 0) {
        // store only annotated declarations
        return false;
    }
    PsiFile file = psi.getContainingFile();
    return file instanceof GroovyFile && ((GroovyFile) file).isScript();
}
Also used : GrTypeDefinitionBody(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 23 with GrVariableDeclaration

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

the class GroovyDocumentationProvider method getQuickNavigateInfo.

@Override
@Nullable
public String getQuickNavigateInfo(PsiElement element, PsiElement originalElement) {
    if (element instanceof GrVariable || element instanceof GrImplicitVariable) {
        StringBuilder buffer = new StringBuilder();
        PsiVariable variable = (PsiVariable) element;
        if (originalElement instanceof GrVariableDeclaration && ((GrVariableDeclaration) originalElement).getVariables().length > 1) {
            for (GrVariable var : ((GrVariableDeclaration) originalElement).getVariables()) {
                generateVariableInfo(originalElement, buffer, var);
                buffer.append("\n\n");
            }
        } else {
            generateVariableInfo(originalElement, buffer, variable);
        }
        return buffer.toString();
    } else if (element instanceof PsiMethod) {
        StringBuilder buffer = new StringBuilder();
        PsiMethod method = (PsiMethod) element;
        if (method instanceof GrGdkMethod) {
            buffer.append("[GDK] ");
        } else {
            PsiClass hisClass = method.getContainingClass();
            if (hisClass != null) {
                String qName = hisClass.getQualifiedName();
                if (qName != null) {
                    buffer.append(qName).append("\n");
                }
            }
        }
        PsiSubstitutor substitutor = calcSubstitutor(originalElement);
        if (!method.isConstructor()) {
            final PsiType substituted = substitutor.substitute(PsiUtil.getSmartReturnType(method));
            PsiImplUtil.appendTypeString(buffer, substituted, originalElement);
            buffer.append(" ");
        }
        buffer.append(method.getName()).append(" ");
        buffer.append("(");
        PsiParameter[] parameters = method.getParameterList().getParameters();
        for (int i = 0; i < parameters.length; i++) {
            PsiParameter parameter = parameters[i];
            if (i > 0)
                buffer.append(", ");
            if (parameter instanceof GrParameter) {
                GroovyPresentationUtil.appendParameterPresentation((GrParameter) parameter, substitutor, TypePresentation.LINK, buffer);
            } else {
                PsiType type = parameter.getType();
                PsiImplUtil.appendTypeString(buffer, substitutor.substitute(type), originalElement);
                buffer.append(" ");
                buffer.append(parameter.getName());
            }
        }
        buffer.append(")");
        final PsiClassType[] referencedTypes = method.getThrowsList().getReferencedTypes();
        if (referencedTypes.length > 0) {
            buffer.append("\nthrows ");
            for (PsiClassType referencedType : referencedTypes) {
                PsiImplUtil.appendTypeString(buffer, referencedType, originalElement);
                buffer.append(", ");
            }
            buffer.delete(buffer.length() - 2, buffer.length());
        }
        return buffer.toString();
    } else if (element instanceof GrTypeDefinition) {
        return generateClassInfo((GrTypeDefinition) element);
    }
    return null;
}
Also used : GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrImplicitVariable(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrImplicitVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrGdkMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) Nullable(org.jetbrains.annotations.Nullable)

Example 24 with GrVariableDeclaration

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

the class GroovyCompletionUtil method isInPossibleClosureParameter.

public static boolean isInPossibleClosureParameter(PsiElement position) {
    //Closure cl={String x, <caret>...
    if (position == null)
        return false;
    if (position instanceof PsiWhiteSpace || position.getNode().getElementType() == GroovyTokenTypes.mNLS) {
        position = FilterPositionUtil.searchNonSpaceNonCommentBack(position);
    }
    boolean hasCommas = false;
    while (position != null) {
        PsiElement parent = position.getParent();
        if (parent instanceof GrVariable) {
            PsiElement prev = FilterPositionUtil.searchNonSpaceNonCommentBack(parent);
            hasCommas = prev != null && prev.getNode().getElementType() == GroovyTokenTypes.mCOMMA;
        }
        if (parent instanceof GrClosableBlock) {
            PsiElement sibling = position.getPrevSibling();
            while (sibling != null) {
                if (sibling instanceof GrParameterList) {
                    return hasCommas;
                }
                boolean isComma = sibling instanceof LeafPsiElement && GroovyTokenTypes.mCOMMA == ((LeafPsiElement) sibling).getElementType();
                hasCommas |= isComma;
                if (isComma || sibling instanceof PsiWhiteSpace || sibling instanceof PsiErrorElement || sibling instanceof GrVariableDeclaration || sibling instanceof GrReferenceExpression && !((GrReferenceExpression) sibling).isQualified()) {
                    sibling = sibling.getPrevSibling();
                } else {
                    return false;
                }
            }
            return false;
        }
        position = parent;
    }
    return false;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 25 with GrVariableDeclaration

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

the class GroovyCompletionUtil method isFirstElementAfterPossibleModifiersInVariableDeclaration.

/**
   * return true, if the element is first element after modifiers and there is no type element
   */
public static boolean isFirstElementAfterPossibleModifiersInVariableDeclaration(PsiElement element, boolean acceptParameter) {
    if (element.getParent() instanceof GrTypeDefinitionBody && !(element instanceof PsiComment)) {
        //is first on the line?
        String text = element.getContainingFile().getText();
        int i = CharArrayUtil.shiftBackward(text, element.getTextRange().getStartOffset() - 1, " \t");
        return i >= 0 && (text.charAt(i) == '\n' || text.charAt(i) == '{');
    }
    final PsiElement parent = element.getParent();
    if (!(parent instanceof GrVariable))
        return false;
    if (acceptParameter && parent instanceof GrParameter) {
        return ((GrParameter) parent).getTypeElementGroovy() == null;
    }
    final PsiElement pparent = parent.getParent();
    if (!(pparent instanceof GrVariableDeclaration))
        return false;
    if (((GrVariableDeclaration) pparent).isTuple())
        return false;
    final GrVariableDeclaration variableDeclaration = (GrVariableDeclaration) pparent;
    if (variableDeclaration.getTypeElementGroovy() != null)
        return false;
    return variableDeclaration.getVariables()[0] == parent;
}
Also used : GrTypeDefinitionBody(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) 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