}
public static void optimizeImportsByIntellij(PsiFile psiFile) {
Project project = psiFile.getProject();
final PsiImportList newImportList = JavaCodeStyleManager.getInstance(project).prepareOptimizeImportsResult(
(PsiJavaFile) psiFile);
try {
final PsiDocumentManager manager = PsiDocumentManager.getInstance(project);
final Document document = manager.getDocument(psiFile);
if (document != null) {
manager.commitDocument(document);
}
final PsiImportList oldImportList = ((PsiJavaFile) psiFile).getImportList();
assert oldImportList != null;
if (newImportList != null) {
oldImportList.replace(newImportList);
}
manager.doPostponedOperationsAndUnblockDocument(document);
} catch (IncorrectOperationException e) {
LOG.error(e);
}