Add a blank line between static and non-static imports in Eclipse automatically
Asked Answered
N

2

7

I want to format my code according to the Google Java Style.

There are some issues when it comes to import statements. The one rule I can not fulfill other than by manual editing each class is this one:

If there are both static and non-static imports, a single blank line separates the two blocks.

Apparently, there is no way to configure the Organize Imports action in Eclipse to add this blank line.

Are there any workarounds, plugins or plans to add such a feature (or is my only choice to write one myself or use a different IDE)?

Nystagmus answered 2/5, 2018 at 10:19 Comment(0)
C
9

It's quite straightforward in Eclipse, here's how to do it:

In "Organize imports" define only two groups:

  1. * - all unmatched static imports
  2. * - all unmatched type imports

In the formatter set Blank LinesBlank lines in compilation unitBetween import groups to 1.

Chequered answered 11/5, 2019 at 20:57 Comment(0)
B
7

I think this issue is related to the formatter used rather than the Organize Imports action per se.

The formatter options are available at Window > Preferences > Java > Code Style > Formatter. When editing a profile, there's a tab named Blank Lines and an option Between import groups which you may find useful.

When I have this option set to 1, Organize Imports action inserts a blank line between import group and import static group.

Bathymetry answered 2/5, 2018 at 11:46 Comment(4)
You're right, but it's adding blank lines after each import group as well (java.util, fr, com, ...), which I think is a side effectUlcer
@Ulcer indeed, unfortunately; but I don't see it as a disadvantage though. I don't think that Eclipse actually distinguishes import-import space from import-static import space, but you could file a bug in this case as an enhancement.Bathymetry
nah I'm good, and I'm sure they have more important issues to take care of... seems this is the best way to do it then!Ulcer
I'll add a feature request. It's not critical but Google Java Style is quite common and it's annoying if Eclipse doesn't fully support it.Nystagmus

© 2022 - 2024 — McMap. All rights reserved.