Search in sources :

Example 11 with ScopeOwner

use of com.jetbrains.python.codeInsight.controlflow.ScopeOwner in project intellij-community by JetBrains.

the class ScopeUtil method getScopeOwner.

/**
   * Return the scope owner for the element.
   *
   * Scope owner is not always the first ScopeOwner parent of the element. Some elements are resolved in outer scopes.
   *
   * This method does not access AST if underlying PSI is stub based.
   */
@Nullable
public static ScopeOwner getScopeOwner(@Nullable final PsiElement element) {
    if (element == null) {
        return null;
    }
    if (element instanceof StubBasedPsiElement) {
        final StubElement stub = ((StubBasedPsiElement) element).getStub();
        if (stub != null) {
            StubElement parentStub = stub.getParentStub();
            while (parentStub != null) {
                final PsiElement parent = parentStub.getPsi();
                if (parent instanceof ScopeOwner) {
                    return (ScopeOwner) parent;
                }
                parentStub = parentStub.getParentStub();
            }
            return null;
        }
    }
    return CachedValuesManager.getCachedValue(element, () -> CachedValueProvider.Result.create(calculateScopeOwnerByAST(element), PsiModificationTracker.MODIFICATION_COUNT));
}
Also used : ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) StubBasedPsiElement(com.intellij.psi.StubBasedPsiElement) StubElement(com.intellij.psi.stubs.StubElement) StubBasedPsiElement(com.intellij.psi.StubBasedPsiElement) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 12 with ScopeOwner

use of com.jetbrains.python.codeInsight.controlflow.ScopeOwner in project intellij-community by JetBrains.

the class ScopeUtil method getDeclarationScopeOwner.

@Nullable
public static ScopeOwner getDeclarationScopeOwner(PsiElement anchor, String name) {
    if (name != null) {
        final ScopeOwner originalScopeOwner = getScopeOwner(anchor);
        ScopeOwner scopeOwner = originalScopeOwner;
        while (scopeOwner != null) {
            if (!(scopeOwner instanceof PyClass) || scopeOwner == originalScopeOwner) {
                Scope scope = ControlFlowCache.getScope(scopeOwner);
                if (scope.containsDeclaration(name)) {
                    return scopeOwner;
                }
            }
            scopeOwner = getScopeOwner(scopeOwner);
        }
    }
    return null;
}
Also used : ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) Nullable(org.jetbrains.annotations.Nullable)

Example 13 with ScopeOwner

use of com.jetbrains.python.codeInsight.controlflow.ScopeOwner in project intellij-community by JetBrains.

the class PyExtractMethodUtil method collectScopes.

@NotNull
private static List<PsiElement> collectScopes(@NotNull PsiElement anchor, @NotNull PyFunction generatedMethod) {
    final ScopeOwner owner = ScopeUtil.getScopeOwner(anchor);
    if (owner instanceof PsiFile)
        return Collections.emptyList();
    final List<PsiElement> scope = new ArrayList<>();
    if (owner instanceof PyFunction) {
        scope.add(owner);
        final PyClass containingClass = ((PyFunction) owner).getContainingClass();
        if (containingClass != null) {
            for (PyFunction function : containingClass.getMethods()) {
                if (!function.equals(owner) && !function.equals(generatedMethod)) {
                    scope.add(function);
                }
            }
        }
    }
    return scope;
}
Also used : ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with ScopeOwner

use of com.jetbrains.python.codeInsight.controlflow.ScopeOwner in project intellij-community by JetBrains.

the class PyContainingFileRenamerFactory method isApplicable.

@Override
public boolean isApplicable(PsiElement element) {
    if (!(element instanceof PyClass)) {
        return false;
    }
    ScopeOwner scopeOwner = PsiTreeUtil.getParentOfType(element, ScopeOwner.class);
    if (scopeOwner instanceof PyFile) {
        String className = ((PyClass) element).getName();
        String fileName = FileUtil.getNameWithoutExtension(scopeOwner.getName());
        return fileName.equalsIgnoreCase(className);
    }
    return false;
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) PyFile(com.jetbrains.python.psi.PyFile)

Example 15 with ScopeOwner

use of com.jetbrains.python.codeInsight.controlflow.ScopeOwner in project intellij-community by JetBrains.

the class RenamePyVariableProcessor method substituteElementToRename.

@Nullable
@Override
public PsiElement substituteElementToRename(PsiElement element, @Nullable Editor editor) {
    if (element instanceof PyLambdaExpression) {
        final PyLambdaExpression lambdaExpression = (PyLambdaExpression) element;
        final ScopeOwner owner = ScopeUtil.getScopeOwner(lambdaExpression);
        if (owner instanceof PyClass) {
            final PyClass cls = (PyClass) owner;
            final Property property = cls.findPropertyByCallable(lambdaExpression);
            if (property != null) {
                final PyTargetExpression site = property.getDefinitionSite();
                if (site != null) {
                    return site;
                }
            }
        }
        return null;
    }
    return element;
}
Also used : ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)51 PsiElement (com.intellij.psi.PsiElement)25 Nullable (org.jetbrains.annotations.Nullable)18 NotNull (org.jetbrains.annotations.NotNull)14 Pair (com.intellij.openapi.util.Pair)6 QualifiedName (com.intellij.psi.util.QualifiedName)6 ArrayList (java.util.ArrayList)6 Instruction (com.intellij.codeInsight.controlflow.Instruction)4 TextRange (com.intellij.openapi.util.TextRange)4 PsiFile (com.intellij.psi.PsiFile)4 Scope (com.jetbrains.python.codeInsight.dataflow.scope.Scope)4 List (java.util.List)4 LookupElement (com.intellij.codeInsight.lookup.LookupElement)3 Project (com.intellij.openapi.project.Project)3 Ref (com.intellij.openapi.util.Ref)3 PsiLanguageInjectionHost (com.intellij.psi.PsiLanguageInjectionHost)3 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)3 TypeEvalContext (com.jetbrains.python.psi.types.TypeEvalContext)3 CannotCreateCodeFragmentException (com.intellij.codeInsight.codeFragment.CannotCreateCodeFragmentException)2 ASTNode (com.intellij.lang.ASTNode)2