BullyWiiPlaza
BullyWiiPlaza

Reputation: 19185

Apply Intellij Code Improvement Suggestions

I'm using Intellij for Java development. In the code editor on the right, it displays some yellow markers which indicate issue with my code, e.g. improvement suggestions. When I hover over the affected code line it tell me what to do. Unfortunately I didn't find a way of automatically applying said suggestion. How do I do that? I don't want to do fixes by hand but rather allow Intellij to do them for convenience.

CTRL + Space is only code completion.

Upvotes: 2

Views: 1792

Answers (2)

Bohuslav Burghardt
Bohuslav Burghardt

Reputation: 34776

Just place the cursor over the highlighted code, hit Alt+Enter, select the suggested fix and hit Enter.

enter image description here

You can also apply the fix for all problems of the given type within specified scope (current file, project, ...). If you want to do that, just select Run inspection on from the menu that is shown.

I'm not sure if there is a way to apply these fixes automatically completely without user intervention, but even if there was, it probably wouldn't be very user friendly, as the warnings are sometimes false-positive.

Upvotes: 3

user2630808
user2630808

Reputation:

If you want to apply a code suggestion it it simply Alt+Enter while the cursor is over the highlighted code. I would strongly suggest not automatically applying suggestions and to use your better judgement.

I can personally attest that while it's a little extra work, being able to choose the suggestions validity is much better. There's been more than a few times when the suggestion wouldn't make sense in the context.

As a sidenote I'd suggest having a reference for the KB shortcuts open so you can improve your speed within the IDE: Mac or Windows/Linux

Upvotes: 1

Related Questions