Search in sources :

Example 56 with GrTypeDefinition

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

the class GrNavBarModelExtension method adjustElement.

@Override
public PsiElement adjustElement(PsiElement psiElement) {
    final ProjectFileIndex index = ProjectRootManager.getInstance(psiElement.getProject()).getFileIndex();
    final PsiFile containingFile = psiElement.getContainingFile();
    if (containingFile instanceof GroovyFileBase) {
        final VirtualFile file = containingFile.getVirtualFile();
        if (file != null && (index.isUnderSourceRootOfType(file, JavaModuleSourceRootTypes.SOURCES) || index.isInLibraryClasses(file) || index.isInLibrarySource(file))) {
            if (psiElement instanceof GroovyFileBase) {
                final GroovyFileBase grFile = (GroovyFileBase) psiElement;
                if (grFile.getViewProvider().getBaseLanguage().equals(GroovyLanguage.INSTANCE)) {
                    final PsiClass[] psiClasses = grFile.getClasses();
                    if (psiClasses.length == 1 && !grFile.isScript()) {
                        return psiClasses[0];
                    }
                }
            } else if (psiElement instanceof GrTypeDefinition) {
                return psiElement;
            }
        }
        return containingFile;
    }
    return psiElement;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) PsiClass(com.intellij.psi.PsiClass) PsiFile(com.intellij.psi.PsiFile)

Example 57 with GrTypeDefinition

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

the class GroovyImplementMethodsHandler method invoke.

@Override
public void invoke(@NotNull final Project project, @NotNull Editor editor, @NotNull PsiFile file) {
    if (!EditorModificationUtil.checkModificationAllowed(editor))
        return;
    PsiClass aClass = OverrideImplementUtil.getContextClass(project, editor, file, true);
    if (aClass instanceof GrTypeDefinition) {
        GrTypeDefinition typeDefinition = (GrTypeDefinition) aClass;
        if (GroovyOverrideImplementExploreUtil.getMethodSignaturesToImplement(typeDefinition).isEmpty()) {
            HintManager.getInstance().showErrorHint(editor, "No methods to implement have been found");
            return;
        }
        GroovyOverrideImplementUtil.chooseAndImplementMethods(project, editor, typeDefinition);
    }
}
Also used : GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) PsiClass(com.intellij.psi.PsiClass)

Example 58 with GrTypeDefinition

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

the class ClassNode method getChildrenImpl.

@Override
@Nullable
protected Collection<AbstractTreeNode> getChildrenImpl() {
    final List<AbstractTreeNode> children = new ArrayList<>();
    final Module module = getModule();
    final GrTypeDefinition grTypeDefinition = extractPsiFromValue();
    assert grTypeDefinition != null;
    buildChildren(module, grTypeDefinition, children);
    return children.isEmpty() ? null : children;
}
Also used : GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) ArrayList(java.util.ArrayList) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable)

Example 59 with GrTypeDefinition

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

the class GroovyRangeTypeCheckInspection method buildFix.

@Override
protected GroovyFix buildFix(@NotNull PsiElement location) {
    final GrRangeExpression range = (GrRangeExpression) location;
    final PsiType type = range.getType();
    final List<GroovyFix> fixes = new ArrayList<>(3);
    if (type instanceof GrRangeType) {
        PsiType iterationType = ((GrRangeType) type).getIterationType();
        if (!(iterationType instanceof PsiClassType))
            return null;
        final PsiClass psiClass = ((PsiClassType) iterationType).resolve();
        if (!(psiClass instanceof GrTypeDefinition))
            return null;
        final GroovyResolveResult[] nexts = ResolveUtil.getMethodCandidates(iterationType, "next", range);
        final GroovyResolveResult[] previouses = ResolveUtil.getMethodCandidates(iterationType, "previous", range);
        final GroovyResolveResult[] compareTos = ResolveUtil.getMethodCandidates(iterationType, "compareTo", range, iterationType);
        if (countImplementations(psiClass, nexts) == 0) {
            fixes.add(GroovyQuickFixFactory.getInstance().createAddMethodFix("next", (GrTypeDefinition) psiClass));
        }
        if (countImplementations(psiClass, previouses) == 0) {
            fixes.add(GroovyQuickFixFactory.getInstance().createAddMethodFix("previous", (GrTypeDefinition) psiClass));
        }
        if (!InheritanceUtil.isInheritor(iterationType, CommonClassNames.JAVA_LANG_COMPARABLE) || countImplementations(psiClass, compareTos) == 0) {
            fixes.add(GroovyQuickFixFactory.getInstance().createAddClassToExtendsFix((GrTypeDefinition) psiClass, CommonClassNames.JAVA_LANG_COMPARABLE));
        }
        return new GroovyFix() {

            @Override
            protected void doFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) throws IncorrectOperationException {
                for (GroovyFix fix : fixes) {
                    fix.applyFix(project, descriptor);
                }
            }

            @NotNull
            @Override
            public String getName() {
                return GroovyInspectionBundle.message("fix.class", psiClass.getName());
            }

            @Nls
            @NotNull
            @Override
            public String getFamilyName() {
                return "Fix range class";
            }
        };
    }
    return null;
}
Also used : ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) ArrayList(java.util.ArrayList) GrRangeType(org.jetbrains.plugins.groovy.lang.psi.impl.GrRangeType) NotNull(org.jetbrains.annotations.NotNull) Project(com.intellij.openapi.project.Project) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrRangeExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.arithmetic.GrRangeExpression)

Example 60 with GrTypeDefinition

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

the class NewInstanceOfSingletonInspection method buildVisitor.

@NotNull
@Override
protected BaseInspectionVisitor buildVisitor() {
    return new BaseInspectionVisitor() {

        @Override
        public void visitNewExpression(@NotNull GrNewExpression newExpression) {
            if (newExpression.getArrayDeclaration() != null)
                return;
            GrCodeReferenceElement refElement = newExpression.getReferenceElement();
            if (refElement == null)
                return;
            PsiElement resolved = refElement.resolve();
            if (!(resolved instanceof GrTypeDefinition))
                return;
            PsiAnnotation annotation = AnnotationUtil.findAnnotation((GrTypeDefinition) resolved, GROOVY_LANG_SINGLETON);
            if (annotation == null)
                return;
            registerError(newExpression, GroovyInspectionBundle.message("new.instance.of.singleton"), ContainerUtil.ar(new ReplaceWithInstanceAccessFix()), ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
        }
    };
}
Also used : GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) BaseInspectionVisitor(org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) PsiAnnotation(com.intellij.psi.PsiAnnotation) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)78 PsiElement (com.intellij.psi.PsiElement)17 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)17 PsiClass (com.intellij.psi.PsiClass)16 NotNull (org.jetbrains.annotations.NotNull)15 Nullable (org.jetbrains.annotations.Nullable)14 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)9 PsiFile (com.intellij.psi.PsiFile)8 GroovyFileBase (org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase)8 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)8 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)8 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)8 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)7 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)7 Project (com.intellij.openapi.project.Project)6 ArrayList (java.util.ArrayList)6 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)6 Editor (com.intellij.openapi.editor.Editor)5 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)5 IncorrectOperationException (com.intellij.util.IncorrectOperationException)4