Search in sources :

Example 6 with Scope

use of com.jetbrains.python.codeInsight.dataflow.scope.Scope in project intellij-community by JetBrains.

the class PyIterableVariableMacro method getVisibleNamedElements.

@NotNull
private static List<PsiNamedElement> getVisibleNamedElements(@NotNull PsiElement anchor) {
    final List<PsiNamedElement> results = new ArrayList<>();
    for (ScopeOwner owner = ScopeUtil.getScopeOwner(anchor); owner != null; owner = ScopeUtil.getScopeOwner(owner)) {
        final Scope scope = ControlFlowCache.getScope(owner);
        results.addAll(scope.getNamedElements());
        StreamEx.of(scope.getImportedNameDefiners()).filter(definer -> !PyImplicitImportNameDefiner.class.isInstance(definer)).flatMap(definer -> StreamSupport.stream(definer.iterateNames().spliterator(), false)).select(PsiNamedElement.class).forEach(results::add);
    }
    return results;
}
Also used : TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext) PyNames(com.jetbrains.python.PyNames) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PyType(com.jetbrains.python.psi.types.PyType) com.intellij.codeInsight.template(com.intellij.codeInsight.template) ScopeUtil(com.jetbrains.python.codeInsight.dataflow.scope.ScopeUtil) PyImplicitImportNameDefiner(com.jetbrains.python.psi.PyImplicitImportNameDefiner) ArrayList(java.util.ArrayList) Nullable(org.jetbrains.annotations.Nullable) PyTypedElement(com.jetbrains.python.psi.PyTypedElement) List(java.util.List) Scope(com.jetbrains.python.codeInsight.dataflow.scope.Scope) PyABCUtil(com.jetbrains.python.psi.types.PyABCUtil) StreamEx(one.util.streamex.StreamEx) PsiElement(com.intellij.psi.PsiElement) ControlFlowCache(com.jetbrains.python.codeInsight.controlflow.ControlFlowCache) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) PsiNamedElement(com.intellij.psi.PsiNamedElement) StreamSupport(java.util.stream.StreamSupport) NotNull(org.jetbrains.annotations.NotNull) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) Scope(com.jetbrains.python.codeInsight.dataflow.scope.Scope) PsiNamedElement(com.intellij.psi.PsiNamedElement) ArrayList(java.util.ArrayList) PyImplicitImportNameDefiner(com.jetbrains.python.psi.PyImplicitImportNameDefiner) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with Scope

use of com.jetbrains.python.codeInsight.dataflow.scope.Scope in project intellij-community by JetBrains.

the class PyQualifiedReference method collectAssignedAttributes.

/**
   * Returns expressions accessible from scope of "anchor" with names that start with  provided "qualifierQName".
   * Can be used for completion.
   */
@NotNull
public static Collection<PyExpression> collectAssignedAttributes(@NotNull final QualifiedName qualifierQName, @NotNull final PsiElement anchor) {
    final Set<String> names = new HashSet<>();
    final List<PyExpression> results = new ArrayList<>();
    for (ScopeOwner owner = ScopeUtil.getScopeOwner(anchor); owner != null; owner = ScopeUtil.getScopeOwner(owner)) {
        final Scope scope = ControlFlowCache.getScope(owner);
        for (final PyTargetExpression target : scope.getTargetExpressions()) {
            final QualifiedName targetQName = target.asQualifiedName();
            if (targetQName != null) {
                if (targetQName.getComponentCount() == qualifierQName.getComponentCount() + 1 && targetQName.matchesPrefix(qualifierQName)) {
                    final String name = target.getName();
                    if (!names.contains(name)) {
                        names.add(name);
                        results.add(target);
                    }
                }
            }
        }
    }
    return results;
}
Also used : ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) Scope(com.jetbrains.python.codeInsight.dataflow.scope.Scope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ProjectScope(com.intellij.psi.search.ProjectScope) QualifiedName(com.intellij.psi.util.QualifiedName) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with Scope

use of com.jetbrains.python.codeInsight.dataflow.scope.Scope in project intellij-community by JetBrains.

the class PyTargetExpressionImpl method getUseScope.

@NotNull
@Override
public SearchScope getUseScope() {
    if (isQualified()) {
        return super.getUseScope();
    }
    final ScopeOwner owner = ScopeUtil.getScopeOwner(this);
    if (owner != null) {
        final Scope scope = ControlFlowCache.getScope(owner);
        if (scope.isGlobal(getName())) {
            return GlobalSearchScope.projectScope(getProject());
        }
        if (scope.isNonlocal(getName())) {
            return new LocalSearchScope(getContainingFile());
        }
    }
    // find highest level function containing our var
    PyElement container = this;
    while (true) {
        PyElement parentContainer = PsiTreeUtil.getParentOfType(container, PyFunction.class, PyClass.class);
        if (parentContainer instanceof PyClass) {
            if (isQualified()) {
                return super.getUseScope();
            }
            break;
        }
        if (parentContainer == null) {
            break;
        }
        container = parentContainer;
    }
    if (container instanceof PyFunction) {
        return new LocalSearchScope(container);
    }
    return super.getUseScope();
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) Scope(com.jetbrains.python.codeInsight.dataflow.scope.Scope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with Scope

use of com.jetbrains.python.codeInsight.dataflow.scope.Scope in project intellij-community by JetBrains.

the class ScopeImpl method getNamedElements.

@NotNull
@Override
public Collection<PsiNamedElement> getNamedElements(String name, boolean includeNestedGlobals) {
    if (myNamedElements == null) {
        collectDeclarations();
    }
    if (myNamedElements.containsKey(name)) {
        final Collection<PsiNamedElement> elements = myNamedElements.get(name);
        elements.forEach(PyPsiUtils::assertValid);
        return elements;
    }
    if (includeNestedGlobals && isGlobal(name)) {
        for (Scope scope : myNestedScopes) {
            final Collection<PsiNamedElement> globals = scope.getNamedElements(name, true);
            if (!globals.isEmpty()) {
                globals.forEach(PyPsiUtils::assertValid);
                return globals;
            }
        }
    }
    return Collections.emptyList();
}
Also used : PyPsiUtils(com.jetbrains.python.psi.impl.PyPsiUtils) Scope(com.jetbrains.python.codeInsight.dataflow.scope.Scope) PsiNamedElement(com.intellij.psi.PsiNamedElement) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with Scope

use of com.jetbrains.python.codeInsight.dataflow.scope.Scope in project intellij-community by JetBrains.

the class ControlFlowCache method getScope.

@NotNull
public static Scope getScope(@NotNull ScopeOwner element) {
    SoftReference<Scope> ref = element.getUserData(SCOPE_KEY);
    Scope scope = SoftReference.dereference(ref);
    if (scope == null) {
        scope = new ScopeImpl(element);
        element.putUserData(SCOPE_KEY, new SoftReference<>(scope));
    }
    return scope;
}
Also used : Scope(com.jetbrains.python.codeInsight.dataflow.scope.Scope) ScopeImpl(com.jetbrains.python.codeInsight.dataflow.scope.impl.ScopeImpl) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Scope (com.jetbrains.python.codeInsight.dataflow.scope.Scope)11 NotNull (org.jetbrains.annotations.NotNull)7 ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)5 Instruction (com.intellij.codeInsight.controlflow.Instruction)3 PsiNamedElement (com.intellij.psi.PsiNamedElement)2 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)2 ReadWriteInstruction (com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction)2 TypeEvalContext (com.jetbrains.python.psi.types.TypeEvalContext)2 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)1 com.intellij.codeInsight.template (com.intellij.codeInsight.template)1 PsiElement (com.intellij.psi.PsiElement)1 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)1 ProjectScope (com.intellij.psi.search.ProjectScope)1 SearchScope (com.intellij.psi.search.SearchScope)1 QualifiedName (com.intellij.psi.util.QualifiedName)1 PyNames (com.jetbrains.python.PyNames)1 ControlFlowCache (com.jetbrains.python.codeInsight.controlflow.ControlFlowCache)1 ScopeUtil (com.jetbrains.python.codeInsight.dataflow.scope.ScopeUtil)1 ScopeImpl (com.jetbrains.python.codeInsight.dataflow.scope.impl.ScopeImpl)1