void () { 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); } }