@Nullable
  private static PsiElement getAnchorToInsertImportAfter(@NotNull GroovyFile psiFile, @NotNull GrImportStatement statement) {
    final GroovyCodeStyleSettings settings = GroovyCodeStyleSettings.getInstance(psiFile);
    final PackageEntryTable layoutTable = settings.IMPORT_LAYOUT_TABLE;
    final PackageEntry[] entries = layoutTable.getEntries();

    GrImportStatement[] importStatements = psiFile.getImportStatements();
    if (importStatements.length == 0) {
      final GrPackageDefinition definition = psiFile.getPackageDefinition();
      if (definition != null) {
        return definition;
      }

      return getShellComment(psiFile);
    }

    final Comparator<GrImportStatement> comparator = GroovyImportOptimizer.getComparator(settings);

    final int idx = getPackageEntryIdx(entries, statement);

    PsiElement anchor = null;

    for (GrImportStatement importStatement : importStatements) {
      final int i = getPackageEntryIdx(entries, importStatement);
      if (i < idx) {
        anchor = importStatement;
      }
      else if (i > idx) {
        break;
      }
      else if (comparator.compare(statement, importStatement) > 0) {
        anchor = importStatement;
      }
      else {
        break;
      }
    }

    if (anchor == null) anchor = psiFile.getPackageDefinition();
    if (anchor == null) anchor = getShellComment(psiFile);
    if (anchor == null) anchor = importStatements[0].getPrevSibling();
    return anchor;
  }
@Nullable
  private static PsiElement getAnchorToInsertImportAfter(@NotNull GroovyFile psiFile, @NotNull GrImportStatement statement)