Search in sources :

Example 91 with GroovyPsiElement

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

the class ControlFlowBuilder method buildFlowForClosure.

private void buildFlowForClosure(final GrClosableBlock closure) {
    for (GrParameter parameter : closure.getAllParameters()) {
        if (myPolicy.isVariableInitialized(parameter)) {
            addNode(new ReadWriteVariableInstruction(parameter.getName(), parameter, ReadWriteVariableInstruction.WRITE));
        }
    }
    addNode(new ReadWriteVariableInstruction("owner", closure.getLBrace(), ReadWriteVariableInstruction.WRITE));
    PsiElement child = closure.getFirstChild();
    while (child != null) {
        if (child instanceof GroovyPsiElement) {
            ((GroovyPsiElement) child).accept(this);
        }
        child = child.getNextSibling();
    }
    final GrStatement[] statements = closure.getStatements();
    if (statements.length > 0) {
        handlePossibleReturn(statements[statements.length - 1]);
    }
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 92 with GroovyPsiElement

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

the class GroovyClassNameInsertHandler method handleInsert.

@Override
public void handleInsert(InsertionContext context, JavaPsiClassReferenceElement item) {
    PsiFile file = context.getFile();
    Editor editor = context.getEditor();
    int endOffset = editor.getCaretModel().getOffset();
    if (PsiTreeUtil.findElementOfClassAtOffset(file, endOffset - 1, GrImportStatement.class, false) != null || !(file instanceof GroovyFileBase)) {
        AllClassesGetter.INSERT_FQN.handleInsert(context, item);
        return;
    }
    PsiDocumentManager.getInstance(context.getProject()).commitDocument(editor.getDocument());
    PsiElement position = file.findElementAt(endOffset - 1);
    boolean parens = shouldInsertParentheses(position);
    final PsiClass psiClass = item.getObject();
    if (isInVariable(position) || GroovyCompletionUtil.isInPossibleClosureParameter(position)) {
        Project project = context.getProject();
        String qname = psiClass.getQualifiedName();
        String shortName = psiClass.getName();
        if (qname == null)
            return;
        PsiClass aClass = JavaPsiFacade.getInstance(project).getResolveHelper().resolveReferencedClass(shortName, position);
        if (aClass == null) {
            ((GroovyFileBase) file).addImportForClass(psiClass);
            return;
        } else if (aClass == CompletionUtil.getOriginalOrSelf(psiClass)) {
            return;
        }
    }
    AllClassesGetter.TRY_SHORTENING.handleInsert(context, item);
    if (parens && context.getCompletionChar() != '[') {
        int identifierEnd = context.getTailOffset();
        GroovyPsiElement place = PsiTreeUtil.findElementOfClassAtOffset(context.getFile(), context.getStartOffset(), GroovyPsiElement.class, false);
        JavaCompletionUtil.insertParentheses(context, item, false, place != null && GroovyCompletionUtil.hasConstructorParameters(psiClass, place));
        if (context.getCompletionChar() == '<' || psiClass.hasTypeParameters() && context.getCompletionChar() != '(') {
            context.setAddCompletionChar(false);
            JavaCompletionUtil.promptTypeArgs(context, identifierEnd);
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Editor(com.intellij.openapi.editor.Editor) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 93 with GroovyPsiElement

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

the class GroovyParameterInfoHandler method findAnchorElement.

@Nullable
private static GroovyPsiElement findAnchorElement(int offset, PsiFile file) {
    PsiElement element = file.findElementAt(offset);
    if (element == null)
        return null;
    GroovyPsiElement argList = PsiTreeUtil.getParentOfType(element, GrArgumentList.class);
    if (argList != null)
        return argList;
    final GrCall call = PsiTreeUtil.getParentOfType(element, GrCall.class);
    if (call != null) {
        argList = call.getArgumentList();
        if (argList != null && argList.getTextRange().contains(element.getTextRange().getStartOffset()))
            return argList;
    } else {
        offset = CharArrayUtil.shiftBackward(file.getText(), offset, "\n\t ");
        if (offset <= 0)
            return null;
        element = file.findElementAt(offset);
        if (element != null && element.getParent() instanceof GrReferenceExpression)
            return (GroovyPsiElement) element.getParent();
    }
    return null;
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) 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 94 with GroovyPsiElement

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

the class GroovyParameterInfoHandler method updateParameterInfo.

@Override
public void updateParameterInfo(@NotNull GroovyPsiElement place, @NotNull UpdateParameterInfoContext context) {
    final PsiElement parameterOwner = context.getParameterOwner();
    if (parameterOwner != place) {
        context.removeHint();
        return;
    }
    int offset = context.getEditor().getCaretModel().getOffset();
    offset = CharArrayUtil.shiftForward(context.getEditor().getDocument().getText(), offset, " \t\n");
    final int currIndex = getCurrentParameterIndex(place, offset);
    context.setCurrentParameter(currIndex);
    final Object[] objects = context.getObjectsToView();
    Outer: for (int i = 0; i < objects.length; i++) {
        PsiType[] parameterTypes = null;
        PsiType[] argTypes = null;
        PsiSubstitutor substitutor = null;
        if (objects[i] instanceof GroovyResolveResult) {
            final GroovyResolveResult resolveResult = (GroovyResolveResult) objects[i];
            PsiNamedElement namedElement = (PsiNamedElement) resolveResult.getElement();
            if (namedElement instanceof GrReflectedMethod)
                namedElement = ((GrReflectedMethod) namedElement).getBaseMethod();
            substitutor = resolveResult.getSubstitutor();
            assert namedElement != null;
            if (!namedElement.isValid()) {
                context.setUIComponentEnabled(i, false);
                continue Outer;
            }
            if (namedElement instanceof PsiMethod) {
                final PsiMethod method = (PsiMethod) namedElement;
                PsiParameter[] parameters = method.getParameterList().getParameters();
                parameters = updateConstructorParams(method, parameters, context.getParameterOwner());
                parameterTypes = PsiType.createArray(parameters.length);
                for (int j = 0; j < parameters.length; j++) {
                    parameterTypes[j] = parameters[j].getType();
                }
                argTypes = PsiUtil.getArgumentTypes(place, false);
            }
            if (argTypes == null)
                continue;
        } else if (objects[i] instanceof GrClosureSignature) {
            final GrClosureSignature signature = (GrClosureSignature) objects[i];
            argTypes = PsiUtil.getArgumentTypes(place, false);
            parameterTypes = PsiType.createArray(signature.getParameterCount());
            int j = 0;
            for (GrClosureParameter parameter : signature.getParameters()) {
                parameterTypes[j++] = parameter.getType();
            }
        } else {
            continue Outer;
        }
        assert argTypes != null;
        if (argTypes.length > currIndex) {
            if (parameterTypes.length <= currIndex) {
                context.setUIComponentEnabled(i, false);
                continue;
            } else {
                for (int j = 0; j < currIndex; j++) {
                    PsiType argType = argTypes[j];
                    final PsiType paramType = substitutor != null ? substitutor.substitute(parameterTypes[j]) : parameterTypes[j];
                    if (!TypesUtil.isAssignableByMethodCallConversion(paramType, argType, place)) {
                        context.setUIComponentEnabled(i, false);
                        break Outer;
                    }
                }
            }
        }
        context.setUIComponentEnabled(i, true);
    }
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrReflectedMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrReflectedMethod) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GrClosureParameter(org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 95 with GroovyPsiElement

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

the class GroovyParameterInfoHandler method showParameterInfo.

@Override
@SuppressWarnings("unchecked")
public void showParameterInfo(@NotNull GroovyPsiElement place, @NotNull CreateParameterInfoContext context) {
    GroovyResolveResult[] variants = ResolveUtil.getCallVariants(place);
    final List elementToShow = new ArrayList();
    final PsiElement parent = place.getParent();
    if (parent instanceof GrMethodCall) {
        final GrExpression invoked = ((GrMethodCall) parent).getInvokedExpression();
        if (isPropertyOrVariableInvoked(invoked)) {
            final PsiType type = invoked.getType();
            if (type instanceof GrClosureType) {
                addSignatureVariant(elementToShow, (GrClosureType) type);
            } else if (type != null) {
                addMethodAndClosureVariants(elementToShow, ResolveUtil.getMethodCandidates(type, "call", invoked, PsiUtil.getArgumentTypes(place, true)));
            }
        } else {
            addMethodAndClosureVariants(elementToShow, variants);
        }
    } else {
        elementToShow.addAll(Arrays.asList(variants));
    }
    filterOutReflectedMethods(elementToShow);
    context.setItemsToShow(ArrayUtil.toObjectArray(elementToShow));
    context.showHint(place, place.getTextRange().getStartOffset(), this);
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrClosureType(org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType)

Aggregations

GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)98 PsiElement (com.intellij.psi.PsiElement)34 Nullable (org.jetbrains.annotations.Nullable)17 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)17 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)16 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)13 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)12 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)11 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)9 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)8 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)8 GrApplicationStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement)8 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)8 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)8 Project (com.intellij.openapi.project.Project)7 TextRange (com.intellij.openapi.util.TextRange)7 NotNull (org.jetbrains.annotations.NotNull)7 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)7 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)7 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)6