Search in sources :

Example 21 with GrParameterList

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList 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 22 with GrParameterList

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

the class GrMethodParametersFixer method apply.

@Override
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
    if (psiElement instanceof GrParameterList && psiElement.getParent() instanceof GrMethod) {
        PsiElement rParenth = psiElement.getNextSibling();
        if (rParenth == null)
            return;
        //      [todo] ends with comma
        if (!")".equals(rParenth.getText())) {
            int offset;
            GrParameterList list = (GrParameterList) psiElement;
            final GrParameter[] params = list.getParameters();
            if (params == null || params.length == 0) {
                offset = list.getTextRange().getStartOffset() + 1;
            } else {
                offset = params[params.length - 1].getTextRange().getEndOffset();
            }
            editor.getDocument().insertString(offset, ")");
        }
    }
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) PsiElement(com.intellij.psi.PsiElement)

Aggregations

GrParameterList (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList)22 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)14 PsiElement (com.intellij.psi.PsiElement)12 NotNull (org.jetbrains.annotations.NotNull)7 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)7 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)4 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)4 PsiType (com.intellij.psi.PsiType)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)3 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)3 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)3 ASTNode (com.intellij.lang.ASTNode)2 Document (com.intellij.openapi.editor.Document)2 Nullable (org.jetbrains.annotations.Nullable)2 GrDocComment (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)2 GrDocTag (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocTag)2 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)2 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)2 GrCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall)2