void () { if (myPySettings.OPTIMIZE_IMPORTS_SORT_IMPORTS) { for (ImportPriority priority : myGroups.keySet()) { final List<PyImportStatementBase> imports = myGroups.get(priority); imports.sort(AddImportHelper.getSameGroupImportsComparator(myFile)); myGroups.put(priority, imports); } } final PyImportStatementBase firstImport = myImportBlock.get(0); final List<PsiComment> boundComments = collectPrecedingLineComments(firstImport).getFirst(); final PsiElement firstElementToRemove = boundComments.isEmpty() ? firstImport : boundComments.get(0); final PyImportStatementBase lastImport = ContainerUtil.getLastItem(myImportBlock); assert lastImport != null; addImportsAfter(lastImport); deleteRangeThroughDocument(firstElementToRemove, PyPsiUtils.getNextNonWhitespaceSibling(lastImport).getPrevSibling()); }