Search in sources :

Example 11 with Scope

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

the class PyExtractMethodUtil method processNonlocalWrites.

private static void processNonlocalWrites(@NotNull PyFunction function, @NotNull PyCodeFragment fragment) {
    final Set<String> nonlocalWrites = fragment.getNonlocalWrites();
    final Set<String> newNonlocalNames = new LinkedHashSet<>();
    final Scope scope = ControlFlowCache.getScope(function);
    for (String name : nonlocalWrites) {
        if (!scope.isNonlocal(name)) {
            newNonlocalNames.add(name);
        }
    }
    if (!newNonlocalNames.isEmpty()) {
        final PyElementGenerator generator = PyElementGenerator.getInstance(function.getProject());
        final PyNonlocalStatement nonlocalStatement = generator.createFromText(LanguageLevel.forElement(function), PyNonlocalStatement.class, "nonlocal " + StringUtil.join(newNonlocalNames, ", "));
        final PyStatementList statementList = function.getStatementList();
        statementList.addBefore(nonlocalStatement, statementList.getFirstChild());
    }
}
Also used : Scope(com.jetbrains.python.codeInsight.dataflow.scope.Scope)

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