impsort-maven-plugin
impsort-maven-plugin copied to clipboard
Additional blank lines above or below imports not allowed
For [reasons] I currently have a format (validated via formatter-maven-plugin) that has two blank lines above and two below the imports, which this plug-in has an issue with.
Since I simplified the whole imports ordering (why make it complicated when no one outside of diffs looks at them anyway) I'm considering to reduce it to one blank line each. But either way, the following issue might still be worth a little fix:
Currently, this plug-in seems to expect and enforce exactly one blank line above and one below the imports.
If possible, the sort goal should just preserve the number of blank lines around the imports.
But more importantly, the check goal should ignore the blank lines around the imports. Currently it complains with Imports are not sorted in [path] when there is more than one blank line around the imports, which is quite confusing and misleading. After comparing with how sort would like the imports, the only difference was the number of blank lines above and below.
I'm currently using this plug-in with its default configuration and executing the check goal during the validate phase.
This seems related to, and could be bundled with #22 / #28 to support explicit declaration of blank lines before, after, and in between groupings.
This could also be implemented trivially with a numeric parameter for number of blank lines before/after imports (or to be used if there are no imports), but even here, I can imagine a bunch of niche cases people might want: separate number of blank lines before and after, a different number of blank lines entirely if there are no imports at all. There are truly an infinite number of options people might want... in general, I think it's reasonable to restrict the plugin to supporting a smaller set of reasonable options.
So, I'm not sure it's worth supporting this. While the request to support two lines of padding is reasonable... we have to draw the line somewhere, and I think this style is already too niche to bother. That said, it might be trivial to include support for this with an explicit configuration of spacing, while implementing support for #22 / #28.
Considering this plug-in is about sorting imports I was surprised it even touched the lines around the imports at all. I expected it to rearrange imports between the line of the first import and the line of the last import. So blank lines above or below the imports would simply be preserved the way they are. No additional configuration necessary. That way it would work even better together with formatter-maven-plugin, which can control the blank lines around imports, but cannot sort imports.
Though, if the current behavior of condensing blank lines to a single one each should be preserved, a flag like "preserveBlankLinesAroundImports" could be introduced.
a flag like "preserveBlankLinesAroundImports" could be introduced.
Merely preserving existing blank lines isn't as simple as it sounds. What if all the imports are removed, and there were 2 lines prior and 3 lines after? Do you keep all 5 lines? Or 2? Or 3? What if there was 1 line before and one line after? Are you left with 2 lines after sorting, or just 1?
This plugin necessarily needs to deal with blank lines surrounding the imports, because conceptually, it is organizing the entire "imports section", and not just the set of import statements. Typically, the formatter plugin runs first, because this plugin can't handle some unformatted edge cases. So, we can't just rely on the formatter to handle the space around the imports, since that will typically already have been run.