Search in sources :

Example 1 with GrReferenceList

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

the class GrClassImplUtil method getInnerClassesForResolve.

@NotNull
private static List<PsiClass> getInnerClassesForResolve(@NotNull final GrTypeDefinition grType, @Nullable final PsiElement lastParent, @NotNull final PsiElement place) {
    if (lastParent instanceof GrReferenceList || PsiTreeUtil.getParentOfType(place, GrReferenceList.class) != null) {
        return Arrays.asList(grType.getCodeInnerClasses());
    }
    boolean includeSynthetic = !PsiTreeUtil.isContextAncestor(grType, place, true);
    Object key = Trinity.create(grType, lastParent, place);
    List<PsiClass> classes = RecursionManager.doPreventingRecursion(key, false, () -> {
        List<PsiClass> result = new ArrayList<>();
        for (CandidateInfo info : CollectClassMembersUtil.getAllInnerClasses(grType, includeSynthetic).values()) {
            final PsiClass inner = (PsiClass) info.getElement();
            final PsiClass containingClass = inner.getContainingClass();
            assert containingClass != null;
            if (lastParent == null || !containingClass.isInterface() || PsiTreeUtil.isAncestor(containingClass, place, false)) {
                ContainerUtil.addIfNotNull(result, inner);
            }
        }
        return result;
    });
    if (classes == null) {
        return Arrays.asList(grType.getCodeInnerClasses());
    }
    return classes;
}
Also used : CandidateInfo(com.intellij.psi.infos.CandidateInfo) GrReferenceList(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrReferenceList) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with GrReferenceList

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

the class GrHighlightHandlerFactory method createHighlightUsagesHandler.

@Override
public HighlightUsagesHandlerBase createHighlightUsagesHandler(@NotNull Editor editor, @NotNull PsiFile file, @NotNull PsiElement target) {
    ASTNode node = target.getNode();
    if (node == null)
        return null;
    IElementType type = node.getElementType();
    if (type == GroovyTokenTypes.kIMPLEMENTS || type == GroovyTokenTypes.kEXTENDS) {
        PsiElement parent = target.getParent();
        if (!(parent instanceof GrReferenceList))
            return null;
        PsiElement grand = parent.getParent();
        if (!(grand instanceof GrTypeDefinition))
            return null;
        return new GrHighlightOverridingMethodsHandler(editor, file, target, (GrTypeDefinition) grand);
    } else if (type == GroovyTokenTypes.kRETURN || type == GroovyTokenTypes.kTHROW) {
        return new GrHighlightExitPointHandler(editor, file, target);
    }
    return null;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) ASTNode(com.intellij.lang.ASTNode) GrReferenceList(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrReferenceList) PsiElement(com.intellij.psi.PsiElement)

Example 3 with GrReferenceList

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

the class GrHighlightOverridingMethodsHandler method getTargets.

@Override
public List<PsiClass> getTargets() {
    GrReferenceList list = GroovyTokenTypes.kEXTENDS == myTarget.getNode().getElementType() ? myClass.getExtendsClause() : myClass.getImplementsClause();
    if (list == null)
        return Collections.emptyList();
    final PsiClassType[] classTypes = list.getReferencedTypes();
    return ChooseClassAndDoHighlightRunnable.resolveClasses(classTypes);
}
Also used : PsiClassType(com.intellij.psi.PsiClassType) GrReferenceList(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrReferenceList)

Example 4 with GrReferenceList

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

the class AddClassToExtendsFix method doFix.

@Override
protected void doFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) throws IncorrectOperationException {
    GrReferenceList list;
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    final PsiClass comparable = JavaPsiFacade.getInstance(project).findClass(CommonClassNames.JAVA_LANG_COMPARABLE, myPsiClass.getResolveScope());
    PsiSubstitutor substitutor = PsiSubstitutor.EMPTY;
    boolean addTypeParam = false;
    if (comparable != null) {
        final PsiTypeParameter[] typeParameters = comparable.getTypeParameters();
        if (typeParameters.length == 1) {
            final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project);
            final PsiTypeParameter[] classParams = myPsiClass.getTypeParameters();
            PsiSubstitutor innerSubstitutor = PsiSubstitutor.EMPTY;
            for (PsiTypeParameter classParam : classParams) {
                innerSubstitutor = innerSubstitutor.put(classParam, elementFactory.createType(classParam));
            }
            substitutor = substitutor.put(typeParameters[0], elementFactory.createType(myPsiClass, innerSubstitutor));
            addTypeParam = true;
        }
    }
    if (!InheritanceUtil.isInheritor(myPsiClass, CommonClassNames.JAVA_LANG_COMPARABLE)) {
        if (myPsiClass.isInterface()) {
            list = myPsiClass.getExtendsClause();
            if (list == null) {
                list = factory.createExtendsClause();
                PsiElement anchor = myPsiClass.getImplementsClause();
                if (anchor == null) {
                    anchor = myPsiClass.getBody();
                }
                if (anchor == null)
                    return;
                list = (GrReferenceList) myPsiClass.addBefore(list, anchor);
                myPsiClass.getNode().addLeaf(TokenType.WHITE_SPACE, " ", anchor.getNode());
                myPsiClass.getNode().addLeaf(TokenType.WHITE_SPACE, " ", list.getNode());
            }
        } else {
            list = myPsiClass.getImplementsClause();
            if (list == null) {
                list = factory.createImplementsClause();
                PsiElement anchor = myPsiClass.getBody();
                if (anchor == null)
                    return;
                list = (GrReferenceList) myPsiClass.addBefore(list, anchor);
                myPsiClass.getNode().addLeaf(TokenType.WHITE_SPACE, " ", list.getNode());
                myPsiClass.getNode().addLeaf(TokenType.WHITE_SPACE, " ", anchor.getNode());
            }
        }
        final GrCodeReferenceElement _ref = factory.createReferenceElementFromText(myInterfaceName + (addTypeParam ? "<" + AddMethodFix.generateTypeText(myPsiClass) + ">" : ""));
        final GrCodeReferenceElement ref = (GrCodeReferenceElement) list.add(_ref);
        JavaCodeStyleManager.getInstance(project).shortenClassReferences(ref);
    }
    if (comparable != null && !myPsiClass.isInterface()) {
        final PsiMethod baseMethod = comparable.getMethods()[0];
        OverrideImplementUtil.overrideOrImplement(myPsiClass, baseMethod);
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GrReferenceList(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrReferenceList) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)

Example 5 with GrReferenceList

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

the class GrMemberInfo method extractClassMembers.

public static void extractClassMembers(PsiClass subclass, List<GrMemberInfo> result, Filter<GrMember> filter, final boolean extractInterfacesDeep) {
    if (!(subclass instanceof GrTypeDefinition))
        return;
    if (extractInterfacesDeep) {
        extractSuperInterfaces(subclass, filter, result, ContainerUtil.<PsiClass>newHashSet());
    } else {
        PsiClass[] interfaces = subclass.getInterfaces();
        GrReferenceList sourceRefList = subclass.isInterface() ? ((GrTypeDefinition) subclass).getExtendsClause() : ((GrTypeDefinition) subclass).getImplementsClause();
        for (PsiClass anInterface : interfaces) {
            if (anInterface instanceof GrTypeDefinition && filter.includeMember((GrMember) anInterface)) {
                result.add(new GrMemberInfo((GrMember) anInterface, true, sourceRefList));
            }
        }
    }
    PsiClass[] innerClasses = subclass.getInnerClasses();
    for (PsiClass innerClass : innerClasses) {
        if (innerClass instanceof GrTypeDefinition && filter.includeMember((GrMember) innerClass)) {
            result.add(new GrMemberInfo((GrMember) innerClass));
        }
    }
    GrMethod[] methods = ((GrTypeDefinition) subclass).getCodeMethods();
    for (GrMethod method : methods) {
        if (!method.isConstructor() && filter.includeMember(method)) {
            result.add(new GrMemberInfo(method));
        }
    }
    GrField[] fields = ((GrTypeDefinition) subclass).getCodeFields();
    for (final GrField field : fields) {
        if (filter.includeMember(field)) {
            result.add(new GrMemberInfo(field));
        }
    }
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GrReferenceList(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrReferenceList) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)

Aggregations

GrReferenceList (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrReferenceList)9 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)4 PsiElement (com.intellij.psi.PsiElement)3 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)3 NotNull (org.jetbrains.annotations.NotNull)2 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)2 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)2 Template (com.intellij.codeInsight.template.Template)1 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)1 TemplateEditingAdapter (com.intellij.codeInsight.template.TemplateEditingAdapter)1 ASTNode (com.intellij.lang.ASTNode)1 Editor (com.intellij.openapi.editor.Editor)1 RangeMarker (com.intellij.openapi.editor.RangeMarker)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiClass (com.intellij.psi.PsiClass)1 PsiClassType (com.intellij.psi.PsiClassType)1 CandidateInfo (com.intellij.psi.infos.CandidateInfo)1 IElementType (com.intellij.psi.tree.IElementType)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 ArrayList (java.util.ArrayList)1