void (final UsageInfo @NotNull [] usages) { final MultiMap<PsiElement, UsageInfo> usagesByElement = MultiMap.create(); for (UsageInfo usage : usages) { usagesByElement.putValue(((MyUsageInfo)usage).myMovedElement, usage); } CommandProcessor.getInstance().executeCommand(myProject, () -> ApplicationManager.getApplication().runWriteAction(() -> { final PyFile destination = PyClassRefactoringUtil.getOrCreateFile(myDestination, myProject); CommonRefactoringUtil.checkReadOnlyStatus(myProject, destination); final LinkedHashSet<PsiFile> optimizeImportsTargets = Sets.newLinkedHashSet(mySourceFiles); for (final SmartPsiElementPointer<PsiNamedElement> pointer : myElements) { // TODO: Check for resulting circular imports final PsiNamedElement e = pointer.getElement(); if (e == null) { continue; } CommonRefactoringUtil.checkReadOnlyStatus(myProject, e); assert e instanceof PyClass || e instanceof PyFunction || e instanceof PyTargetExpression; final String name = e.getName(); if (name == null) { continue; } if (e instanceof PyClass && destination.findTopLevelClass(name) != null) { throw new IncorrectOperationException( PyBundle.message("refactoring.move.error.destination.file.contains.class", name)); } if (e instanceof PyFunction && destination.findTopLevelFunction(name) != null) { throw new IncorrectOperationException( PyBundle.message("refactoring.move.error.destination.file.contains.function", name)); } if (e instanceof PyTargetExpression && destination.findTopLevelAttribute(name) != null) { throw new IncorrectOperationException( PyBundle.message("refactoring.move.error.destination.file.contains.global.variable", name)); } final Collection<UsageInfo> usageInfos = usagesByElement.get(e); final boolean usedFromOutside = ContainerUtil.exists(usageInfos, usageInfo -> { final PsiElement element = usageInfo.getElement(); return element != null && !PsiTreeUtil.isAncestor(e, element, false); }); if (usedFromOutside) { PyMoveRefactoringUtil.checkValidImportableFile(e, destination.getVirtualFile()); } final PyMoveSymbolResult results = new PyMoveSymbolProcessor(e, destination, usageInfos, myElements).moveElement(); optimizeImportsTargets.addAll(results.getOptimizeImportsTargets()); } for (PsiFile file : optimizeImportsTargets) { PyClassRefactoringUtil.optimizeImports(file); } }), getRefactoringName(), null); }