Search in sources :

Example 1 with DelegatingScopeProcessor

use of com.intellij.psi.scope.DelegatingScopeProcessor in project intellij-community by JetBrains.

the class PyFileImpl method processDeclarations.

@Override
public boolean processDeclarations(@NotNull final PsiScopeProcessor processor, @NotNull ResolveState resolveState, PsiElement lastParent, @NotNull PsiElement place) {
    final List<String> dunderAll = getDunderAll();
    final List<String> remainingDunderAll = dunderAll == null ? null : new ArrayList<>(dunderAll);
    PsiScopeProcessor wrapper = new DelegatingScopeProcessor(processor) {

        @Override
        public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) {
            if (!super.execute(element, state))
                return false;
            if (remainingDunderAll != null && element instanceof PyElement) {
                remainingDunderAll.remove(((PyElement) element).getName());
            }
            return true;
        }
    };
    Set<PyFile> pyFiles = resolveState.get(PROCESSED_FILES);
    if (pyFiles == null) {
        pyFiles = new HashSet<>();
        resolveState = resolveState.put(PROCESSED_FILES, pyFiles);
    }
    if (pyFiles.contains(this))
        return true;
    pyFiles.add(this);
    for (PyClass c : getTopLevelClasses()) {
        if (c == lastParent)
            continue;
        if (!wrapper.execute(c, resolveState))
            return false;
    }
    for (PyFunction f : getTopLevelFunctions()) {
        if (f == lastParent)
            continue;
        if (!wrapper.execute(f, resolveState))
            return false;
    }
    for (PyTargetExpression e : getTopLevelAttributes()) {
        if (e == lastParent)
            continue;
        if (!wrapper.execute(e, resolveState))
            return false;
    }
    for (PyImportElement e : getImportTargets()) {
        if (e == lastParent)
            continue;
        if (!wrapper.execute(e, resolveState))
            return false;
    }
    for (PyFromImportStatement e : getFromImports()) {
        if (e == lastParent)
            continue;
        if (!e.processDeclarations(wrapper, resolveState, null, this))
            return false;
    }
    if (remainingDunderAll != null) {
        for (String s : remainingDunderAll) {
            if (!PyNames.isIdentifier(s)) {
                continue;
            }
            if (!processor.execute(new LightNamedElement(myManager, PythonLanguage.getInstance(), s), resolveState))
                return false;
        }
    }
    return true;
}
Also used : PsiScopeProcessor(com.intellij.psi.scope.PsiScopeProcessor) NotNull(org.jetbrains.annotations.NotNull) DelegatingScopeProcessor(com.intellij.psi.scope.DelegatingScopeProcessor)

Example 2 with DelegatingScopeProcessor

use of com.intellij.psi.scope.DelegatingScopeProcessor in project intellij-community by JetBrains.

the class PsiReferenceExpressionImpl method processVariants.

@Override
public void processVariants(@NotNull PsiScopeProcessor processor) {
    DelegatingScopeProcessor filterProcessor = new DelegatingScopeProcessor(processor) {

        private PsiElement myResolveContext;

        private final Set<String> myVarNames = new THashSet<>();

        @Override
        public boolean execute(@NotNull final PsiElement element, @NotNull final ResolveState state) {
            return !shouldProcess(element) || super.execute(element, state);
        }

        private boolean shouldProcess(@NotNull PsiElement element) {
            if (element instanceof PsiVariable)
                return ensureNonShadowedVariable((PsiVariable) element);
            if (element instanceof PsiClass)
                return !seemsScrambled((PsiClass) element);
            if (element instanceof PsiPackage)
                return isQualified();
            if (element instanceof PsiMethod)
                return shouldProcessMethod((PsiMethod) element);
            return false;
        }

        private boolean ensureNonShadowedVariable(@NotNull PsiVariable element) {
            if (element instanceof PsiLocalVariable || element instanceof PsiParameter) {
                myVarNames.add(element.getName());
            }
            if (element instanceof PsiField && myVarNames.contains(element.getName())) {
                return false;
            }
            return true;
        }

        private boolean shouldProcessMethod(@NotNull PsiMethod method) {
            PsiReferenceExpressionImpl ref = PsiReferenceExpressionImpl.this;
            return !method.isConstructor() && hasValidQualifier(method, ref, myResolveContext);
        }

        @Override
        public void handleEvent(@NotNull Event event, Object associated) {
            if (event == JavaScopeProcessorEvent.SET_CURRENT_FILE_CONTEXT) {
                myResolveContext = (PsiElement) associated;
            }
            super.handleEvent(event, associated);
        }
    };
    PsiScopesUtil.resolveAndWalk(filterProcessor, this, null, true);
}
Also used : THashSet(gnu.trove.THashSet) NotNull(org.jetbrains.annotations.NotNull) JavaScopeProcessorEvent(com.intellij.psi.scope.JavaScopeProcessorEvent) DelegatingScopeProcessor(com.intellij.psi.scope.DelegatingScopeProcessor)

Example 3 with DelegatingScopeProcessor

use of com.intellij.psi.scope.DelegatingScopeProcessor in project intellij-community by JetBrains.

the class GrImportStatementImpl method processDeclarationsForMultipleElements.

private boolean processDeclarationsForMultipleElements(@NotNull final PsiScopeProcessor processor, @Nullable PsiElement lastParent, @NotNull PsiElement place, @NotNull ResolveState state) {
    GrCodeReferenceElement ref = getImportReference();
    if (ref == null)
        return true;
    if (isStatic()) {
        final PsiElement resolved = ref.resolve();
        if (resolved instanceof PsiClass) {
            state = state.put(ClassHint.RESOLVE_CONTEXT, this);
            final PsiClass clazz = (PsiClass) resolved;
            for (final PsiScopeProcessor each : GroovyResolverProcessor.allProcessors(processor)) {
                if (!clazz.processDeclarations(new DelegatingScopeProcessor(each) {

                    @Override
                    public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) {
                        if (element instanceof PsiMember && ((PsiMember) element).hasModifierProperty(PsiModifier.STATIC)) {
                            return super.execute(element, state);
                        }
                        return true;
                    }
                }, state, lastParent, place))
                    return false;
            }
        }
    } else {
        if (ResolveUtil.shouldProcessClasses(processor.getHint(ElementClassHint.KEY))) {
            String qName = PsiUtil.getQualifiedReferenceText(ref);
            if (qName != null) {
                PsiPackage aPackage = JavaPsiFacade.getInstance(getProject()).findPackage(qName);
                if (aPackage != null && !((GroovyFile) getContainingFile()).getPackageName().equals(aPackage.getQualifiedName())) {
                    state = state.put(ClassHint.RESOLVE_CONTEXT, this);
                    if (!aPackage.processDeclarations(processor, state, lastParent, place))
                        return false;
                }
            }
        }
    }
    return true;
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) PsiScopeProcessor(com.intellij.psi.scope.PsiScopeProcessor) DelegatingScopeProcessor(com.intellij.psi.scope.DelegatingScopeProcessor) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with DelegatingScopeProcessor

use of com.intellij.psi.scope.DelegatingScopeProcessor in project intellij-community by JetBrains.

the class GdkMethodUtil method processMixinToMetaclass.

public static boolean processMixinToMetaclass(GrStatementOwner run, final PsiScopeProcessor processor, ResolveState state, PsiElement lastParent, PsiElement place) {
    GrStatement[] statements = run.getStatements();
    for (GrStatement statement : statements) {
        if (statement == lastParent)
            break;
        final Trinity<PsiClassType, GrReferenceExpression, PsiClass> result = getMixinTypes(statement);
        if (result != null) {
            final PsiClassType subjectType = result.first;
            final GrReferenceExpression qualifier = result.second;
            final PsiClass mixin = result.third;
            for (PsiScopeProcessor each : GroovyResolverProcessor.allProcessors(processor)) {
                if (!mixin.processDeclarations(new MixinMemberContributor.MixinProcessor(each, subjectType, qualifier), state, null, place)) {
                    return false;
                }
            }
        } else {
            Trinity<PsiClassType, GrReferenceExpression, List<GrMethod>> closureResult = getClosureMixins(statement);
            if (closureResult != null) {
                final PsiClassType subjectType = closureResult.first;
                final GrReferenceExpression qualifier = closureResult.second;
                final List<GrMethod> methods = closureResult.third;
                final DelegatingScopeProcessor delegate = new MixinMemberContributor.MixinProcessor(processor, subjectType, qualifier);
                for (GrMethod method : methods) {
                    ResolveUtil.processElement(delegate, method, state);
                }
            }
        }
    }
    return true;
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) PsiScopeProcessor(com.intellij.psi.scope.PsiScopeProcessor) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) List(java.util.List) DelegatingScopeProcessor(com.intellij.psi.scope.DelegatingScopeProcessor)

Aggregations

DelegatingScopeProcessor (com.intellij.psi.scope.DelegatingScopeProcessor)4 PsiScopeProcessor (com.intellij.psi.scope.PsiScopeProcessor)3 NotNull (org.jetbrains.annotations.NotNull)3 JavaScopeProcessorEvent (com.intellij.psi.scope.JavaScopeProcessorEvent)1 THashSet (gnu.trove.THashSet)1 List (java.util.List)1 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)1 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)1 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)1 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)1 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)1