boolean (@NotNull IntroduceOperation operation) { PsiElement selectionElement = getOriginalSelectionCoveringElement(operation.getElement()); PsiFile containingFile = selectionElement.getContainingFile(); if (!(containingFile instanceof PyFile)) return false; Editor editor = operation.getEditor(); if (editor == null) return false; List<PsiElement> sameFileRefs = collectReferencedDefinitionsInSameFile(operation.getElement(), operation.getFile()); if (!ContainerUtil.all(sameFileRefs, it -> PyUtil.isTopLevel(it))) return false; PsiElement maxPosition = getLowermostTopLevelStatement(sameFileRefs); if (maxPosition == null) return true; return PsiUtilCore.compareElementsByPosition(maxPosition, selectionElement) <= 0 && !PsiTreeUtil.isAncestor(maxPosition, selectionElement, false); }