Search in sources :

Example 1 with Scope

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

the class PyReferenceImpl method getResultsFromProcessor.

protected List<RatedResolveResult> getResultsFromProcessor(@NotNull String referencedName, @NotNull PyResolveProcessor processor, @Nullable PsiElement realContext, @Nullable PsiElement resolveRoof) {
    boolean unreachableLocalDeclaration = false;
    boolean resolveInParentScope = false;
    final ResolveResultList resultList = new ResolveResultList();
    final ScopeOwner referenceOwner = ScopeUtil.getScopeOwner(realContext);
    final TypeEvalContext typeEvalContext = myContext.getTypeEvalContext();
    ScopeOwner resolvedOwner = processor.getOwner();
    if (resolvedOwner != null && !processor.getResults().isEmpty()) {
        final Collection<PsiElement> resolvedElements = processor.getElements();
        final Scope resolvedScope = ControlFlowCache.getScope(resolvedOwner);
        if (!resolvedScope.isGlobal(referencedName)) {
            if (resolvedOwner == referenceOwner) {
                final List<Instruction> instructions = PyDefUseUtil.getLatestDefs(resolvedOwner, referencedName, realContext, false, true);
                // TODO: Use the results from the processor as a cache for resolving to latest defs
                final ResolveResultList latestDefs = resolveToLatestDefs(instructions, realContext, referencedName, typeEvalContext);
                if (!latestDefs.isEmpty()) {
                    return latestDefs;
                } else if (resolvedOwner instanceof PyClass || instructions.isEmpty() && allInOwnScopeComprehensions(resolvedElements)) {
                    resolveInParentScope = true;
                } else if (PyiUtil.isInsideStubAnnotation(myElement)) {
                    for (PsiElement element : resolvedElements) {
                        resultList.poke(element, getRate(element, typeEvalContext));
                    }
                    return resultList;
                } else {
                    unreachableLocalDeclaration = true;
                }
            } else if (referenceOwner != null) {
                final Scope referenceScope = ControlFlowCache.getScope(referenceOwner);
                if (referenceScope.containsDeclaration(referencedName)) {
                    unreachableLocalDeclaration = true;
                }
            }
        }
    }
    if (!unreachableLocalDeclaration) {
        if (resolveInParentScope) {
            processor = new PyResolveProcessor(referencedName);
            resolvedOwner = ScopeUtil.getScopeOwner(resolvedOwner);
            if (resolvedOwner != null) {
                PyResolveUtil.scopeCrawlUp(processor, resolvedOwner, referencedName, resolveRoof);
            }
        }
        for (Map.Entry<PsiElement, PyImportedNameDefiner> entry : processor.getResults().entrySet()) {
            final PsiElement resolved = entry.getKey();
            final PyImportedNameDefiner definer = entry.getValue();
            if (resolved != null) {
                if (typeEvalContext.maySwitchToAST(resolved) && isInnerComprehension(realContext, resolved)) {
                    continue;
                }
                if (skipClassForwardReferences(referenceOwner, resolved)) {
                    continue;
                }
                if (definer == null) {
                    resultList.poke(resolved, getRate(resolved, typeEvalContext));
                } else {
                    resultList.poke(definer, getRate(definer, typeEvalContext));
                    resultList.add(new ImportedResolveResult(resolved, getRate(resolved, typeEvalContext), definer));
                }
            } else if (definer != null) {
                resultList.add(new ImportedResolveResult(null, RatedResolveResult.RATE_LOW, definer));
            }
        }
        if (!resultList.isEmpty()) {
            return resultList;
        }
    }
    return resolveByReferenceResolveProviders();
}
Also used : Instruction(com.intellij.codeInsight.controlflow.Instruction) TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) Scope(com.jetbrains.python.codeInsight.dataflow.scope.Scope)

Example 2 with Scope

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

the class PyCodeFragmentUtil method getGlobalWrites.

@NotNull
private static Set<String> getGlobalWrites(@NotNull List<Instruction> instructions, @NotNull ScopeOwner owner) {
    final Scope scope = ControlFlowCache.getScope(owner);
    final Set<String> globalWrites = new LinkedHashSet<>();
    for (Instruction instruction : getWriteInstructions(instructions)) {
        if (instruction instanceof ReadWriteInstruction) {
            final String name = ((ReadWriteInstruction) instruction).getName();
            final PsiElement element = instruction.getElement();
            if (scope.isGlobal(name) || (owner instanceof PsiFile && element instanceof PsiNamedElement && isUsedOutside((PsiNamedElement) element, instructions))) {
                globalWrites.add(name);
            }
        }
    }
    return globalWrites;
}
Also used : ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) Scope(com.jetbrains.python.codeInsight.dataflow.scope.Scope) ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) Instruction(com.intellij.codeInsight.controlflow.Instruction) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with Scope

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

the class PyCodeFragmentUtil method getNonlocalWrites.

@NotNull
private static Set<String> getNonlocalWrites(@NotNull List<Instruction> instructions, @NotNull ScopeOwner owner) {
    final Scope scope = ControlFlowCache.getScope(owner);
    final Set<String> nonlocalWrites = new LinkedHashSet<>();
    for (Instruction instruction : getWriteInstructions(instructions)) {
        if (instruction instanceof ReadWriteInstruction) {
            final String name = ((ReadWriteInstruction) instruction).getName();
            if (scope.isNonlocal(name)) {
                nonlocalWrites.add(name);
            }
        }
    }
    return nonlocalWrites;
}
Also used : ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) Scope(com.jetbrains.python.codeInsight.dataflow.scope.Scope) ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) Instruction(com.intellij.codeInsight.controlflow.Instruction) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with Scope

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

the class PyExtractMethodUtil method processGlobalWrites.

private static void processGlobalWrites(@NotNull final PyFunction function, @NotNull final PyCodeFragment fragment) {
    final Set<String> globalWrites = fragment.getGlobalWrites();
    final Set<String> newGlobalNames = new LinkedHashSet<>();
    final Scope scope = ControlFlowCache.getScope(function);
    for (String name : globalWrites) {
        if (!scope.isGlobal(name)) {
            newGlobalNames.add(name);
        }
    }
    if (!newGlobalNames.isEmpty()) {
        final PyElementGenerator generator = PyElementGenerator.getInstance(function.getProject());
        final PyGlobalStatement globalStatement = generator.createFromText(LanguageLevel.forElement(function), PyGlobalStatement.class, "global " + StringUtil.join(newGlobalNames, ", "));
        final PyStatementList statementList = function.getStatementList();
        statementList.addBefore(globalStatement, statementList.getFirstChild());
    }
}
Also used : Scope(com.jetbrains.python.codeInsight.dataflow.scope.Scope)

Example 5 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)

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