void (@NotNull final Project project, final Set<PsiJavaFile> javaFiles) { CodeStyleManager.getInstance(project).performActionWithFormatterDisabled( (Runnable)() -> PsiDocumentManager.getInstance(project).commitAllDocuments()); DumbService.getInstance(project).completeJustSubmittedTasks(); final List<SmartPsiElementPointer<PsiImportStatementBase>> redundants = new ArrayList<>(); final Runnable findRedundantImports = () -> ReadAction.run(() -> { final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(project); final ProgressIndicator progressIndicator = ProgressManager.getInstance().getProgressIndicator(); if (progressIndicator != null) { progressIndicator.setIndeterminate(false); } final SmartPointerManager pointerManager = SmartPointerManager.getInstance(project); int i = 0; final int fileCount = javaFiles.size(); for (PsiJavaFile file : javaFiles) { if (file.isValid()) { final VirtualFile virtualFile = file.getVirtualFile(); if (virtualFile != null) { if (progressIndicator != null) { progressIndicator.setText2(virtualFile.getPresentableUrl()); progressIndicator.setFraction((double)i++ / fileCount); } final Collection<PsiImportStatementBase> perFile = styleManager.findRedundantImports(file); if (perFile != null) { for (PsiImportStatementBase redundant : perFile) { redundants.add(pointerManager.createSmartPsiElementPointer(redundant)); } } } } } }); String removingRedundantImportsTitle = JavaRefactoringBundle.message("removing.redundant.imports.progress.title"); if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(findRedundantImports, removingRedundantImportsTitle, false, project)) return; ApplicationManager.getApplication().runWriteAction(() -> { final SequentialModalProgressTask progressTask = new SequentialModalProgressTask(project, removingRedundantImportsTitle, false); progressTask.setMinIterationTime(200); progressTask.setTask(new OptimizeImportsTask(progressTask, redundants)); ProgressManager.getInstance().run(progressTask); }); }