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));
}
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;
}
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;
}
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;
}
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;
}
Aggregations