void (@NotNull DataContext dataContext) { final Project project = CommonDataKeys.PROJECT.getData(dataContext); if (project == null) { return; } PsiDocumentManager.getInstance(project).commitAllDocuments(); final Editor editor = BaseCodeInsightAction.getInjectedEditor(project, CommonDataKeys.EDITOR.getData(dataContext)); final VirtualFile[] files = CommonDataKeys.VIRTUAL_FILE_ARRAY.getData(dataContext); PsiFile file = null; PsiDirectory dir; if (editor != null) { file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument()); if (file == null) return; dir = file.getContainingDirectory(); } else if (files != null && ReformatCodeAction.containsOnlyFiles(files)) { new OptimizeImportsProcessor(project, ReformatCodeAction.convertToPsiFiles(files, project), null).run(); return; } else { Project projectContext = PlatformCoreDataKeys.PROJECT_CONTEXT.getData(dataContext); Module moduleContext = LangDataKeys.MODULE_CONTEXT.getData(dataContext); if (projectContext != null || moduleContext != null) { final String text; final boolean hasChanges; if (moduleContext != null) { text = CodeInsightBundle.message("process.scope.module", moduleContext.getName()); hasChanges = VcsFacade.getInstance().hasChanges(moduleContext); } else { text = CodeInsightBundle.message("process.scope.project", projectContext.getPresentableUrl()); hasChanges = VcsFacade.getInstance().hasChanges(projectContext); } Boolean isProcessVcsChangedText = isProcessVcsChangedText(project, text, hasChanges); if (isProcessVcsChangedText == null) { return; } if (moduleContext != null) { OptimizeImportsProcessor processor = new OptimizeImportsProcessor(project, moduleContext); processor.setProcessChangedTextOnly(isProcessVcsChangedText); processor.run(); } else { new OptimizeImportsProcessor(projectContext).run(); } return; } PsiElement element = CommonDataKeys.PSI_ELEMENT.getData(dataContext); if (element == null) return; if (element instanceof PsiDirectoryContainer) { dir = ArrayUtil.getFirstElement(((PsiDirectoryContainer)element).getDirectories()); } else if (element instanceof PsiDirectory) { dir = (PsiDirectory)element; } else { file = element.getContainingFile(); if (file == null) return; dir = file.getContainingDirectory(); } } boolean processDirectory = false; boolean processOnlyVcsChangedFiles = false; if (!ApplicationManager.getApplication().isUnitTestMode() && file == null && dir != null) { String message = CodeInsightBundle.message("process.scope.directory", dir.getName()); OptimizeImportsDialog dialog = new OptimizeImportsDialog(project, message, VcsFacade.getInstance().hasChanges(dir)); dialog.show(); if (!dialog.isOK()) { return; } processDirectory = true; processOnlyVcsChangedFiles = dialog.isProcessOnlyVcsChangedFiles(); } if (processDirectory) { new OptimizeImportsProcessor(project, dir, true, processOnlyVcsChangedFiles).run(); } else { OptimizeImportsProcessor optimizer = new OptimizeImportsProcessor(project, file); if (editor != null && EditorSettingsExternalizable.getInstance().isShowNotificationAfterOptimizeImports()) { optimizer.setCollectInfo(true); optimizer.setPostRunnable(() -> { LayoutCodeInfoCollector collector = optimizer.getInfoCollector(); if (collector != null) { String info = collector.getOptimizeImportsNotification(); if (!editor.isDisposed() && UIUtil.isShowing(editor.getContentComponent())) { String message = info != null ? info : CodeInsightBundle.message("hint.text.no.unused.imports.found"); FileInEditorProcessor.showHint(editor, message, null); } } }); } optimizer.run(); } }