void (@NotNull PyReferenceExpression sourceNode, @NotNull PsiElement targetNode, PsiElement @NotNull [] otherMovedElements) { try { PsiNamedElement target = sourceNode.getCopyableUserData(ENCODED_IMPORT); final String asName = sourceNode.getCopyableUserData(ENCODED_IMPORT_AS); final Boolean useFromImport = sourceNode.getCopyableUserData(ENCODED_USE_FROM_IMPORT); if (target == null) return; if (target instanceof PsiDirectory) { target = (PsiNamedElement)PyUtil.getPackageElement((PsiDirectory)target, sourceNode); } if (target instanceof PyFunction f) { final PyClass c = f.getContainingClass(); if (c != null && c.multiFindInitOrNew(false, null).contains(f)) { target = c; } } if (PsiTreeUtil.isAncestor(targetNode.getContainingFile(), target, false)) return; if (ArrayUtil.contains(target, otherMovedElements)) return; if (target instanceof PyFile || target instanceof PsiDirectory) { PyPsiRefactoringUtil.insertImport(targetNode, target, asName, useFromImport != null ? useFromImport : true); } else { PyPsiRefactoringUtil.insertImport(targetNode, target, asName, true); } } finally { sourceNode.putCopyableUserData(ENCODED_IMPORT, null); sourceNode.putCopyableUserData(ENCODED_IMPORT_AS, null); sourceNode.putCopyableUserData(ENCODED_USE_FROM_IMPORT, null); } }