Search in sources :

Example 26 with GroovyFile

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

the class GroovyFindJarFix method getFqns.

@Override
protected Collection<String> getFqns(@NotNull GrReferenceElement ref) {
    GrImportStatement importStatement = PsiTreeUtil.getParentOfType(ref.getElement(), GrImportStatement.class);
    //from static imports
    if (importStatement != null) {
        GrCodeReferenceElement reference = importStatement.getImportReference();
        if (reference != null) {
            return Collections.singleton(reference.getText());
        }
        return Collections.emptyList();
    }
    if (ref.getQualifier() != null)
        return Collections.emptyList();
    final String className = ref.getReferenceName();
    if (className == null)
        return Collections.emptyList();
    PsiFile file = ref.getContainingFile().getOriginalFile();
    if (!(file instanceof GroovyFile))
        return Collections.emptyList();
    GrImportStatement[] importList = ((GroovyFile) file).getImportStatements();
    for (GrImportStatement imp : importList) {
        if (className.equals(imp.getImportedName())) {
            GrCodeReferenceElement importReference = imp.getImportReference();
            if (importReference == null)
                return Collections.emptyList();
            return Collections.singleton(importReference.getText());
        }
    }
    return Collections.emptyList();
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) PsiFile(com.intellij.psi.PsiFile) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 27 with GroovyFile

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

the class StaticImportInsertHandler method handleInsert.

@Override
public void handleInsert(InsertionContext context, JavaGlobalMemberLookupElement item) {
    GroovyInsertHandler.INSTANCE.handleInsert(context, item);
    final PsiMember member = item.getObject();
    PsiDocumentManager.getInstance(member.getProject()).commitDocument(context.getDocument());
    final GrReferenceExpression ref = PsiTreeUtil.findElementOfClassAtOffset(context.getFile(), context.getStartOffset(), GrReferenceExpression.class, false);
    if (ref != null && ref.getQualifier() == null && context.getFile() instanceof GroovyFile && !importAlreadyExists(member, ((GroovyFile) context.getFile()), ref) && !PsiManager.getInstance(context.getProject()).areElementsEquivalent(ref.resolve(), member)) {
        ref.bindToElementViaStaticImport(member);
    }
}
Also used : GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 28 with GroovyFile

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

Example 29 with GroovyFile

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

the class BaseScriptAnnotationChecker method checkApplicability.

@Override
public boolean checkApplicability(@NotNull AnnotationHolder holder, @NotNull GrAnnotation annotation) {
    if (GroovyCommonClassNames.GROOVY_TRANSFORM_BASE_SCRIPT.equals(annotation.getQualifiedName())) {
        PsiFile file = annotation.getContainingFile();
        if (file instanceof GroovyFile && !(((GroovyFile) file).isScript())) {
            holder.createErrorAnnotation(annotation, GroovyBundle.message("base.script.annotation.is.allowed.only.inside.scripts"));
            return true;
        }
        PsiElement pparent = annotation.getParent().getParent();
        if (pparent instanceof GrVariableDeclaration) {
            GrTypeElement typeElement = ((GrVariableDeclaration) pparent).getTypeElementGroovy();
            PsiType type = typeElement != null ? typeElement.getType() : null;
            if (!InheritanceUtil.isInheritor(type, GroovyCommonClassNames.GROOVY_LANG_SCRIPT)) {
                String typeText = type != null ? type.getCanonicalText() : CommonClassNames.JAVA_LANG_OBJECT;
                holder.createErrorAnnotation(annotation, GroovyBundle.message("declared.type.0.have.to.extend.script", typeText));
                return true;
            }
        }
    }
    return false;
}
Also used : GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) PsiFile(com.intellij.psi.PsiFile) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType)

Example 30 with GroovyFile

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

the class GroovyFileIconProvider method getIcon.

@Nullable
@Override
public Icon getIcon(@NotNull VirtualFile virtualFile, @Iconable.IconFlags int flags, @Nullable Project project) {
    if (project == null || virtualFile.getFileType() != GroovyFileType.GROOVY_FILE_TYPE)
        return null;
    final PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
    if (!(psiFile instanceof GroovyFile))
        return null;
    final GroovyFile file = (GroovyFile) psiFile;
    final Icon icon;
    if (file.isScript()) {
        icon = GroovyScriptTypeDetector.getIcon(file);
    } else if (GrFileIndexUtil.isGroovySourceFile(file)) {
        final GrTypeDefinition[] typeDefinitions = file.getTypeDefinitions();
        icon = typeDefinitions.length > 0 ? typeDefinitions[0].getIcon(flags) : JetgroovyIcons.Groovy.GroovyFile;
    } else {
        icon = JetgroovyIcons.Groovy.Groovy_outsideSources;
    }
    return ElementBase.createLayeredIcon(psiFile, icon, ElementBase.transformFlags(psiFile, flags));
}
Also used : PsiFile(com.intellij.psi.PsiFile) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)91 PsiFile (com.intellij.psi.PsiFile)26 PsiElement (com.intellij.psi.PsiElement)21 NotNull (org.jetbrains.annotations.NotNull)17 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)17 VirtualFile (com.intellij.openapi.vfs.VirtualFile)13 Project (com.intellij.openapi.project.Project)10 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)10 PsiClass (com.intellij.psi.PsiClass)9 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)9 Nullable (org.jetbrains.annotations.Nullable)8 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)8 GroovyScriptClass (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass)8 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)7 Module (com.intellij.openapi.module.Module)6 IncorrectOperationException (com.intellij.util.IncorrectOperationException)6 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)6 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)6 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)6 ASTNode (com.intellij.lang.ASTNode)5