Search in sources :

Example 21 with GrImportStatement

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

the class GroovyChangeUtilSupport method encodeInformation.

@Override
public void encodeInformation(final TreeElement element, final ASTNode original, final Map<Object, Object> encodingState) {
    if (original instanceof CompositeElement) {
        if (original.getElementType() == GroovyElementTypes.REFERENCE_ELEMENT || original.getElementType() == GroovyElementTypes.REFERENCE_EXPRESSION) {
            PsiElement psi = original.getPsi();
            Project project = psi.getProject();
            if (!PsiUtil.isThisOrSuperRef(psi) && project.isInitialized() && !DumbService.isDumb(project)) {
                final GroovyResolveResult result = ((GrReferenceElement) psi).advancedResolve();
                if (result != null) {
                    final PsiElement target = result.getElement();
                    if (target instanceof PsiClass || (target instanceof PsiMethod || target instanceof PsiField) && ((PsiMember) target).hasModifierProperty(PsiModifier.STATIC) && result.getCurrentFileResolveContext() instanceof GrImportStatement) {
                        element.putCopyableUserData(REFERENCED_MEMBER_KEY, (PsiMember) target);
                    }
                }
            }
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) CompositeElement(com.intellij.psi.impl.source.tree.CompositeElement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)

Example 22 with GrImportStatement

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

the class GroovyFoldingBuilder method processImports.

private static void processImports(final List<FoldingDescriptor> descriptors, GrImportStatement[] imports) {
    if (imports.length < 2)
        return;
    PsiElement first = imports[0];
    while (first != null) {
        PsiElement marker = first;
        PsiElement next = first.getNextSibling();
        while (next instanceof GrImportStatement || next instanceof LeafPsiElement) {
            if (next instanceof GrImportStatement)
                marker = next;
            next = next.getNextSibling();
        }
        if (marker != first) {
            int start = first.getTextRange().getStartOffset();
            int end = marker.getTextRange().getEndOffset();
            int tail = "import ".length();
            if (start + tail < end && !JavaFoldingBuilderBase.hasErrorElementsNearby(first.getContainingFile(), start, end)) {
                FoldingDescriptor descriptor = new FoldingDescriptor(first.getNode(), new TextRange(start + tail, end));
                // imports are often added/removed automatically, so we enable autoupdate of folded region for foldings even if it's collapsed
                descriptor.setCanBeRemovedWhenCollapsed(true);
                descriptors.add(descriptor);
            }
        }
        while (!(next instanceof GrImportStatement) && next != null) next = next.getNextSibling();
        first = next;
    }
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) NamedFoldingDescriptor(com.intellij.lang.folding.NamedFoldingDescriptor) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) TextRange(com.intellij.openapi.util.TextRange) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Example 23 with GrImportStatement

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

the class ControlFlowBuilderUtil method findClassByText.

private static boolean findClassByText(GrReferenceExpression ref) {
    final String text = ref.getText();
    final int i = text.indexOf('<');
    String className = i == -1 ? text : text.substring(0, i);
    PsiClass[] names = PsiShortNamesCache.getInstance(ref.getProject()).getClassesByName(className, ref.getResolveScope());
    if (names.length > 0)
        return true;
    PsiFile file = ref.getContainingFile();
    if (file instanceof GroovyFile) {
        GrImportStatement[] imports = ((GroovyFile) file).getImportStatements();
        for (GrImportStatement anImport : imports) {
            if (className.equals(anImport.getImportedName()))
                return true;
        }
    }
    return false;
}
Also used : PsiClass(com.intellij.psi.PsiClass) 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 24 with GrImportStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement 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 25 with GrImportStatement

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

the class GroovyFileImpl method processDeclarationsNoGuess.

private boolean processDeclarationsNoGuess(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state, @Nullable PsiElement lastParent, @NotNull PsiElement place) {
    ElementClassHint classHint = processor.getHint(ElementClassHint.KEY);
    if (myContext != null) {
        if (ResolveUtil.shouldProcessProperties(classHint)) {
            if (!processChildrenScopes(processor, state, lastParent, place))
                return false;
        }
        return true;
    }
    boolean processClasses = ResolveUtil.shouldProcessClasses(classHint);
    GrImportStatement[] importStatements = getImportStatements();
    if (!processImports(processor, state, lastParent, place, importStatements, ImportKind.ALIAS, false))
        return false;
    GroovyScriptClass scriptClass = getScriptClass();
    if (scriptClass != null && StringUtil.isJavaIdentifier(scriptClass.getName())) {
        if (!(lastParent instanceof GrTypeDefinition)) {
            if (!ResolveUtil.processClassDeclarations(scriptClass, processor, state, lastParent, place))
                return false;
        }
        if (processClasses) {
            if (!ResolveUtil.processElement(processor, scriptClass, state))
                return false;
        }
    }
    if (processClasses) {
        for (GrTypeDefinition definition : getTypeDefinitions()) {
            if (!ResolveUtil.processElement(processor, definition, state))
                return false;
        }
    }
    if (ResolveUtil.shouldProcessProperties(classHint)) {
        if (!processChildrenScopes(processor, state, lastParent, place))
            return false;
    }
    if (!processImports(processor, state, lastParent, place, importStatements, ImportKind.ALIAS, true))
        return false;
    if (!processImports(processor, state, lastParent, place, importStatements, ImportKind.SIMPLE, null))
        return false;
    if (!processDeclarationsInPackage(processor, state, lastParent, place))
        return false;
    if (!processImports(processor, state, lastParent, place, importStatements, ImportKind.ON_DEMAND, null))
        return false;
    if (!ImplicitImportsKt.processImplicitImports(processor, state, lastParent, place, this))
        return false;
    if (ResolveUtil.shouldProcessPackages(classHint)) {
        NameHint nameHint = processor.getHint(NameHint.KEY);
        String expectedName = nameHint != null ? nameHint.getName(state) : null;
        final JavaPsiFacade facade = JavaPsiFacade.getInstance(getProject());
        if (expectedName != null) {
            final PsiPackage pkg = facade.findPackage(expectedName);
            if (pkg != null && !processor.execute(pkg, state)) {
                return false;
            }
        } else {
            PsiPackage defaultPackage = facade.findPackage("");
            if (defaultPackage != null) {
                for (PsiPackage subPackage : defaultPackage.getSubPackages(getResolveScope())) {
                    if (!ResolveUtil.processElement(processor, subPackage, state))
                        return false;
                }
            }
        }
    }
    if (ResolveUtil.shouldProcessProperties(classHint)) {
        if (lastParent != null && !(lastParent instanceof GrTypeDefinition) && scriptClass != null) {
            if (!ResolveUtil.processElement(processor, getSyntheticArgsParameter(), state))
                return false;
        }
    }
    return true;
}
Also used : GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) ElementClassHint(com.intellij.psi.scope.ElementClassHint) NameHint(com.intellij.psi.scope.NameHint) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)

Aggregations

GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)49 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)15 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)11 NotNull (org.jetbrains.annotations.NotNull)9 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)9 PsiElement (com.intellij.psi.PsiElement)7 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)6 GrPackageDefinition (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition)6 TextRange (com.intellij.openapi.util.TextRange)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 UsageInfo (com.intellij.usageView.UsageInfo)4 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)4 GrTopStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement)4 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)4 ASTNode (com.intellij.lang.ASTNode)3 Document (com.intellij.openapi.editor.Document)3 PsiFile (com.intellij.psi.PsiFile)3 GroovyScriptClass (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass)3 DaemonCodeAnalyzerImpl (com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerImpl)2 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)2