Jason Lowenthal
Jason Lowenthal

Reputation: 810

How can I stop PhpStorm optimizing imports when reformatting code?

OK, I've scoured the internet trying to figure out which bit I have to change in PhpStorm to make it stop doing this.

Right now when I run "reformat code" on some PHP, it also decides to optimize imports for me. While normally I'd say "gee, that's great" - in some cases PhpStorm doesn't recognize that some of the imports it thinks are unused are, in fact, actually used.

Comments as code, and all that.

Please tell me someone out there knows how to make PhpStorm stop doing this. It's driving me mad that I have to undo changes to my imports whenever I auto-format my code.

Upvotes: 2

Views: 612

Answers (3)

Deez
Deez

Reputation: 43

I don't have the reputation to comment on an answer :sob: but I wanted to confirm and expand on an answer above. Also, I'm on v 2023.2.3

As it turns out, @LazyOne hit the nail on the head in the comments up above. Once I opened the reformat file dialog, all of my problems suddenly went away.

I searched in settings where to include "optimize imports" when reformatting code and I couldn't find it. But when I did as the post above said - go to Code > Reformat File... and select Optimize Imports, that's now applied every time I Reformat Code (⌥⌘L on mac).

Very confusing that this isn't in the main settings!

Note: I know this post was to stop optimizing imports, but I had the reverse problem :) Either way it helps!

Upvotes: 0

ans1genie
ans1genie

Reputation: 459

Mac users can use keyboard shortcut: ⌥⇧⌘L

This available to all Jetbrains products.

Upvotes: 0

Jason Lowenthal
Jason Lowenthal

Reputation: 810

As it turns out, @LazyOne hit the nail on the head in the comments up above. Once I opened the reformat file dialog, all of my problems suddenly went away.

Very obscurely hidden setting, since it's not something easy to find in general preferences :-)

Reformat Dialog

Upvotes: 3

Related Questions