Java IntelliJ 静态导入完成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4387134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IntelliJ Static Import Completion
提问by Scruffers
In Eclipse I was able to register a set of classes for static import auto-completion, e.g. Math., Assert., EasyMock.*
在 Eclipse 中,我能够注册一组用于静态导入自动完成的类,例如 Math。,断言。, EasyMock.*
With this enabled I was able to hit ctrl-space assertEquals, pow, createMock etc. would appear as valid code completions.
启用此功能后,我可以按 ctrl-space assertEquals、pow、createMock 等将显示为有效的代码完成。
I'm struggling to find this in IntelliJ but am sure it must exist. Can anyone help?
我正在努力在 IntelliJ 中找到它,但我确信它一定存在。任何人都可以帮忙吗?
Thanks!
谢谢!
采纳答案by Andrzej Doyle
Version >= 11:
版本 >= 11:
Settings -> Code Style -> Java -> Imports (tab)
.
Settings -> Code Style -> Java -> Imports (tab)
.
Version < 11
版本 < 11
Settings -> Code Style -> Imports
Settings -> Code Style -> Imports
In the middle of the pane is the "Packages to Use Import with '*'" table. You can add an entry here of a fully-qualified class name, and tick the static box; now all static methods in this class will be available for auto-completion.
在窗格的中间是“使用'*'导入的包”表。您可以在此处添加一个完全限定类名的条目,并勾选静态框;现在该类中的所有静态方法都可用于自动完成。
(I'm not sure how the static import works with specifying a package, as I've never tried it, but I don't see why it wouldn't. Specifying a super-package and ticking the "with subpackages" option could be even more interesting.)
(我不确定静态导入如何与指定包一起工作,因为我从未尝试过,但我不明白为什么它不会。指定一个超级包并勾选“带有子包”选项可以更有趣。)
回答by Andreas Wederbrand
Just do the reference once, then put the caret on the class name (ie Math), press alt+enter and choose "add on demand static import for 'java.lang.Math'. Intellij will add
只需做一次引用,然后将插入符号放在类名(即 Math)上,按 alt+enter 并选择“为 'java.lang.Math' 添加按需静态导入。Intellij 将添加
import static java.lang.Math.*;
to the top of the file.
到文件的顶部。
回答by Pakka Pakka
If you're using IntelliJ 10, try the following:
如果您使用的是 IntelliJ 10,请尝试以下操作:
assEq<Ctrl-Alt-Space>
Accepting a suggestion from the resulting popup list will, by default, insert a ClassName.methodName() reference (which you can convert to a static import using an Alt-Enter intention).
默认情况下,接受来自结果弹出列表的建议将插入 ClassName.methodName() 引用(您可以使用 Alt-Enter 意图将其转换为静态导入)。
You can also insert a statically imported method from the completion list by choosing "Right" in the completion menu, and selecting "Import Statically":
您还可以通过在完成菜单中选择“右”并选择“静态导入”从完成列表中插入静态导入的方法:
Note that once you've statically imported a single method from a class (Assert.assertSame), other static methods from that class (like Assert.assertEquals) will be included in the "regular" code completion (Ctrl-Space).
请注意,一旦您从类 (Assert.assertSame) 静态导入了单个方法,该类中的其他静态方法(如 Assert.assertEquals)将包含在“常规”代码完成(Ctrl-Space)中。
回答by loyalBrown
For Intellij 12 just hit <ctrl + spacebar(twice)>
. Then to import the method statically hit <alt + return>
. Otherwise just hitting enter will insert the fully qualified name of the method.
对于 Intellij 12,只需点击<ctrl + spacebar(twice)>
. 然后静态地导入方法命中<alt + return>
。否则只需按 Enter 键将插入方法的完全限定名称。
Update:Sometimes just using <alt + return>
works too.
更新:有时也只是使用<alt + return>
作品。
回答by John Dewell
In OS X you need to do a option + return.
在 OS X 中,您需要执行选项 + 返回。
回答by nucatus
Alt + Enter
will do the trick. You have to have the whole expression at first, and the hit Alt + Enter
on the method you want to statically import.
Alt + Enter
会做的伎俩。您首先必须拥有整个表达式,以及Alt + Enter
您想要静态导入的方法。