Search in sources :

Example 76 with GrArgumentList

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

the class PsiUtil method getArgumentTypes.

@Nullable
public static PsiType[] getArgumentTypes(@Nullable PsiElement place, boolean nullAsBottom, @Nullable GrExpression stopAt) {
    PsiElement parent = place instanceof GrEnumConstant ? place : place != null ? place.getParent() : null;
    if (parent instanceof GrIndexProperty) {
        GrIndexProperty index = (GrIndexProperty) parent;
        PsiType[] argTypes = getArgumentTypes(index.getNamedArguments(), index.getExpressionArguments(), index.getClosureArguments(), nullAsBottom, stopAt);
        if (isLValue(index) && argTypes != null) {
            PsiType rawInitializer = TypeInferenceHelper.getInitializerTypeFor(index);
            PsiType initializer = notNullizeType(rawInitializer, nullAsBottom, index);
            return ArrayUtil.append(argTypes, initializer);
        } else {
            return argTypes;
        }
    }
    if (parent instanceof GrCall) {
        GrCall call = (GrCall) parent;
        GrNamedArgument[] namedArgs = call.getNamedArguments();
        GrExpression[] expressions = call.getExpressionArguments();
        GrClosableBlock[] closures = call.getClosureArguments();
        return getArgumentTypes(namedArgs, expressions, closures, nullAsBottom, stopAt);
    } else if (parent instanceof GrAnonymousClassDefinition) {
        final GrArgumentList argList = ((GrAnonymousClassDefinition) parent).getArgumentListGroovy();
        if (argList == null) {
            return getArgumentTypes(GrNamedArgument.EMPTY_ARRAY, GrExpression.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, nullAsBottom, stopAt);
        } else {
            return getArgumentTypes(argList.getNamedArguments(), argList.getExpressionArguments(), GrClosableBlock.EMPTY_ARRAY, nullAsBottom, stopAt);
        }
    } else if (parent instanceof GrBinaryExpression) {
        GrExpression right = ((GrBinaryExpression) parent).getRightOperand();
        PsiType type = right != null ? right.getType() : null;
        return new PsiType[] { notNullizeType(type, nullAsBottom, parent) };
    } else if (parent instanceof GrAssignmentExpression) {
        PsiType type = ((GrAssignmentExpression) parent).getType();
        return new PsiType[] { notNullizeType(type, nullAsBottom, parent) };
    }
    return null;
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrIndexProperty(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) Nullable(org.jetbrains.annotations.Nullable)

Example 77 with GrArgumentList

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

the class PsiUtil method getAllArguments.

/**
   * Returns all arguments passed to method. First argument is null if Named Arguments is present.
   */
public static GrExpression[] getAllArguments(@NotNull GrCall call) {
    GrArgumentList argumentList = call.getArgumentList();
    if (argumentList == null)
        return GrExpression.EMPTY_ARRAY;
    GrClosableBlock[] closureArguments = call.getClosureArguments();
    GrExpression[] expressionArguments = argumentList.getExpressionArguments();
    GrNamedArgument[] namedArguments = argumentList.getNamedArguments();
    int length = expressionArguments.length + closureArguments.length;
    int k = 0;
    if (namedArguments.length > 0) {
        length++;
        k = 1;
    }
    GrExpression[] res = new GrExpression[length];
    for (GrExpression expressionArgument : expressionArguments) {
        res[k++] = expressionArgument;
    }
    for (GrClosableBlock closureArgument : closureArguments) {
        res[k++] = closureArgument;
    }
    return res;
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)

Example 78 with GrArgumentList

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

the class PsiUtil method getCallByNamedParameter.

@Nullable
public static GrCall getCallByNamedParameter(GrNamedArgument namedArgument) {
    PsiElement parent = namedArgument.getParent();
    PsiElement eMethodCall;
    if (parent instanceof GrArgumentList) {
        eMethodCall = parent.getParent();
    } else {
        if (!(parent instanceof GrListOrMap))
            return null;
        PsiElement eArgumentList = parent.getParent();
        if (!(eArgumentList instanceof GrArgumentList))
            return null;
        GrArgumentList argumentList = (GrArgumentList) eArgumentList;
        if (argumentList.getNamedArguments().length > 0)
            return null;
        if (argumentList.getExpressionArgumentIndex((GrListOrMap) parent) != 0)
            return null;
        eMethodCall = eArgumentList.getParent();
    }
    if (!(eMethodCall instanceof GrCall))
        return null;
    return (GrCall) eMethodCall;
}
Also used : GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) Nullable(org.jetbrains.annotations.Nullable)

Example 79 with GrArgumentList

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

the class ResolveUtil method getSelfOrWithQualifier.

@Nullable
public static GrExpression getSelfOrWithQualifier(GrReferenceExpression ref) {
    final GrExpression qualifier = ref.getQualifierExpression();
    if (qualifier != null) {
        return qualifier;
    }
    PsiElement place = ref;
    while (true) {
        final GrClosableBlock closure = PsiTreeUtil.getParentOfType(place, GrClosableBlock.class, true, GrMember.class, GroovyFile.class);
        if (closure == null)
            break;
        place = closure;
        PsiElement clParent = closure.getParent();
        if (clParent instanceof GrArgumentList)
            clParent = clParent.getParent();
        if (!(clParent instanceof GrMethodCall))
            continue;
        final GrExpression expression = ((GrMethodCall) clParent).getInvokedExpression();
        if (expression instanceof GrReferenceExpression && GdkMethodUtil.isWithName(((GrReferenceExpression) expression).getReferenceName()) && ((GrReferenceExpression) expression).resolve() instanceof GrGdkMethod) {
            final GrExpression withQualifier = ((GrReferenceExpression) expression).getQualifierExpression();
            if (withQualifier != null) {
                return withQualifier;
            }
        }
    }
    return null;
}
Also used : GrGdkMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod) GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 80 with GrArgumentList

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

the class NamedArgumentInsertHandler method handleInsert.

@Override
public void handleInsert(InsertionContext context, LookupElement item) {
    int tailOffset = context.getTailOffset();
    PsiElement argumentList = context.getFile().findElementAt(tailOffset - 1);
    while (argumentList != null && !(argumentList instanceof GrArgumentList) && !(argumentList instanceof GrListOrMap)) {
        argumentList = argumentList.getParent();
    }
    final Editor editor = context.getEditor();
    if (argumentList != null) {
        CodeStyleSettings settings = CodeStyleSettingsManager.getInstance(context.getProject()).getCurrentSettings();
        GroovyCodeStyleSettings codeStyleSettings = settings.getCustomSettings(GroovyCodeStyleSettings.class);
        CommonCodeStyleSettings commonCodeStyleSettings = settings.getCommonSettings(GroovyLanguage.INSTANCE);
        boolean insertSpace = codeStyleSettings.SPACE_IN_NAMED_ARGUMENT;
        if (context.getCompletionChar() == ':' || (insertSpace && context.getCompletionChar() == ' ')) {
            context.setAddCompletionChar(false);
        }
        String argumentListText = argumentList.getText();
        String s = argumentListText.substring(tailOffset - argumentList.getTextOffset());
        s = StringUtil.trimEnd(s, ")");
        if (s.trim().isEmpty()) {
            String toInsert = insertSpace ? ": " : ":";
            editor.getDocument().insertString(tailOffset, toInsert);
            editor.getCaretModel().moveToOffset(tailOffset + toInsert.length());
        } else {
            if (context.getCompletionChar() == Lookup.REPLACE_SELECT_CHAR) {
                char a = s.charAt(0);
                if (Character.isLetterOrDigit(a)) {
                    return;
                }
            }
            Matcher m = Pattern.compile("([ \\t]*):([ \\t]*)(.*)", Pattern.DOTALL).matcher(s);
            if (m.matches()) {
                int caret = tailOffset + m.end(2);
                if (m.group(2).isEmpty()) {
                    editor.getDocument().insertString(caret, " ");
                    caret++;
                }
                editor.getCaretModel().moveToOffset(caret);
            } else {
                m = Pattern.compile("([ \\t]*)([\\n \\t]*)[\\],](.*)", Pattern.DOTALL).matcher(s);
                if (m.matches()) {
                    String toInsert = insertSpace ? ": " : ":";
                    editor.getDocument().replaceString(tailOffset, tailOffset + m.start(2), toInsert);
                    editor.getCaretModel().moveToOffset(tailOffset + toInsert.length());
                } else {
                    m = Pattern.compile("([ \\t]*)(.*)", Pattern.DOTALL).matcher(s);
                    if (!m.matches())
                        throw new RuntimeException("This pattern must match any non-empty string! (" + s + ")");
                    StringBuilder sb = new StringBuilder(3);
                    sb.append(':');
                    int shiftCaret = 1;
                    if (insertSpace) {
                        sb.append(' ');
                        shiftCaret++;
                    }
                    if (!m.group(2).startsWith("\n") && commonCodeStyleSettings.SPACE_AFTER_COMMA) {
                        sb.append(' ');
                    }
                    editor.getDocument().replaceString(tailOffset, tailOffset + m.start(2), sb);
                    editor.getCaretModel().moveToOffset(tailOffset + shiftCaret);
                }
            }
        }
        editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
        editor.getSelectionModel().removeSelection();
    }
}
Also used : Matcher(java.util.regex.Matcher) GroovyCodeStyleSettings(org.jetbrains.plugins.groovy.codeStyle.GroovyCodeStyleSettings) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) CommonCodeStyleSettings(com.intellij.psi.codeStyle.CommonCodeStyleSettings) GroovyCodeStyleSettings(org.jetbrains.plugins.groovy.codeStyle.GroovyCodeStyleSettings) CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) CommonCodeStyleSettings(com.intellij.psi.codeStyle.CommonCodeStyleSettings) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement)

Aggregations

GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)80 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)26 PsiElement (com.intellij.psi.PsiElement)21 Nullable (org.jetbrains.annotations.Nullable)20 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)18 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)15 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)14 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)14 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)14 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)14 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)12 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)9 GrCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall)8 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)8 NotNull (org.jetbrains.annotations.NotNull)7 GrCommandArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList)6 GrLiteral (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)6 GrGdkMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod)6 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)6 ASTNode (com.intellij.lang.ASTNode)5