Search in sources :

Example 36 with GrCodeReferenceElement

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

the class GrImportStatementImpl method processSingleClassImport.

private boolean processSingleClassImport(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state) {
    if (!ResolveUtil.shouldProcessClasses(processor.getHint(ElementClassHint.KEY)))
        return true;
    GrCodeReferenceElement ref = getImportReference();
    if (ref == null)
        return true;
    final PsiElement resolved = ref.resolve();
    if (!(resolved instanceof PsiClass)) {
        return true;
    }
    if (!isAliasedImport() && isFromSamePackage((PsiClass) resolved)) {
        //don't process classes from the same package because such import statements are ignored by compiler
        return true;
    }
    return processor.execute(resolved, state.put(ClassHint.RESOLVE_CONTEXT, this));
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)

Example 37 with GrCodeReferenceElement

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

the class GrCodeReferenceElementImpl method bindWithQualifiedRef.

@Override
protected GrCodeReferenceElement bindWithQualifiedRef(@NotNull String qName) {
    final GrCodeReferenceElement qualifiedRef = GroovyPsiElementFactory.getInstance(getProject()).createTypeOrPackageReference(qName);
    final PsiElement list = getTypeArgumentList();
    if (list != null) {
        qualifiedRef.getNode().addChild(list.copy().getNode());
    }
    getNode().getTreeParent().replaceChild(getNode(), qualifiedRef.getNode());
    return qualifiedRef;
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)

Example 38 with GrCodeReferenceElement

use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project android by JetBrains.

the class GradleDslParser method getExpressionElement.

@Nullable
private static GradleDslExpression getExpressionElement(@NotNull GradleDslElement parentElement, @NotNull GroovyPsiElement psiElement, @NotNull String propertyName, @NotNull GrExpression propertyExpression) {
    if (propertyExpression instanceof GrLiteral) {
        // ex: compileSdkVersion 23 or compileSdkVersion = "android-23"
        return new GradleDslLiteral(parentElement, psiElement, propertyName, (GrLiteral) propertyExpression);
    }
    if (propertyExpression instanceof GrReferenceExpression) {
        // ex: compileSdkVersion SDK_VERSION or sourceCompatibility = VERSION_1_5
        return new GradleDslReference(parentElement, psiElement, propertyName, (GrReferenceExpression) propertyExpression);
    }
    if (propertyExpression instanceof GrMethodCallExpression) {
        // ex: compile project("someProject")
        GrMethodCallExpression methodCall = (GrMethodCallExpression) propertyExpression;
        GrReferenceExpression callReferenceExpression = getChildOfType(methodCall, GrReferenceExpression.class);
        if (callReferenceExpression != null) {
            String referenceName = callReferenceExpression.getText();
            if (!isEmpty(referenceName)) {
                GrArgumentList argumentList = methodCall.getArgumentList();
                if (argumentList.getAllArguments().length > 0) {
                    return getMethodCall(parentElement, methodCall, referenceName, argumentList);
                } else {
                    return new GradleDslMethodCall(parentElement, methodCall, referenceName);
                }
            }
        }
    }
    if (propertyExpression instanceof GrNewExpression) {
        GrNewExpression newExpression = (GrNewExpression) propertyExpression;
        GrCodeReferenceElement referenceElement = newExpression.getReferenceElement();
        if (referenceElement != null) {
            String objectName = referenceElement.getText();
            if (!isEmpty(objectName)) {
                GrArgumentList argumentList = newExpression.getArgumentList();
                if (argumentList != null) {
                    if (argumentList.getAllArguments().length > 0) {
                        return getNewExpression(parentElement, newExpression, objectName, argumentList);
                    }
                }
            }
        }
    }
    return null;
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) Nullable(org.jetbrains.annotations.Nullable)

Example 39 with GrCodeReferenceElement

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

the class CreateClassFix method createClassFixAction.

public static IntentionAction createClassFixAction(final GrReferenceElement refElement, GrCreateClassKind type) {
    return new CreateClassActionBase(type, refElement) {

        @Override
        protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
            final PsiFile file = element.getContainingFile();
            if (!(file instanceof GroovyFileBase))
                return;
            GroovyFileBase groovyFile = (GroovyFileBase) file;
            PsiElement qualifier = myRefElement.getQualifier();
            if (qualifier == null || qualifier instanceof GrReferenceElement && ((GrReferenceElement) qualifier).resolve() instanceof PsiPackage) {
                createTopLevelClass(project, groovyFile);
            } else {
                createInnerClass(project, editor, qualifier);
            }
        }

        private void createInnerClass(Project project, final Editor editor, PsiElement qualifier) {
            PsiElement resolved = resolveQualifier(qualifier);
            if (!(resolved instanceof PsiClass))
                return;
            JVMElementFactory factory = JVMElementFactories.getFactory(resolved.getLanguage(), project);
            if (factory == null)
                return;
            String name = myRefElement.getReferenceName();
            PsiClass template = createTemplate(factory, name);
            if (template == null) {
                ApplicationManager.getApplication().invokeLater(() -> {
                    if (editor != null && editor.getComponent().isDisplayable()) {
                        HintManager.getInstance().showErrorHint(editor, GroovyIntentionsBundle.message("cannot.create.class"));
                    }
                });
                return;
            }
            if (!FileModificationService.getInstance().preparePsiElementForWrite(resolved))
                return;
            WriteAction.run(() -> {
                PsiClass added = (PsiClass) resolved.add(template);
                PsiModifierList modifierList = added.getModifierList();
                if (modifierList != null) {
                    modifierList.setModifierProperty(PsiModifier.STATIC, true);
                }
                IntentionUtils.positionCursor(project, added.getContainingFile(), added);
            });
        }

        @Nullable
        private PsiElement resolveQualifier(@NotNull PsiElement qualifier) {
            if (qualifier instanceof GrCodeReferenceElement) {
                return ((GrCodeReferenceElement) qualifier).resolve();
            } else if (qualifier instanceof GrExpression) {
                PsiType type = ((GrExpression) qualifier).getType();
                if (type instanceof PsiClassType) {
                    return ((PsiClassType) type).resolve();
                } else if (qualifier instanceof GrReferenceExpression) {
                    final PsiElement resolved = ((GrReferenceExpression) qualifier).resolve();
                    if (resolved instanceof PsiClass || resolved instanceof PsiPackage) {
                        return resolved;
                    }
                }
            }
            return null;
        }

        @Nullable
        private PsiClass createTemplate(JVMElementFactory factory, String name) {
            switch(getType()) {
                case ENUM:
                    return factory.createEnum(name);
                case TRAIT:
                    if (factory instanceof GroovyPsiElementFactory) {
                        return ((GroovyPsiElementFactory) factory).createTrait(name);
                    } else {
                        return null;
                    }
                case CLASS:
                    return factory.createClass(name);
                case INTERFACE:
                    return factory.createInterface(name);
                case ANNOTATION:
                    return factory.createAnnotationType(name);
                default:
                    return null;
            }
        }

        private void createTopLevelClass(@NotNull Project project, @NotNull GroovyFileBase file) {
            final String pack = getPackage(file);
            final PsiManager manager = PsiManager.getInstance(project);
            final String name = myRefElement.getReferenceName();
            assert name != null;
            final Module module = ModuleUtilCore.findModuleForPsiElement(file);
            PsiDirectory targetDirectory = getTargetDirectory(project, pack, name, module, getText());
            if (targetDirectory == null)
                return;
            String templateName = getTemplateName(getType());
            final PsiClass targetClass = createClassByType(targetDirectory, name, manager, myRefElement, templateName, true);
            if (targetClass == null)
                return;
            bindRef(targetClass, myRefElement);
            IntentionUtils.positionCursor(project, targetClass.getContainingFile(), targetClass);
        }

        @NotNull
        private String getPackage(@NotNull PsiClassOwner file) {
            final PsiElement qualifier = myRefElement.getQualifier();
            if (qualifier instanceof GrReferenceElement) {
                final PsiElement resolved = ((GrReferenceElement) qualifier).resolve();
                if (resolved instanceof PsiPackage) {
                    return ((PsiPackage) resolved).getQualifiedName();
                }
            }
            return file instanceof GroovyFile ? file.getPackageName() : "";
        }

        @Override
        public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
            if (!super.isAvailable(project, editor, file))
                return false;
            final PsiElement qualifier = myRefElement.getQualifier();
            if (qualifier != null && resolveQualifier(qualifier) == null) {
                return false;
            }
            return true;
        }
    };
}
Also used : GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) NotNull(org.jetbrains.annotations.NotNull) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) Project(com.intellij.openapi.project.Project) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) Editor(com.intellij.openapi.editor.Editor) Module(com.intellij.openapi.module.Module) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 40 with GrCodeReferenceElement

use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement 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)

Aggregations

GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)71 NotNull (org.jetbrains.annotations.NotNull)14 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)13 PsiElement (com.intellij.psi.PsiElement)12 Nullable (org.jetbrains.annotations.Nullable)12 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)11 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)10 GrNewExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression)8 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)7 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)7 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)6 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)5 GrAnonymousClassDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition)5 PsiClass (com.intellij.psi.PsiClass)4 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)4 GrTypeArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeArgumentList)4 Editor (com.intellij.openapi.editor.Editor)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 GrAnnotation (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation)3 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)3