IntelliJ Organize Imports

匿名 (未验证) 提交于 2019-12-03 09:02:45

问题:

Does IntelliJ have an Organize Imports feature similar to that in Eclipse? What I have is a Java file with multiple classes missing their imports. Example:

package com.test; public class Foo {     public Map map;     public JTable table; } 

In Eclipse I could use Organize Imports and it would automatically import both java.util.Map and javax.swing.JTable. In IntelliJ I have to individually go to each class, select it, then press Alt-Enter. There is an Optimize Imports feature but all it seems to do is sort and expand the existing imports.

I'm using IntelliJ 10.5.

回答1:

Ctrl + Alt + O (Code → Optimize Imports...) is what you're looking for, both on Windows/Linux and macOS keymaps.

It says "Optimize", but, if configured to do so, it will also:

  • organize existing imports
  • remove unneeded imports
  • add new required imports
  • add unambiguous imports on the fly

You can tune the auto-import settings under "Settings → Editor → General → Auto Import" as described by Dave.



回答2:

Under "Settings -> Editor -> General -> Auto Import" there are several options regarding automatic imports. Only unambiguous imports may be added automatically; this is one of the options.



回答3:

Simple & short solution worked for me.

Go to File -> Settings -> Editor -> Auto Import -> Java (left panel) and make the below things: Select check box for "Add unambigious imports on the fly" and "Optimize imports on the fly"

Refer this.



回答4:

In addition to Optimize Imports and Auto Import, which were pointed out by @dave-newton and @ryan-stewart in earlier answers, go to:

  • IDEA File menu > Settings > Code Style > Java > Imports
  • IDEA >= 14: File menu > Settings > Editor > Code Style > Java > Imports (thanks to @mathias-bader for the hint!)

There you can fine tune the grouping and order or imports, "Class count to use import with '*'", etc.

Note:
since IDEA 13 you can configure the project default settings from the IDEA "start page": Configure > Project defaults > Settings > .... Then every new project will have those default settings:



回答5:

July 2015 - I have concluded that IntelliJ does not support automatically resolving imports with a single function. "Organize imports" simply removes unused imports, it does not resolve unimported types. Control-Space resolves a single unimported type. There does not exist a single action to resolve all types' imports.



回答6:

In IntelliJ 14, the path to the settings for Auto Import has changed. The path is

IntelliJ IDEA->Preferences->Editor->General->Auto Import

then follow the instructions above, clicking Add unambiguous imports on the fly

I can't imagine why this wouldn't be set by default.



回答7:

navigate to the file where you want to organize imports or just click on the whole package or even click on the project than press CTRL + ALT + O



回答8:

Just move your mouse over the missing view and hit keys on windows ALT + ENTER



回答9:

ALT+ENTER was far from eclipse habit ,in IDEA for me mouse over did not work , so in setting>IDESetting>Keymap>Show intention actions and quick-fixes I changed it to mouse left click , It did not support mouse over! but mouse left click was OK and closest to my intention.



回答10:

Shortcut for the Mac: (ctrl + opt + o)



回答11:

I finally created a workaround around this frustrating issue. I'm not completely happy with the workaround, but it's better than nothing.

Basically, after you paste the source code and unambigous imports are fixed, just press F2 to highlight the next compiler error. If the current error is an import-missing error, press Alt+Enter, then Enter to select the Import option, then pick the correct import. Then, press F2 again.



回答12:

If you are missing just one import (the class name has red underline), click and hover the mouse over it, and a blue suggested import statement will appear. If you hit, Alt + Enter at this point, the import will be included in the file and the red underline should disappear.



回答13:

That plugin will automatically do the "organize import" action on file save: https://github.com/dubreuia/intellij-plugin-save-actions.

To install: "File > Settings > Plugins > Browse repositories... > Search 'Save Actions' > Category 'Code tools'". Then activate the "organize import" save action.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!