Skip to content

ImportOrder without blank line between groups #1375

Closed

Description

@tisonkun

Currently, when I configure ImportOrder as \\#, in the maven plugin, I intend to format the imports as:

import static xxx;
import static yyy;
import zzz;

... but it actually produces:

import static xxx;
import static yyy;

import zzz;

Is there an approach or we can make a patch to support the former format?

cc @nedtwigg

Activity

  1. nedtwigg commented on Oct 19, 2022

    @nedtwigg
    Member

    The importsorter is a mess. My personal opinion is that I only care that the imports are consistent, I don't care at all how they are consistent.

    I'm happy to take a PR which adds whatever feature anyone wants, but it can't change behavior for existing users. That's a difficult but not impossible thing, which I have lumped into the "importsorter" tag.

  2. tisonkun commented on Jan 10, 2023

    @tisonkun
    ContributorAuthor

    @nedtwigg I actually find a way to do this:

    --- a/lib/src/main/java/com/diffplug/spotless/java/ImportSorterImpl.java
    +++ b/lib/src/main/java/com/diffplug/spotless/java/ImportSorterImpl.java
     /*not thread safe*/
     // Based on ImportSorterImpl from https://github.com/krasa/EclipseCodeFormatter,
     // which itself is licensed under the Apache 2.0 license.
    @@ -32,6 +34,8 @@ final class ImportSorterImpl {
            private static final String STATIC_SYMBOL = "\\#";
            private static final String SUBGROUP_SEPARATOR = "|";
     
    +       private final int blankLinesBetweenImportGroups;
    +
            private final List<ImportsGroup> importsGroups;
            private final Map<String, List<String>> matchingImports = new HashMap<>();
            private final List<String> notMatching = new ArrayList<>();
    @@ -200,7 +204,7 @@ final class ImportSorterImpl {
     
                    for (String s : sortedImported) {
                            if (s.equals(ImportSorter.N)) {
    -                               strings.add(s);
    +                               strings.add(Strings.repeat(ImportSorter.N.charAt(0), blankLinesBetweenImportGroups));
                            } else {
                                    strings.add(String.format(lineFormat, s) + ImportSorter.N);
                            }

    ... which comes from how Eclipse does it.

    But I noticed @murdos introduced a new concept "subGroups" to try to resolve it with more complex string interpreting syntax. Let me try it out.

  3. tisonkun commented on Jan 10, 2023

    @tisonkun
    ContributorAuthor

    Somehow these two configs can live together that it doesn't affect "subGroups", but..what a mess, lol.

    I'm a bit confused how can I configure a simple:

    import static ...; // sorted alphabetically
    import  ...; // sorted alphabetically
    

    with the subgroup feature.

    See #1401 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions