Code formating and import optimization
Answered
Hi!
Just migrated from Eclipse and I am quite satisfied except two things:
1) I've specified line lenght (right margin) to 120 characters and is works fine for lines longer than that.
But what about SHORTER lines? For example in Eclipse this:
user.
getId();
would become this after formatting:
user.getId();
because those lines belong together and don't exceed 120 characters. IDEA just won't do it.
2) Typo in import isn't fixed after optimizing imports, for example:
import org.springframework.whatever.ClassUtils;
should be removed and new import should be added (or I should be asked which one if there are more possiblities).
Hope I described both "issued" correctly.
Zbynek
Please sign in to leave a comment.
1. Disable Code Style | Java | Wrapping and Braces | Keep when reformatting | Line Breaks. If you want exactly the same formatting as in Eclipse, consider using https://plugins.jetbrains.com/idea/plugin/6546-eclipse-code-formatter plug-in instead.
2. Unused import should be removed, make sure this option is enabled:
Otherwise you'll need to call Optimize Imports action separately: https://www.jetbrains.com/help/idea/2016.3/optimizing-imports.html. Or you can enable the option to automatically optimize imports on the fly:
Hey,
well thanks for reply but:
2) Yes both checkboxes are checked and nothing happens. I should mention that the class contains reference to "MyClass" that hasn't been imported because there is "import x.y.z.MyClass" (with "y.z" in red color) and the class is actully in "x.x.x.MyClass". If there is import of a class not used at all then it is properly removed. But these ones are completely ignored.
Zbynek
1. See the updated answer (Code Style | Java | Wrapping and Braces | Keep when reformatting | Line Breaks).
2. Try Alt+Enter on the import statement or the red imported class usage, do you get the suggestions to remove the import or fix it? Do you have "Add unambiguous imports on the fly" option enabled?
Hey,
1) Working when near brackets:
user.find
id);
is properly formatted but
user.
find(id);
is not. Also doesn't work in JavaDoc.
2) When I do Alt+Enter on import I got "Find JAR on web", "Add Maven dependency...", "Optimize import" with sub-menu (nothing working there) and "Create Test" with sub-menu. When done on class usage I got "Create class", "Create enum", "Create interface", "Find JAR on web", "Add Maven dependency...", "Go to implementation(s)", "Convert to class", "Create Test", "Implement interface", "Unimplement interface" and "Make package-private". Yep I got the "Add unambiguous imports on the fly" enabled.
I may stress out that the code is copied (not written manually) - but that shouln't be an issue here.
Btw if there is a way to post screenshots it would simplify stuff...
Zbynek
1. I've asked the responsible developer to comment on it. If the issue is urgent, I recommend switching to Eclipse Formatter plug-in, this way you will get the same results as you are already familiar with from using Eclipse.
2. I see, since the import is marked as error, it's not optimized, here is the relevant ticket: https://youtrack.jetbrains.com/issue/IDEA-132179. Feel free to vote. See https://intellij-support.jetbrains.com/hc/articles/207241135 if you are not familiar with YouTrack.
3. You can attach the screenshots here:
Hi,
1) It's no biggie, I just thought I missed some settings.
2) Well, voted, too bad the ticket is from 2014 :(
3) Thanks, silly me...
Still surprised not many people are bothered by this.
Anyway thanks for your perseverance and quick responses - one of the best supports I've seen in long time.
Zbynek