Search in sources :

Example 31 with GrListOrMap

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

the class LiteralConstructorReference method inferConversionType.

@Nullable
private PsiClassType inferConversionType() {
    final GrListOrMap listOrMap = getElement();
    final PsiClassType conversionType = getTargetConversionType(listOrMap);
    if (conversionType == null)
        return null;
    if (listOrMap.isEmpty()) {
        PsiType unboxed = TypesUtil.unboxPrimitiveTypeWrapper(conversionType);
        if (PsiType.BOOLEAN.equals(unboxed) || PsiType.CHAR.equals(unboxed)) {
            return null;
        }
    }
    final PsiType type = listOrMap.getType();
    final PsiType ownType = type instanceof PsiClassType ? ((PsiClassType) type).rawType() : type;
    if (ownType != null && TypesUtil.isAssignableWithoutConversions(conversionType.rawType(), ownType, listOrMap))
        return null;
    final PsiClass resolved = conversionType.resolve();
    if (resolved != null) {
        if (InheritanceUtil.isInheritor(resolved, CommonClassNames.JAVA_UTIL_SET))
            return null;
        if (InheritanceUtil.isInheritor(resolved, CommonClassNames.JAVA_UTIL_LIST))
            return null;
    }
    return conversionType;
}
Also used : GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) Nullable(org.jetbrains.annotations.Nullable)

Example 32 with GrListOrMap

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap 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 33 with GrListOrMap

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

the class GrSplitDeclarationIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    if (!(element instanceof GrVariableDeclaration))
        return;
    GrVariableDeclaration declaration = (GrVariableDeclaration) element;
    GrVariable[] variables = declaration.getVariables();
    if (variables.length == 1) {
        processSingleVar(project, declaration, variables[0]);
    } else if (variables.length > 1) {
        if (!declaration.isTuple() || declaration.getTupleInitializer() instanceof GrListOrMap) {
            processMultipleVars(project, declaration);
        } else {
            processTuple(project, declaration);
        }
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)

Example 34 with GrListOrMap

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

the class MyPredicate method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull final Project project, Editor editor) throws IncorrectOperationException {
    final GrListOrMap map = (GrListOrMap) element;
    final GrNamedArgument[] namedArguments = map.getNamedArguments();
    LOG.assertTrue(map.getInitializers().length == 0);
    final PsiFile file = map.getContainingFile();
    final String packageName = file instanceof GroovyFileBase ? ((GroovyFileBase) file).getPackageName() : "";
    final CreateClassDialog dialog = new CreateClassDialog(project, GroovyBundle.message("create.class.family.name"), "", packageName, GrCreateClassKind.CLASS, true, ModuleUtilCore.findModuleForPsiElement(element));
    dialog.show();
    if (dialog.getExitCode() != DialogWrapper.OK_EXIT_CODE)
        return;
    boolean replaceReturnType = checkForReturnFromMethod(map);
    boolean variableDeclaration = checkForVariableDeclaration(map);
    final GrParameter methodParameter = checkForMethodParameter(map);
    final String qualifiedClassName = dialog.getClassName();
    final String selectedPackageName = StringUtil.getPackageName(qualifiedClassName);
    final String shortName = StringUtil.getShortName(qualifiedClassName);
    final GrTypeDefinition typeDefinition = createClass(project, namedArguments, selectedPackageName, shortName);
    final PsiClass generatedClass = CreateClassActionBase.createClassByType(dialog.getTargetDirectory(), typeDefinition.getName(), PsiManager.getInstance(project), map, GroovyTemplates.GROOVY_CLASS, true);
    final PsiClass replaced = (PsiClass) generatedClass.replace(typeDefinition);
    replaceMapWithClass(project, map, replaced, replaceReturnType, variableDeclaration, methodParameter);
}
Also used : GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) CreateClassDialog(com.intellij.codeInsight.intention.impl.CreateClassDialog) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)

Example 35 with GrListOrMap

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

the class MyPredicate method satisfiedBy.

@Override
public boolean satisfiedBy(PsiElement element) {
    if (!(element instanceof GrListOrMap))
        return false;
    final GrListOrMap map = (GrListOrMap) element;
    final GrNamedArgument[] namedArguments = map.getNamedArguments();
    final GrExpression[] initializers = map.getInitializers();
    if (initializers.length != 0)
        return false;
    for (GrNamedArgument argument : namedArguments) {
        final GrArgumentLabel label = argument.getLabel();
        final GrExpression expression = argument.getExpression();
        if (label == null || expression == null)
            return false;
        if (label.getName() == null)
            return false;
    }
    return true;
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrArgumentLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)

Aggregations

GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)39 PsiElement (com.intellij.psi.PsiElement)13 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)12 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)12 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)12 Nullable (org.jetbrains.annotations.Nullable)9 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)9 NotNull (org.jetbrains.annotations.NotNull)8 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)5 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)4 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)4 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)4 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)4 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)3 GrArgumentLabel (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel)3 ASTNode (com.intellij.lang.ASTNode)2 Pair (com.intellij.openapi.util.Pair)2 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)2 IElementType (com.intellij.psi.tree.IElementType)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2