Search in sources :

Example 86 with GroovyFile

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

the class GrStubFileElementType method getBuilder.

@Override
public StubBuilder getBuilder() {
    return new DefaultStubBuilder() {

        @NotNull
        @Override
        protected StubElement createStubForFile(@NotNull final PsiFile file) {
            if (file instanceof GroovyFile) {
                return new GrFileStub((GroovyFile) file);
            }
            return super.createStubForFile(file);
        }

        @Override
        public boolean skipChildProcessingWhenBuildingStubs(@NotNull ASTNode parent, @NotNull ASTNode node) {
            IElementType childType = node.getElementType();
            IElementType parentType = parent.getElementType();
            if (childType == GroovyElementTypes.PARAMETER && parentType != GroovyElementTypes.PARAMETERS_LIST) {
                return true;
            }
            if (childType == GroovyElementTypes.PARAMETERS_LIST && !(parent.getPsi() instanceof GrMethod)) {
                return true;
            }
            if (childType == GroovyElementTypes.MODIFIERS) {
                if (parentType == GroovyElementTypes.CLASS_INITIALIZER) {
                    return true;
                }
                if (parentType == GroovyElementTypes.VARIABLE_DEFINITION && !GroovyElementTypes.VARIABLE_DEFINITION.shouldCreateStub(parent)) {
                    return true;
                }
            }
            return false;
        }
    };
}
Also used : IElementType(com.intellij.psi.tree.IElementType) ASTNode(com.intellij.lang.ASTNode) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) PsiFile(com.intellij.psi.PsiFile) NotNull(org.jetbrains.annotations.NotNull) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) GrFileStub(org.jetbrains.plugins.groovy.lang.psi.stubs.GrFileStub)

Example 87 with GroovyFile

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

the class GrVariableDeclarationElementType method shouldCreateStub.

@Override
public boolean shouldCreateStub(ASTNode node) {
    PsiElement parent = SharedImplUtil.getParent(node);
    if (parent instanceof GrTypeDefinitionBody) {
        // store fields
        return true;
    }
    if (PsiTreeUtil.getParentOfType(parent, GrTypeDefinition.class) != null) {
        // do not store variable declarations within classes, as they are not scripts
        return false;
    }
    PsiElement psi = node.getPsi();
    if (!(psi instanceof GrVariableDeclaration) || ((GrVariableDeclaration) psi).getModifierList().getRawAnnotations().length == 0) {
        // store only annotated declarations
        return false;
    }
    PsiFile file = psi.getContainingFile();
    return file instanceof GroovyFile && ((GroovyFile) file).isScript();
}
Also used : GrTypeDefinitionBody(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 88 with GroovyFile

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

the class ResolveUtil method resolveLabelTargets.

@NotNull
public static Pair<GrStatement, GrLabeledStatement> resolveLabelTargets(@Nullable String labelName, @Nullable PsiElement element, boolean isBreak) {
    if (element == null)
        return new Pair<>(null, null);
    if (labelName == null) {
        do {
            element = element.getContext();
            if (element == null || element instanceof GrClosableBlock || element instanceof GrMember || element instanceof GroovyFile) {
                return new Pair<>(null, null);
            }
        } while (!(element instanceof GrLoopStatement) && !(isBreak && element instanceof GrSwitchStatement));
        return new Pair<>(((GrStatement) element), null);
    }
    GrStatement statement = null;
    do {
        PsiElement last = element;
        element = element.getContext();
        if (element == null || element instanceof GrMember || element instanceof GroovyFile)
            break;
        if (element instanceof GrStatement && !(element instanceof GrClosableBlock)) {
            statement = (GrStatement) element;
        }
        PsiElement sibling = element;
        while (sibling != null) {
            final GrLabeledStatement labelStatement = findLabelStatementIn(sibling, last, labelName);
            if (labelStatement != null) {
                return Pair.create(statement, labelStatement);
            }
            sibling = sibling.getPrevSibling();
        }
        if (element instanceof GrClosableBlock)
            break;
    } while (true);
    return new Pair<>(null, null);
}
Also used : GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 89 with GroovyFile

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

the class GrMainCompletionProvider method completeStaticMembers.

static StaticMemberProcessor completeStaticMembers(CompletionParameters parameters) {
    final PsiElement position = parameters.getPosition();
    final PsiElement originalPosition = parameters.getOriginalPosition();
    final StaticMemberProcessor processor = new StaticMemberProcessor(position) {

        @NotNull
        @Override
        protected LookupElement createLookupElement(@NotNull PsiMember member, @NotNull PsiClass containingClass, boolean shouldImport) {
            shouldImport |= originalPosition != null && PsiTreeUtil.isAncestor(containingClass, originalPosition, false);
            return createGlobalMemberElement(member, containingClass, shouldImport);
        }

        @Override
        protected LookupElement createLookupElement(@NotNull List<PsiMethod> overloads, @NotNull PsiClass containingClass, boolean shouldImport) {
            shouldImport |= originalPosition != null && PsiTreeUtil.isAncestor(containingClass, originalPosition, false);
            return new JavaGlobalMemberLookupElement(overloads, containingClass, QualifiedMethodInsertHandler.INSTANCE, StaticImportInsertHandler.INSTANCE, shouldImport);
        }

        @Override
        protected boolean isAccessible(PsiMember member) {
            boolean result = super.isAccessible(member);
            if (!result && member instanceof GrField) {
                GrAccessorMethod[] getters = ((GrField) member).getGetters();
                return getters.length > 0 && super.isAccessible(getters[0]);
            }
            return result;
        }
    };
    final PsiFile file = position.getContainingFile();
    if (file instanceof GroovyFile) {
        for (GrImportStatement statement : ((GroovyFile) file).getImportStatements()) {
            if (statement.isStatic()) {
                GrCodeReferenceElement importReference = statement.getImportReference();
                if (importReference != null) {
                    if (!statement.isOnDemand()) {
                        importReference = importReference.getQualifier();
                    }
                    if (importReference != null) {
                        final PsiElement target = importReference.resolve();
                        if (target instanceof PsiClass) {
                            processor.importMembersOf((PsiClass) target);
                        }
                    }
                }
            }
        }
    }
    return processor;
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) NotNull(org.jetbrains.annotations.NotNull) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GrTypeParameterList(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeParameterList) List(java.util.List) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 90 with GroovyFile

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

the class GroovyDocumentationProvider method generateClassInfo.

private static String generateClassInfo(PsiClass aClass) {
    StringBuilder buffer = new StringBuilder();
    GroovyFile file = (GroovyFile) aClass.getContainingFile();
    String packageName = file.getPackageName();
    if (!packageName.isEmpty()) {
        buffer.append(packageName).append("\n");
    }
    final String classString = aClass.isInterface() ? "interface" : aClass instanceof PsiTypeParameter ? "type parameter" : aClass.isEnum() ? "enum" : "class";
    buffer.append(classString).append(" ").append(aClass.getName());
    JavaDocumentationProvider.generateTypeParameters(aClass, buffer);
    JavaDocumentationProvider.writeExtends(aClass, buffer, aClass.getExtendsListTypes());
    JavaDocumentationProvider.writeImplements(aClass, buffer, aClass.getImplementsListTypes());
    return buffer.toString();
}
Also used : GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

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