Search in sources :

Example 11 with GrReferenceExpression

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

the class StaticImportInsertHandler method handleInsert.

@Override
public void handleInsert(InsertionContext context, JavaGlobalMemberLookupElement item) {
    GroovyInsertHandler.INSTANCE.handleInsert(context, item);
    final PsiMember member = item.getObject();
    PsiDocumentManager.getInstance(member.getProject()).commitDocument(context.getDocument());
    final GrReferenceExpression ref = PsiTreeUtil.findElementOfClassAtOffset(context.getFile(), context.getStartOffset(), GrReferenceExpression.class, false);
    if (ref != null && ref.getQualifier() == null && context.getFile() instanceof GroovyFile && !importAlreadyExists(member, ((GroovyFile) context.getFile()), ref) && !PsiManager.getInstance(context.getProject()).areElementsEquivalent(ref.resolve(), member)) {
        ref.bindToElementViaStaticImport(member);
    }
}
Also used : GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 12 with GrReferenceExpression

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

the class GeeseUtil method calculateRBraceAlignment.

static void calculateRBraceAlignment(PsiElement rBrace, AlignmentProvider alignments) {
    int leadingBraceCount = 0;
    PsiElement next;
    if (!isClosureContainLF(rBrace))
        return;
    for (next = PsiUtil.getPreviousNonWhitespaceToken(rBrace); isClosureRBrace(next) && isClosureContainLF(next); next = PsiUtil.getPreviousNonWhitespaceToken(next)) {
        leadingBraceCount++;
    }
    PsiElement cur = rBrace;
    for (next = getNextNonWhitespaceToken(cur); isClosureRBrace(next); next = getNextNonWhitespaceToken(cur)) {
        cur = next;
    }
    for (; leadingBraceCount > 0; leadingBraceCount--) {
        cur = PsiUtil.getPreviousNonWhitespaceToken(cur);
    }
    PsiElement parent = cur.getParent();
    LOG.assertTrue(parent instanceof GrClosableBlock);
    //search for start of the line
    cur = parent;
    if (cur.getParent() instanceof GrMethodCall) {
        GrMethodCall call = (GrMethodCall) cur.getParent();
        GrExpression invoked = call.getInvokedExpression();
        if (invoked instanceof GrReferenceExpression && ((GrReferenceExpression) invoked).getReferenceNameElement() != null) {
            cur = ((GrReferenceExpression) invoked).getReferenceNameElement();
        } else {
            cur = call;
        }
    }
    cur = PsiTreeUtil.getDeepestFirst(cur);
    while (!PsiUtil.isNewLine(next = PsiTreeUtil.prevLeaf(cur, true))) {
        if (next == null)
            break;
        if (next.getNode().getElementType() == TokenType.WHITE_SPACE && PsiTreeUtil.prevLeaf(next) == null) {
            //if cur is first word in the text, whitespace could be before it
            break;
        }
        cur = next;
    }
    int startOffset = cur.getTextRange().getStartOffset();
    int endOffset = rBrace.getTextRange().getStartOffset();
    if (rBrace.getContainingFile().getText().substring(startOffset, endOffset).indexOf('\n') < 0) {
        return;
    }
    while (true) {
        final PsiElement p = cur.getParent();
        if (p != null && p.getTextOffset() == cur.getTextOffset()) {
            cur = p;
        } else {
            break;
        }
    }
    alignments.addPair(rBrace, cur, true);
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 13 with GrReferenceExpression

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

the class GrMainCompletionProvider method completeReference.

@NotNull
static Runnable completeReference(final CompletionParameters parameters, final GrReferenceElement reference, final JavaCompletionSession inheritorsHolder, final PrefixMatcher matcher, final Consumer<LookupElement> _consumer) {
    final Consumer<LookupElement> consumer = new Consumer<LookupElement>() {

        final Set<LookupElement> added = ContainerUtil.newHashSet();

        @Override
        public void consume(LookupElement element) {
            if (added.add(element)) {
                _consumer.consume(element);
            }
        }
    };
    final Map<PsiModifierListOwner, LookupElement> staticMembers = ContainerUtil.newHashMap();
    final PsiElement qualifier = reference.getQualifier();
    final PsiType qualifierType = GroovyCompletionUtil.getQualifierType(qualifier);
    if (reference instanceof GrReferenceExpression && (qualifier instanceof GrExpression || qualifier == null)) {
        for (String string : CompleteReferencesWithSameQualifier.getVariantsWithSameQualifier((GrReferenceExpression) reference, matcher, (GrExpression) qualifier)) {
            consumer.consume(LookupElementBuilder.create(string).withItemTextUnderlined(true));
        }
        if (parameters.getInvocationCount() < 2 && qualifier != null && qualifierType == null && !(qualifier instanceof GrReferenceExpression && ((GrReferenceExpression) qualifier).resolve() instanceof PsiPackage)) {
            if (parameters.getInvocationCount() == 1) {
                showInfo();
            }
            return EmptyRunnable.INSTANCE;
        }
    }
    final List<LookupElement> zeroPriority = ContainerUtil.newArrayList();
    PsiClass qualifierClass = com.intellij.psi.util.PsiUtil.resolveClassInClassTypeOnly(qualifierType);
    final boolean honorExcludes = qualifierClass == null || !JavaCompletionUtil.isInExcludedPackage(qualifierClass, false);
    GroovyCompletionUtil.processVariants(reference, matcher, parameters, lookupElement -> {
        Object object = lookupElement.getObject();
        if (object instanceof GroovyResolveResult) {
            object = ((GroovyResolveResult) object).getElement();
        }
        if (isLightElementDeclaredDuringCompletion(object)) {
            return;
        }
        if (!(lookupElement instanceof LookupElementBuilder) && inheritorsHolder.alreadyProcessed(lookupElement)) {
            return;
        }
        if (honorExcludes && object instanceof PsiMember && JavaCompletionUtil.isInExcludedPackage((PsiMember) object, true)) {
            return;
        }
        if (!(object instanceof PsiClass)) {
            int priority = assignPriority(lookupElement, qualifierType);
            lookupElement = JavaCompletionUtil.highlightIfNeeded(qualifierType, PrioritizedLookupElement.withPriority(lookupElement, priority), object, reference);
        }
        if ((object instanceof PsiMethod || object instanceof PsiField) && ((PsiModifierListOwner) object).hasModifierProperty(PsiModifier.STATIC)) {
            if (lookupElement.getLookupString().equals(((PsiMember) object).getName())) {
                staticMembers.put(CompletionUtil.getOriginalOrSelf((PsiModifierListOwner) object), lookupElement);
            }
        }
        PrioritizedLookupElement prio = lookupElement.as(PrioritizedLookupElement.CLASS_CONDITION_KEY);
        if (prio == null || prio.getPriority() == 0) {
            zeroPriority.add(lookupElement);
        } else {
            consumer.consume(lookupElement);
        }
    });
    for (LookupElement element : zeroPriority) {
        consumer.consume(element);
    }
    if (qualifier == null) {
        return addStaticMembers(parameters, matcher, staticMembers, consumer);
    }
    return EmptyRunnable.INSTANCE;
}
Also used : Set(java.util.Set) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) LookupElement(com.intellij.codeInsight.lookup.LookupElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) PairConsumer(com.intellij.util.PairConsumer) Consumer(com.intellij.util.Consumer) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with GrReferenceExpression

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

the class GrMethodCallWithSingleClosureArgFixer method apply.

@Override
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
    final PsiElement parent = psiElement.getParent();
    if (parent instanceof GrReferenceExpression && !(parent.getParent() instanceof GrMethodCall) && hasOnlyClosureParam((GrReferenceExpression) parent)) {
        final int endOffset = psiElement.getTextRange().getEndOffset();
        editor.getDocument().insertString(endOffset, "{\n}");
    }
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 15 with GrReferenceExpression

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

the class GrCallReferenceProcessor method process.

@Override
public boolean process(@NotNull PsiReference reference, @NotNull JavaCallHierarchyData data) {
    PsiClass originalClass = data.getOriginalClass();
    PsiMethod method = data.getMethod();
    Set<PsiMethod> methodsToFind = data.getMethodsToFind();
    PsiMethod methodToFind = data.getMethodToFind();
    PsiClassType originalType = data.getOriginalType();
    Map<PsiMember, NodeDescriptor> methodToDescriptorMap = data.getResultMap();
    Project project = data.getProject();
    if (reference instanceof GrReferenceExpression) {
        final GrExpression qualifier = ((GrReferenceExpression) reference).getQualifierExpression();
        if (org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.isSuperReference(qualifier)) {
            // filter super.foo() call inside foo() and similar cases (bug 8411)
            assert qualifier != null;
            final PsiClass superClass = PsiUtil.resolveClassInType(qualifier.getType());
            if (originalClass == null || originalClass.isInheritor(superClass, true)) {
                return true;
            }
        }
        if (qualifier != null && !methodToFind.hasModifierProperty(PsiModifier.STATIC)) {
            final PsiType qualifierType = qualifier.getType();
            if (qualifierType instanceof PsiClassType && !TypeConversionUtil.isAssignable(qualifierType, originalType) && methodToFind != method) {
                final PsiClass psiClass = ((PsiClassType) qualifierType).resolve();
                if (psiClass != null) {
                    final PsiMethod callee = psiClass.findMethodBySignature(methodToFind, true);
                    if (callee != null && !methodsToFind.contains(callee)) {
                        // skip sibling methods
                        return true;
                    }
                }
            }
        }
    } else {
        if (!(reference instanceof PsiElement)) {
            return true;
        }
        final PsiElement parent = ((PsiElement) reference).getParent();
        if (parent instanceof PsiNewExpression) {
            if (((PsiNewExpression) parent).getClassReference() != reference) {
                return true;
            }
        } else if (parent instanceof GrAnonymousClassDefinition) {
            if (((GrAnonymousClassDefinition) parent).getBaseClassReferenceGroovy() != reference) {
                return true;
            }
        } else {
            return true;
        }
    }
    final PsiElement element = reference.getElement();
    final PsiMember key = CallHierarchyNodeDescriptor.getEnclosingElement(element);
    synchronized (methodToDescriptorMap) {
        CallHierarchyNodeDescriptor d = (CallHierarchyNodeDescriptor) methodToDescriptorMap.get(key);
        if (d == null) {
            d = new CallHierarchyNodeDescriptor(project, (CallHierarchyNodeDescriptor) data.getNodeDescriptor(), element, false, true);
            methodToDescriptorMap.put(key, d);
        } else if (!d.hasReference(reference)) {
            d.incrementUsageCount();
        }
        d.addReference(reference);
    }
    return true;
}
Also used : GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) CallHierarchyNodeDescriptor(com.intellij.ide.hierarchy.call.CallHierarchyNodeDescriptor) NodeDescriptor(com.intellij.ide.util.treeView.NodeDescriptor) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) Project(com.intellij.openapi.project.Project) CallHierarchyNodeDescriptor(com.intellij.ide.hierarchy.call.CallHierarchyNodeDescriptor)

Aggregations

GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)177 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)92 PsiElement (com.intellij.psi.PsiElement)56 Nullable (org.jetbrains.annotations.Nullable)28 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)27 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)26 NotNull (org.jetbrains.annotations.NotNull)25 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)22 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)22 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)21 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)17 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)16 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)15 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)14 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)14 PsiType (com.intellij.psi.PsiType)13 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)12 Project (com.intellij.openapi.project.Project)11 ArrayList (java.util.ArrayList)11 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)11