Search in sources :

Example 6 with GrReferenceElement

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

the class GroovyRefactoringSupportProvider method isMemberInplaceRenameAvailable.

@Override
public boolean isMemberInplaceRenameAvailable(@NotNull PsiElement element, @Nullable PsiElement context) {
    if (context == null || context.getContainingFile() instanceof GroovyFile)
        return false;
    PsiElement parent = context.getParent();
    //don't try to inplace rename aliased imported references
    if (parent instanceof GrReferenceElement) {
        GroovyResolveResult result = ((GrReferenceElement) parent).advancedResolve();
        PsiElement fileResolveContext = result.getCurrentFileResolveContext();
        if (fileResolveContext instanceof GrImportStatement && ((GrImportStatement) fileResolveContext).isAliasedImport()) {
            return false;
        }
    }
    return element instanceof GrMember;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) PsiElement(com.intellij.psi.PsiElement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)

Example 7 with GrReferenceElement

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

the class GroovyCompletionData method isControlStructure.

private static boolean isControlStructure(PsiElement context) {
    final int offset = context.getTextRange().getStartOffset();
    PsiElement prevSibling = context.getPrevSibling();
    if (context.getParent() instanceof GrReferenceElement && prevSibling != null && prevSibling.getNode() != null) {
        ASTNode node = prevSibling.getNode();
        return !TokenSets.DOTS.contains(node.getElementType());
    }
    if (GroovyCompletionUtil.isNewStatement(context, true)) {
        final PsiElement leaf = GroovyCompletionUtil.getLeafByOffset(offset - 1, context);
        if (leaf != null && (leaf.getParent() instanceof GrStatementOwner || leaf.getParent() instanceof GrLabeledStatement)) {
            return true;
        }
    }
    if (context.getParent() != null) {
        PsiElement parent = context.getParent();
        if (parent instanceof GrExpression && parent.getParent() instanceof GroovyFile) {
            return true;
        }
        if (parent instanceof GrReferenceExpression) {
            PsiElement superParent = parent.getParent();
            if (superParent instanceof GrStatementOwner || superParent instanceof GrLabeledStatement || superParent instanceof GrControlStatement || superParent instanceof GrMethodCall) {
                return true;
            }
        }
        return false;
    }
    return false;
}
Also used : ASTNode(com.intellij.lang.ASTNode) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) GrControlStatement(org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 8 with GrReferenceElement

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

the class GroovyCompletionData method suggestPrimitiveTypes.

private static boolean suggestPrimitiveTypes(PsiElement context) {
    if (isInfixOperatorPosition(context))
        return false;
    if (isAfterForParameter(context))
        return false;
    final PsiElement parent = context.getParent();
    if (parent == null)
        return false;
    PsiElement previous = PsiImplUtil.realPrevious(parent.getPrevSibling());
    if (parent instanceof GrReferenceElement && parent.getParent() instanceof GrArgumentList) {
        PsiElement prevSibling = context.getPrevSibling();
        if (prevSibling != null && prevSibling.getNode() != null) {
            if (!TokenSets.DOTS.contains(prevSibling.getNode().getElementType())) {
                return true;
            }
        } else if (!(previous != null && GroovyTokenTypes.mAT.equals(previous.getNode().getElementType()))) {
            return true;
        }
    }
    if (GroovyCompletionUtil.isTupleVarNameWithoutTypeDeclared(context))
        return true;
    if (previous != null && GroovyTokenTypes.mAT.equals(previous.getNode().getElementType())) {
        return false;
    }
    if (GroovyCompletionUtil.asSimpleVariable(context) || GroovyCompletionUtil.asTypedMethod(context) || GroovyCompletionUtil.asVariableInBlock(context) || asVariableAfterModifiers(context)) {
        return true;
    }
    if ((parent instanceof GrParameter && ((GrParameter) parent).getTypeElementGroovy() == null) || parent instanceof GrReferenceElement && !(parent.getParent() instanceof GrImportStatement) && !(parent.getParent() instanceof GrPackageDefinition) && !(parent.getParent() instanceof GrArgumentList)) {
        PsiElement prevSibling = context.getPrevSibling();
        if (parent instanceof GrReferenceElement && prevSibling != null && prevSibling.getNode() != null) {
            ASTNode node = prevSibling.getNode();
            return !TokenSets.DOTS.contains(node.getElementType());
        } else {
            return true;
        }
    }
    if (PsiImplUtil.realPrevious(parent.getPrevSibling()) instanceof GrModifierList) {
        return true;
    }
    if (PsiImplUtil.realPrevious(context.getPrevSibling()) instanceof GrModifierList) {
        return true;
    }
    return parent instanceof GrExpression && parent.getParent() instanceof GroovyFile && GroovyCompletionUtil.isNewStatement(context, false);
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) ASTNode(com.intellij.lang.ASTNode) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 9 with GrReferenceElement

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

the class GrMainCompletionProvider method addCompletions.

@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull final CompletionResultSet result) {
    GroovyCompletionData.addGroovyDocKeywords(parameters, result);
    PsiElement position = parameters.getPosition();
    if (PlatformPatterns.psiElement().inside(false, PlatformPatterns.psiElement(PsiComment.class)).accepts(position)) {
        return;
    }
    GroovyCompletionData.addGroovyKeywords(parameters, result);
    addUnfinishedMethodTypeParameters(position, result);
    suggestVariableNames(position, result);
    GrReferenceElement reference = findGroovyReference(position);
    if (reference == null) {
        if (parameters.getInvocationCount() >= 2) {
            result.stopHere();
            addAllClasses(parameters, result.withPrefixMatcher(CompletionUtil.findJavaIdentifierPrefix(parameters)), new JavaCompletionSession(result));
        }
        return;
    }
    if (reference.getParent() instanceof GrImportStatement && reference.getQualifier() != null) {
        result.addElement(LookupElementBuilder.create("*"));
    }
    JavaCompletionSession inheritors = new JavaCompletionSession(result);
    if (GroovySmartCompletionContributor.AFTER_NEW.accepts(position)) {
        GroovySmartCompletionContributor.generateInheritorVariants(parameters, result.getPrefixMatcher(), inheritors);
    }
    Runnable addSlowVariants = completeReference(parameters, reference, inheritors, result.getPrefixMatcher(), lookupElement -> result.addElement(lookupElement));
    if (reference.getQualifier() == null) {
        if (!GroovySmartCompletionContributor.AFTER_NEW.accepts(position)) {
            GroovySmartCompletionContributor.addExpectedClassMembers(parameters, result);
        }
        if (isClassNamePossible(position) && JavaCompletionContributor.mayStartClassName(result)) {
            result.stopHere();
            if (parameters.getInvocationCount() >= 2) {
                addAllClasses(parameters, result, inheritors);
            } else {
                JavaCompletionContributor.advertiseSecondCompletion(position.getProject(), result);
            }
        }
    }
    addSlowVariants.run();
}
Also used : EmptyRunnable(com.intellij.openapi.util.EmptyRunnable) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)

Example 10 with GrReferenceElement

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

the class ImportOnDemandIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    if (!(element instanceof GrReferenceElement))
        return;
    final GrReferenceElement ref = (GrReferenceElement) element;
    final PsiElement resolved = ref.resolve();
    if (!(resolved instanceof PsiClass))
        return;
    final String qname = ((PsiClass) resolved).getQualifiedName();
    final GrImportStatement importStatement = GroovyPsiElementFactory.getInstance(project).createImportStatementFromText(qname, true, true, null);
    final PsiFile containingFile = element.getContainingFile();
    if (!(containingFile instanceof GroovyFile))
        return;
    ((GroovyFile) containingFile).addImport(importStatement);
    for (PsiReference reference : ReferencesSearch.search(resolved, new LocalSearchScope(containingFile))) {
        final PsiElement refElement = reference.getElement();
        if (refElement == null)
            continue;
        final PsiElement parent = refElement.getParent();
        if (parent instanceof GrQualifiedReference<?>) {
            org.jetbrains.plugins.groovy.codeStyle.GrReferenceAdjuster.shortenReference((GrQualifiedReference<?>) parent);
        }
    }
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) PsiClass(com.intellij.psi.PsiClass) PsiReference(com.intellij.psi.PsiReference) PsiFile(com.intellij.psi.PsiFile) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) PsiElement(com.intellij.psi.PsiElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) GrQualifiedReference(org.jetbrains.plugins.groovy.lang.psi.GrQualifiedReference)

Aggregations

GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)25 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)10 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)6 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)6 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)6 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)5 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)5 PsiElement (com.intellij.psi.PsiElement)4 IncorrectOperationException (com.intellij.util.IncorrectOperationException)4 NotNull (org.jetbrains.annotations.NotNull)4 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)4 Nullable (org.jetbrains.annotations.Nullable)3 GroovyFileBase (org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase)3 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)3 GrPackageDefinition (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition)3 ReferenceData (com.intellij.codeInsight.editorActions.ReferenceData)2 ASTNode (com.intellij.lang.ASTNode)2 Document (com.intellij.openapi.editor.Document)2 Project (com.intellij.openapi.project.Project)2 TextRange (com.intellij.openapi.util.TextRange)2