Search in sources :

Example 21 with GrExpression

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

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

the class GrIfConditionFixer method apply.

@Override
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
    if (psiElement instanceof GrIfStatement) {
        final Document doc = editor.getDocument();
        final GrIfStatement ifStatement = (GrIfStatement) psiElement;
        final PsiElement rParen = ifStatement.getRParenth();
        final PsiElement lParen = ifStatement.getLParenth();
        final GrExpression condition = ifStatement.getCondition();
        if (condition == null) {
            if (lParen == null || rParen == null) {
                int stopOffset = doc.getLineEndOffset(doc.getLineNumber(ifStatement.getTextRange().getStartOffset()));
                final GrStatement then = ifStatement.getThenBranch();
                if (then != null) {
                    stopOffset = Math.min(stopOffset, then.getTextRange().getStartOffset());
                }
                stopOffset = Math.min(stopOffset, ifStatement.getTextRange().getEndOffset());
                doc.replaceString(ifStatement.getTextRange().getStartOffset(), stopOffset, "if ()");
                processor.registerUnresolvedError(ifStatement.getTextRange().getStartOffset() + "if (".length());
            } else {
                processor.registerUnresolvedError(lParen.getTextRange().getEndOffset());
            }
        } else if (rParen == null) {
            doc.insertString(condition.getTextRange().getEndOffset(), ")");
        }
    }
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 23 with GrExpression

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

the class GrMethodCallFixer method apply.

@Override
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
    final GrArgumentList argList = psiElement instanceof GrCall ? ((GrCall) psiElement).getArgumentList() : null;
    if (argList == null || argList instanceof GrCommandArgumentList)
        return;
    GrCall call = (GrCall) psiElement;
    PsiElement parenth = argList.getLastChild();
    if (parenth != null && ")".equals(parenth.getText()) || PsiImplUtil.hasClosureArguments(call))
        return;
    int endOffset = -1;
    for (PsiElement child = argList.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (!(child instanceof PsiErrorElement))
            continue;
        final PsiErrorElement errorElement = (PsiErrorElement) child;
        if (errorElement.getErrorDescription().contains("')'")) {
            endOffset = errorElement.getTextRange().getStartOffset();
            break;
        }
    }
    if (endOffset == -1) {
        endOffset = argList.getTextRange().getEndOffset();
    }
    final GrExpression[] params = argList.getExpressionArguments();
    if (params.length > 0 && GrForBodyFixer.startLine(editor.getDocument(), argList) != GrForBodyFixer.startLine(editor.getDocument(), params[0])) {
        endOffset = argList.getTextRange().getStartOffset() + 1;
    }
    endOffset = CharArrayUtil.shiftBackward(editor.getDocument().getCharsSequence(), endOffset - 1, " \t\n") + 1;
    editor.getDocument().insertString(endOffset, ")");
}
Also used : GrCommandArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList) PsiErrorElement(com.intellij.psi.PsiErrorElement) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElement(com.intellij.psi.PsiElement)

Example 24 with GrExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression 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)

Example 25 with GrExpression

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

the class ConvertConcatenationToGstringIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final PsiFile file = element.getContainingFile();
    final int offset = editor.getCaretModel().getOffset();
    final AccessToken accessToken = ReadAction.start();
    final List<GrExpression> expressions;
    try {
        expressions = collectExpressions(file, offset);
    } finally {
        accessToken.finish();
    }
    final Document document = editor.getDocument();
    if (expressions.size() == 1) {
        invokeImpl(expressions.get(0), document);
    } else if (!expressions.isEmpty()) {
        if (ApplicationManager.getApplication().isUnitTestMode()) {
            invokeImpl(expressions.get(expressions.size() - 1), document);
            return;
        }
        IntroduceTargetChooser.showChooser(editor, expressions, new Pass<GrExpression>() {

            @Override
            public void pass(final GrExpression selectedValue) {
                invokeImpl(selectedValue, document);
            }
        }, grExpression -> grExpression.getText());
    }
}
Also used : TypeConversionUtil(com.intellij.psi.util.TypeConversionUtil) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) WriteAction(com.intellij.openapi.application.WriteAction) GrLiteralImpl(org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.literals.GrLiteralImpl) org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals) Document(com.intellij.openapi.editor.Document) ReadAction(com.intellij.openapi.application.ReadAction) ArrayList(java.util.ArrayList) AccessToken(com.intellij.openapi.application.AccessToken) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) GroovyCommonClassNames(org.jetbrains.plugins.groovy.lang.psi.util.GroovyCommonClassNames) Intention(org.jetbrains.plugins.groovy.intentions.base.Intention) Project(com.intellij.openapi.project.Project) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) IntroduceTargetChooser(com.intellij.refactoring.IntroduceTargetChooser) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) GroovyTokenTypes(org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrStringUtil(org.jetbrains.plugins.groovy.lang.psi.util.GrStringUtil) MethodSignatureUtil(com.intellij.psi.util.MethodSignatureUtil) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) TypesUtil(org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.TypesUtil) Editor(com.intellij.openapi.editor.Editor) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) CommandProcessor(com.intellij.openapi.command.CommandProcessor) Nullable(org.jetbrains.annotations.Nullable) GrBinaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression) List(java.util.List) Pass(com.intellij.openapi.util.Pass) ErrorUtil(org.jetbrains.plugins.groovy.lang.psi.util.ErrorUtil) ApplicationManager(com.intellij.openapi.application.ApplicationManager) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) PsiUtil(org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil) Pass(com.intellij.openapi.util.Pass) AccessToken(com.intellij.openapi.application.AccessToken) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Document(com.intellij.openapi.editor.Document)

Aggregations

GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)312 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)93 Nullable (org.jetbrains.annotations.Nullable)68 PsiElement (com.intellij.psi.PsiElement)62 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)43 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)43 NotNull (org.jetbrains.annotations.NotNull)37 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)35 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)33 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)29 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)27 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)26 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)26 PsiType (com.intellij.psi.PsiType)24 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)23 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)23 IElementType (com.intellij.psi.tree.IElementType)22 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)18 ArrayList (java.util.ArrayList)16 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)16