Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportOrder without blank line between groups #1375

Closed
tisonkun opened this issue Oct 18, 2022 · 3 comments · Fixed by #1401
Closed

ImportOrder without blank line between groups #1375

tisonkun opened this issue Oct 18, 2022 · 3 comments · Fixed by #1401

Comments

@tisonkun
Copy link
Contributor

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

@nedtwigg
Copy link
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.

@tisonkun
Copy link
Contributor Author

@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.

@tisonkun
Copy link
Contributor Author

tisonkun commented Jan 10, 2023

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants