ScopeOwner (@Nullable final PsiElement element) { if (element == null) { return null; } if (element instanceof PyExpressionCodeFragment) { final PsiElement context = element.getContext(); return context instanceof ScopeOwner ? (ScopeOwner)context : getScopeOwner(context); } 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)); }