java 将最喜欢的方法添加到 IntelliJ 中的静态导入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5107023/
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
Add favorite methods to static import in IntelliJ?
提问by matt b
In Eclipse it is possible to configure certain "favorite" classes which will be looked up when code completetion is invoked to see if a static import can be added for a method (this is under Preferences > Java > Editor > Content Assist > Favorites).
在 Eclipse 中,可以配置某些“最喜欢的”类,这些类将在调用代码完成时查找,以查看是否可以为方法添加静态导入(这是在Preferences > Java > Editor > Content Assist > Favorites 下)。
For example, I can begin to type assertT
, and Eclipse will ask if I want to add a static import of org.junit.Assert.assertTrue;
.
例如,我可以开始键入assertT
,Eclipse 会询问我是否要添加org.junit.Assert.assertTrue;
.
Is it possible to do the same thing in IntelliJ?
是否可以在 IntelliJ 中做同样的事情?
The method detailed in this questionwill add a *
import for the type (static import org.junit.Assert.*
), but I do not want to add star-imports. I'd prefer to import just the methods I am using.
此问题中详述的方法将为*
类型 ( static import org.junit.Assert.*
)添加导入,但我不想添加星形导入。我宁愿只导入我正在使用的方法。
采纳答案by limc
I think you can:-
我认为你可以:-
File-> Settings-> Code Style-> Java-> Imports. Then, add your import statements under Packages to Use Import with '*'table.
文件->设置->代码样式-> Java->导入。然后,在Packages to Use Import with '*'表下添加您的导入语句。
回答by Pakka Pakka
Strictly speaking, this does not answer your original question.
严格来说,这并不能回答您最初的问题。
However, IDEA 10 supports Ctrl-Alt-Space completion for static methods. For example, in a JUnit 4 test that does not import anything, type "assEq" and invoke Ctrl-Alt-space. The resulting dialog will also allow you to import this method statically.
但是,IDEA 10 支持 Ctrl-Alt-Space 完成静态方法。例如,在不导入任何内容的 JUnit 4 测试中,键入“assEq”并调用 Ctrl-Alt-space。生成的对话框还允许您静态导入此方法。
Once you're imported a single method (like assertEquals), other static methods from the same class will be included in the regular code completion suggestions (although you can still use the Ctrl-Alt-Space shortcut, of course).
导入单个方法(如 assertEquals)后,来自同一类的其他静态方法将包含在常规代码完成建议中(当然,您仍然可以使用 Ctrl-Alt-Space 快捷方式)。
I find this feature, combined with the "automatic import blacklist", to work very well.
我发现此功能与“自动导入黑名单”相结合,效果很好。