Search in sources :

Example 6 with GrPackageDefinition

use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition in project intellij-community by JetBrains.

the class GrUnresolvedAccessChecker method checkCodeRefInner.

@Nullable
private HighlightInfo checkCodeRefInner(GrCodeReferenceElement refElement) {
    if (PsiTreeUtil.getParentOfType(refElement, GroovyDocPsiElement.class) != null)
        return null;
    PsiElement nameElement = refElement.getReferenceNameElement();
    if (nameElement == null)
        return null;
    if (isResolvedStaticImport(refElement))
        return null;
    GroovyResolveResult resolveResult = refElement.advancedResolve();
    final PsiElement resolved = resolveResult.getElement();
    if (!(refElement.getParent() instanceof GrPackageDefinition) && resolved == null) {
        String message = GroovyBundle.message("cannot.resolve", refElement.getReferenceName());
        HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.WRONG_REF).range(nameElement).descriptionAndTooltip(message).create();
        // todo implement for nested classes
        registerCreateClassByTypeFix(refElement, info, myDisplayKey);
        registerAddImportFixes(refElement, info, myDisplayKey);
        UnresolvedReferenceQuickFixProvider.registerReferenceFixes(refElement, new QuickFixActionRegistrarAdapter(info, myDisplayKey));
        QuickFixFactory.getInstance().registerOrderEntryFixes(new QuickFixActionRegistrarAdapter(info, myDisplayKey), refElement);
        return info;
    }
    if (refElement.getParent() instanceof GrNewExpression) {
        boolean inStaticContext = GrStaticChecker.isInStaticContext(refElement);
        if (!inStaticContext && GrUnresolvedAccessInspection.isSuppressed(refElement))
            return null;
        if (!inStaticContext) {
            if (!myInspectionEnabled)
                return null;
            assert myInspection != null;
            if (!myInspection.myHighlightInnerClasses)
                return null;
        }
        GrNewExpression newExpression = (GrNewExpression) refElement.getParent();
        if (resolved instanceof PsiClass) {
            PsiClass clazz = (PsiClass) resolved;
            final PsiClass outerClass = clazz.getContainingClass();
            if (com.intellij.psi.util.PsiUtil.isInnerClass(clazz) && outerClass != null && newExpression.getArgumentList() != null && !PsiUtil.hasEnclosingInstanceInScope(outerClass, newExpression, true) && !hasEnclosingInstanceInArgList(newExpression.getArgumentList(), outerClass)) {
                String qname = clazz.getQualifiedName();
                LOG.assertTrue(qname != null, clazz.getText());
                return createAnnotationForRef(refElement, inStaticContext, GroovyBundle.message("cannot.reference.non.static", qname));
            }
        }
    }
    return null;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GroovyDocPsiElement(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GroovyDocPsiElement) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GroovyDocPsiElement(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GroovyDocPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with GrPackageDefinition

use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition in project intellij-community by JetBrains.

the class GrUnresolvedAccessChecker method registerCreateClassByTypeFix.

private static void registerCreateClassByTypeFix(@NotNull GrReferenceElement refElement, @Nullable HighlightInfo info, final HighlightDisplayKey key) {
    GrPackageDefinition packageDefinition = PsiTreeUtil.getParentOfType(refElement, GrPackageDefinition.class);
    if (packageDefinition != null)
        return;
    PsiElement parent = refElement.getParent();
    if (parent instanceof GrNewExpression && refElement.getManager().areElementsEquivalent(((GrNewExpression) parent).getReferenceElement(), refElement)) {
        QuickFixAction.registerQuickFixAction(info, GroovyQuickFixFactory.getInstance().createClassFromNewAction((GrNewExpression) parent), key);
    } else if (canBeClassOrPackage(refElement)) {
        if (shouldBeInterface(refElement)) {
            QuickFixAction.registerQuickFixAction(info, GroovyQuickFixFactory.getInstance().createClassFixAction(refElement, GrCreateClassKind.INTERFACE), key);
            QuickFixAction.registerQuickFixAction(info, GroovyQuickFixFactory.getInstance().createClassFixAction(refElement, GrCreateClassKind.TRAIT), key);
        } else if (shouldBeClass(refElement)) {
            QuickFixAction.registerQuickFixAction(info, GroovyQuickFixFactory.getInstance().createClassFixAction(refElement, GrCreateClassKind.CLASS), key);
            QuickFixAction.registerQuickFixAction(info, GroovyQuickFixFactory.getInstance().createClassFixAction(refElement, GrCreateClassKind.ENUM), key);
        } else if (shouldBeAnnotation(refElement)) {
            QuickFixAction.registerQuickFixAction(info, GroovyQuickFixFactory.getInstance().createClassFixAction(refElement, GrCreateClassKind.ANNOTATION), key);
        } else {
            QuickFixAction.registerQuickFixAction(info, GroovyQuickFixFactory.getInstance().createClassFixAction(refElement, GrCreateClassKind.CLASS), key);
            QuickFixAction.registerQuickFixAction(info, GroovyQuickFixFactory.getInstance().createClassFixAction(refElement, GrCreateClassKind.INTERFACE), key);
            if (!refElement.isQualified() || resolvesToGroovy(refElement.getQualifier())) {
                QuickFixAction.registerQuickFixAction(info, GroovyQuickFixFactory.getInstance().createClassFixAction(refElement, GrCreateClassKind.TRAIT), key);
            }
            QuickFixAction.registerQuickFixAction(info, GroovyQuickFixFactory.getInstance().createClassFixAction(refElement, GrCreateClassKind.ENUM), key);
            QuickFixAction.registerQuickFixAction(info, GroovyQuickFixFactory.getInstance().createClassFixAction(refElement, GrCreateClassKind.ANNOTATION), key);
        }
    }
}
Also used : GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GroovyDocPsiElement(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GroovyDocPsiElement)

Example 8 with GrPackageDefinition

use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition in project intellij-community by JetBrains.

the class GroovyFileImpl method checkIsScript.

private boolean checkIsScript() {
    final GrTopStatement[] topStatements = findChildrenByClass(GrTopStatement.class);
    boolean hasClassDefinitions = false;
    boolean hasTopStatements = false;
    for (GrTopStatement st : topStatements) {
        if (st instanceof GrTypeDefinition) {
            hasClassDefinitions = true;
        } else if (!(st instanceof GrImportStatement || st instanceof GrPackageDefinition)) {
            hasTopStatements = true;
            break;
        }
    }
    return hasTopStatements || !hasClassDefinitions;
}
Also used : GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrTopStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement)

Example 9 with GrPackageDefinition

use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition in project intellij-community by JetBrains.

the class GroovyFileImpl method setPackageName.

@Override
public void setPackageName(String packageName) {
    final ASTNode fileNode = getNode();
    final GrPackageDefinition currentPackage = getPackageDefinition();
    if (packageName == null || packageName.isEmpty()) {
        if (currentPackage != null) {
            final ASTNode currNode = currentPackage.getNode();
            fileNode.removeChild(currNode);
        }
        return;
    }
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
    final GrPackageDefinition newPackage = (GrPackageDefinition) factory.createTopElementFromText("package " + packageName);
    if (currentPackage != null) {
        final GrCodeReferenceElement packageReference = currentPackage.getPackageReference();
        if (packageReference != null) {
            GrCodeReferenceElement ref = newPackage.getPackageReference();
            if (ref != null) {
                packageReference.replace(ref);
            }
            return;
        }
    }
    final ASTNode newNode = newPackage.getNode();
    if (currentPackage != null) {
        final ASTNode currNode = currentPackage.getNode();
        fileNode.replaceChild(currNode, newNode);
    } else {
        ASTNode anchor = fileNode.getFirstChildNode();
        if (anchor != null && anchor.getElementType() == GroovyTokenTypes.mSH_COMMENT) {
            anchor = anchor.getTreeNext();
            fileNode.addLeaf(GroovyTokenTypes.mNLS, "\n", anchor);
        }
        fileNode.addChild(newNode, anchor);
        if (anchor != null && !anchor.getText().startsWith("\n\n")) {
            fileNode.addLeaf(GroovyTokenTypes.mNLS, "\n", anchor);
        }
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) ASTNode(com.intellij.lang.ASTNode) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition)

Example 10 with GrPackageDefinition

use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition in project intellij-community by JetBrains.

the class GrPackageInspection method getElementToHighlight.

@Nullable
private static PsiElement getElementToHighlight(GroovyFile file) {
    GrPackageDefinition packageDefinition = file.getPackageDefinition();
    if (packageDefinition != null)
        return packageDefinition;
    PsiClass[] classes = file.getClasses();
    for (PsiClass aClass : classes) {
        if (!(aClass instanceof SyntheticElement) && aClass instanceof GrTypeDefinition) {
            return ((GrTypeDefinition) aClass).getNameIdentifierGroovy();
        }
    }
    GrTopStatement[] statements = file.getTopStatements();
    if (statements.length > 0) {
        GrTopStatement first = statements[0];
        if (first instanceof GrNamedElement)
            return ((GrNamedElement) first).getNameIdentifierGroovy();
        return first;
    }
    return null;
}
Also used : GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrNamedElement(org.jetbrains.plugins.groovy.lang.psi.GrNamedElement) PsiClass(com.intellij.psi.PsiClass) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition) SyntheticElement(com.intellij.psi.SyntheticElement) GrTopStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GrPackageDefinition (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition)13 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)7 Nullable (org.jetbrains.annotations.Nullable)4 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)4 ASTNode (com.intellij.lang.ASTNode)3 GrTopStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement)3 PsiElement (com.intellij.psi.PsiElement)2 PackageEntry (com.intellij.psi.codeStyle.PackageEntry)2 PackageEntryTable (com.intellij.psi.codeStyle.PackageEntryTable)2 GrDocComment (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)2 GroovyDocPsiElement (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GroovyDocPsiElement)2 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)2 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)2 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)2 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)2 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)2 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)2 GroovyScriptClass (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass)2 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)1 FileASTNode (com.intellij.lang.FileASTNode)1