Search in sources :

Example 81 with GrVariable

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

the class ReachingDefinitionsCollector method filterNonlocals.

private static VariableInfo[] filterNonlocals(Map<String, VariableInfo> infos, GrStatement place) {
    List<VariableInfo> result = new ArrayList<>();
    for (Iterator<VariableInfo> iterator = infos.values().iterator(); iterator.hasNext(); ) {
        VariableInfo info = iterator.next();
        String name = info.getName();
        GroovyPsiElement property = ResolveUtil.resolveProperty(place, name);
        if (property instanceof GrVariable) {
            iterator.remove();
        } else if (property instanceof GrReferenceExpression) {
            GrMember member = PsiTreeUtil.getParentOfType(property, GrMember.class);
            if (member == null) {
                continue;
            } else if (!member.hasModifierProperty(PsiModifier.STATIC)) {
                if (member.getContainingClass() instanceof GroovyScriptClass) {
                    //binding variable
                    continue;
                }
            }
        }
        if (ResolveUtil.resolveClass(place, name) == null) {
            result.add(info);
        }
    }
    return result.toArray(new VariableInfo[result.size()]);
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 82 with GrVariable

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

the class GrCharConverter method isConvertibleEx.

@Nullable
@Override
public ConversionResult isConvertibleEx(@NotNull PsiType lType, @NotNull PsiType rType, @NotNull GroovyPsiElement context, @NotNull ApplicableTo currentPosition) {
    if (!PsiType.CHAR.equals(TypesUtil.unboxPrimitiveTypeWrapper(lType)))
        return null;
    if (PsiType.CHAR.equals(TypesUtil.unboxPrimitiveTypeWrapper(rType)))
        return ConversionResult.OK;
    // can assign numeric types to char
    if (TypesUtil.isNumericType(rType)) {
        if (rType instanceof PsiPrimitiveType || TypesUtil.unboxPrimitiveTypeWrapper(rType) instanceof PsiPrimitiveType) {
            return PsiType.CHAR.equals(lType) ? ConversionResult.OK : ConversionResult.ERROR;
        } else {
            // BigDecimal && BigInteger
            return ConversionResult.ERROR;
        }
    }
    {
        // special case 'c = []' will throw RuntimeError
        final GrExpression rValue;
        if (context instanceof GrAssignmentExpression) {
            final GrAssignmentExpression assignmentExpression = (GrAssignmentExpression) context;
            rValue = assignmentExpression.getRValue();
        } else if (context instanceof GrVariable) {
            final GrVariable assignmentExpression = (GrVariable) context;
            rValue = assignmentExpression.getInitializerGroovy();
        } else {
            rValue = null;
        }
        if (rValue instanceof GrListOrMap && ((GrListOrMap) rValue).isEmpty()) {
            return ConversionResult.WARNING;
        }
    }
    if (PsiType.BOOLEAN.equals(TypesUtil.unboxPrimitiveTypeWrapper(rType))) {
        switch(currentPosition) {
            case ASSIGNMENT:
            case RETURN_VALUE:
                return ConversionResult.WARNING;
            default:
                return null;
        }
    }
    // one-symbol string-to-char conversion doesn't work with return value
    if (currentPosition == ApplicableTo.RETURN_VALUE) {
        return null;
    }
    // can cast and assign one-symbol strings to char
    if (!TypesUtil.isClassType(rType, CommonClassNames.JAVA_LANG_STRING))
        return null;
    return checkSingleSymbolLiteral(context) ? ConversionResult.OK : ConversionResult.ERROR;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) PsiPrimitiveType(com.intellij.psi.PsiPrimitiveType) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) Nullable(org.jetbrains.annotations.Nullable)

Example 83 with GrVariable

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

the class GrMainCompletionProvider method suggestVariableNames.

private static void suggestVariableNames(PsiElement context, CompletionResultSet result) {
    final PsiElement parent = context.getParent();
    if (GroovyCompletionUtil.isWildcardCompletion(context))
        return;
    if (parent instanceof GrVariable) {
        final GrVariable variable = (GrVariable) parent;
        if (context.equals(variable.getNameIdentifierGroovy())) {
            final PsiType type = variable.getTypeGroovy();
            if (type != null) {
                final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(context.getProject());
                VariableKind kind = variable instanceof GrParameter ? VariableKind.PARAMETER : variable instanceof GrField ? VariableKind.FIELD : VariableKind.LOCAL_VARIABLE;
                SuggestedNameInfo suggestedNameInfo = codeStyleManager.suggestVariableName(kind, null, null, type);
                String[] names = suggestedNameInfo.names;
                if (names.length > 0) {
                    String name = names[0];
                    String newName = InlineMethodConflictSolver.suggestNewName(name, null, parent);
                    if (!name.equals(newName)) {
                        result.addElement(LookupElementBuilder.create(newName));
                        return;
                    }
                }
                for (String name : names) {
                    result.addElement(LookupElementBuilder.create(name));
                }
            }
            GrExpression initializer = variable.getInitializerGroovy();
            if (initializer != null) {
                for (String name : GroovyNameSuggestionUtil.suggestVariableNames(initializer, new DefaultGroovyVariableNameValidator(variable), variable.hasModifierProperty(PsiModifier.STATIC))) {
                    result.addElement(LookupElementBuilder.create(name));
                }
            }
        }
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) DefaultGroovyVariableNameValidator(org.jetbrains.plugins.groovy.refactoring.DefaultGroovyVariableNameValidator) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) VariableKind(com.intellij.psi.codeStyle.VariableKind)

Example 84 with GrVariable

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

the class GroovySmartCompletionContributor method addExpectedType.

@Nullable
private static LookupElement addExpectedType(PsiType type, final PsiElement place, CompletionParameters parameters, @Nullable PsiType diamond) {
    if (!JavaCompletionUtil.hasAccessibleConstructor(type))
        return null;
    final PsiClass psiClass = com.intellij.psi.util.PsiUtil.resolveClassInType(type);
    if (psiClass == null)
        return null;
    if (!checkForInnerClass(psiClass, place))
        return null;
    boolean isDiamond = false;
    if (diamond != null && psiClass.hasTypeParameters() && !((PsiClassType) type).isRaw() && !psiClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
        final String canonicalText = TypeConversionUtil.erasure(type).getCanonicalText();
        final GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(place.getProject());
        final String text = diamond.getCanonicalText() + " v = new " + canonicalText + "<>()";
        final GrStatement statement = elementFactory.createStatementFromText(text, parameters.getOriginalFile());
        final GrVariable declaredVar = ((GrVariableDeclaration) statement).getVariables()[0];
        final GrNewExpression initializer = (GrNewExpression) declaredVar.getInitializerGroovy();
        assert initializer != null;
        final boolean hasDefaultConstructorOrNoGenericsOne = PsiDiamondTypeImpl.hasDefaultConstructor(psiClass) || !PsiDiamondTypeImpl.haveConstructorsGenericsParameters(psiClass);
        final PsiType initializerType = initializer.getType();
        if (hasDefaultConstructorOrNoGenericsOne && initializerType != null && initializerType instanceof PsiClassType && ((PsiClassType) initializerType).getParameters().length > 0) {
            type = initializerType;
            isDiamond = true;
        }
    }
    final PsiTypeLookupItem item = PsiTypeLookupItem.createLookupItem(GenericsUtil.eliminateWildcards(type), place, isDiamond, ChooseTypeExpression.IMPORT_FIXER).setShowPackage();
    Object object = item.getObject();
    if (object instanceof PsiClass && ((PsiClass) object).hasModifierProperty(PsiModifier.ABSTRACT)) {
        item.setIndicateAnonymous(true);
    }
    item.setInsertHandler(new AfterNewClassInsertHandler((PsiClassType) type, true));
    return item;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) PsiTypeLookupItem(com.intellij.codeInsight.lookup.PsiTypeLookupItem) AfterNewClassInsertHandler(org.jetbrains.plugins.groovy.lang.completion.handlers.AfterNewClassInsertHandler) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) Nullable(org.jetbrains.annotations.Nullable)

Example 85 with GrVariable

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable 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)

Aggregations

GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)88 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)34 PsiElement (com.intellij.psi.PsiElement)24 Nullable (org.jetbrains.annotations.Nullable)20 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)19 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)16 NotNull (org.jetbrains.annotations.NotNull)14 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)14 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)12 StringPartInfo (org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo)11 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)10 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)10 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)8 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)8 PsiType (com.intellij.psi.PsiType)7 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)7 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)7 TextRange (com.intellij.openapi.util.TextRange)6 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)6 ASTNode (com.intellij.lang.ASTNode)5