void (@NotNull Project project, @NotNull PsiFile file, @Nullable Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) { final PsiFile myFile = startElement.getContainingFile(); if (!FileModificationService.getInstance().prepareFileForWrite(myFile)) return; Module module = ModuleUtilCore.findModuleForFile(myFile); Stream<SourceFolder> testRoots = getTestRoots(ModuleRootManager.getInstance(Objects.requireNonNull(module))); DirectoryChooser chooser = new DirectoryChooser(project); chooser.setTitle(JavaRefactoringBundle.message("select.source.root.chooser.title")); PsiManager manager = myFile.getManager(); PsiDirectory[] directories = testRoots.map(ContentFolder::getFile).filter(Objects::nonNull) .map(vFile -> manager.findDirectory(vFile)) .filter(Objects::nonNull).toArray(PsiDirectory[]::new); chooser.fillList(directories, null, project, ""); if (!chooser.showAndGet()) { return; } PsiDirectory sourceRoot = chooser.getSelectedDirectory(); if (sourceRoot == null) return; PsiPackage targetPackage = JavaDirectoryService.getInstance().getPackage(myFile.getContainingDirectory()); PackageWrapper wrapper = PackageWrapper.create(targetPackage); PsiDirectory selectedDirectory = WriteAction.compute(() -> CommonJavaRefactoringUtil.createPackageDirectoryInSourceRoot(wrapper, sourceRoot.getVirtualFile())); try { String error; try { error = RefactoringMessageUtil.checkCanCreateFile(selectedDirectory, myFile.getName()); } catch (IncorrectOperationException e) { error = e.getLocalizedMessage(); } if (error != null) { Messages.showMessageDialog(project, error, CommonBundle.getErrorTitle(), Messages.getErrorIcon()); return; } SingleSourceRootMoveDestination moveDestination = new SingleSourceRootMoveDestination(wrapper, selectedDirectory); JavaRefactoringFactory.getInstance(project) .createMoveClassesOrPackages(((PsiJavaFile) myFile).getClasses(), moveDestination, false, false).run(); } catch (IncorrectOperationException e) { LOG.error(e); } }