Runnable (@NotNull PsiFile file) { if (!(file instanceof PsiJavaFile)) { return EmptyRunnable.getInstance(); } Project project = file.getProject(); final PsiImportList newImportList = JavaCodeStyleManager.getInstance(project).prepareOptimizeImportsResult((PsiJavaFile)file); if (newImportList == null) return EmptyRunnable.getInstance(); return new CollectingInfoRunnable() { private int myImportsAdded; private int myImportsRemoved; @Override public void run() { try { final PsiDocumentManager manager = PsiDocumentManager.getInstance(file.getProject()); final Document document = manager.getDocument(file); if (document != null) { manager.commitDocument(document); } final PsiImportList oldImportList = ((PsiJavaFile)file).getImportList(); assert oldImportList != null; final List<String> oldImports = new ArrayList<>(); for (PsiImportStatementBase statement : oldImportList.getAllImportStatements()) { PsiJavaCodeReferenceElement reference = statement.getImportReference(); oldImports.add(reference == null ? statement.getText() : removeWhiteSpace(reference.getText())); } oldImportList.replace(newImportList); for (PsiImportStatementBase statement : newImportList.getAllImportStatements()) { PsiJavaCodeReferenceElement reference = statement.getImportReference(); if (!oldImports.remove(reference == null ? statement.getText() : removeWhiteSpace(reference.getText()))) { myImportsAdded++; } } myImportsRemoved += oldImports.size(); } catch (IncorrectOperationException e) { LOG.error(e); } } @Override public String getUserNotificationInfo() { if (myImportsRemoved == 0) { return JavaBundle.message("hint.text.rearranged.imports"); } String notification = JavaBundle.message("hint.text.removed.imports", myImportsRemoved, myImportsRemoved == 1 ? 0 : 1); if (myImportsAdded > 0) { notification += JavaBundle.message("hint.text.added.imports", myImportsAdded, myImportsAdded == 1 ? 0 : 1); } return notification; } }; }