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

Java style for Eclipse formats imports incorrectly #273

Open
donce opened this issue Sep 6, 2017 · 6 comments
Open

Java style for Eclipse formats imports incorrectly #273

donce opened this issue Sep 6, 2017 · 6 comments
Labels
lang:java The Java language

Comments

@donce
Copy link
Contributor

donce commented Sep 6, 2017

From https://google.github.io/styleguide/javaguide.html#s3.3.3-import-ordering-and-spacing:

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

Now, all imports are in a single block:

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import java.net.URI;
import java.util.Collections;
import org.eclipse.core.runtime.CoreException;
import org.junit.Test;

Empty line should be left between static and non-static imports, so it should be like this:

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

import java.net.URI;
import java.util.Collections;
import org.eclipse.core.runtime.CoreException;
import org.junit.Test;

This formatting is done by this line in config:

<setting id="org.eclipse.jdt.core.formatter.blank_lines_between_import_groups" value="0"/>

The problem is that Eclipse doesn't seem to support custom spacing between static and non-static groups.

@donce donce changed the title Java style Java style for Eclipse: incorrect formatting of imports Sep 6, 2017
@donce donce changed the title Java style for Eclipse: incorrect formatting of imports Java style for Eclipse formats imports incorrectly Sep 6, 2017
@nicolai86
Copy link

Is there a workaround available besides running the formatter outside of eclipse?

@donce
Copy link
Contributor Author

donce commented Mar 23, 2018

Using IntelliJ? :D

@mkolesnikov
Copy link

For those who still comes to this issue for solution, here is how to Organize Import should be set up:

  • In Organize Import, keep only 2 groups, one for all static and one for everything else
  • In Formatter, Blank lines#Between import groups set 1. (the preview window does not organize imports, that's fine)
    image
    image
    Works in Eclipse Photon

@MarceloLeite2604
Copy link

Thanks @mkolesnikov for the workaround. It helped me out organize import groups on my project.
I also used your second workaround to add a blank line between import groups.
I think GoogleStyle formatting profile defined on eclipse-java-google-style.xml should have this value defined, so it wouldn't be necessary to change it after import. Is it possible to add it?

@oliviercailloux
Copy link

Just stumbled upon this problem. Indeed, this would be a nice and easy change: just set <setting id="org.eclipse.jdt.core.formatter.blank_lines_between_import_groups" value="1"/> instead of 0 currently, here.

@florianlacreuse
Copy link

There has been an open PR #279 for a long time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lang:java The Java language
Projects
None yet
Development

No branches or pull requests

7 participants