简体   繁体   English

在IntelliJ中添加最喜欢的静态导入方法?

[英]Add favorite methods to static import in IntelliJ?

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;的静态导入org.junit.Assert.assertTrue; .

IntelliJ编辑器

Is it possible to do the same thing in IntelliJ? 在IntelliJ中可以做同样的事情吗?

The method detailed in this question will add a * import for the type ( static import org.junit.Assert.* ), but I do not want to add star-imports. 此问题中详述的方法将为类型添加* import( static import org.junit.Assert.* ),但我不想添加star-imports。 I'd prefer to import just the methods I am using. 我更喜欢只导入我正在使用的方法。

Strictly speaking, this does not answer your original question. 严格地说,这不能回答你原来的问题。

However, IDEA 10 supports Ctrl-Alt-Space completion for static methods. 但是,IDEA 10支持静态方法的Ctrl-Alt-Space完成。 For example, in a JUnit 4 test that does not import anything, type "assEq" and invoke Ctrl-Alt-space. 例如,在不导入任何内容的JUnit 4测试中,键入“assEq”并调用Ctrl-Alt-space。 The resulting dialog will also allow you to import this method statically. 生成的对话框还允许您静态导入此方法。

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. 我发现这个功能与“自动导入黑名单”相结合,可以很好地运行。

在此输入图像描述

I think you can:- 我想你可以: -

File -> Settings -> Code Style -> Java -> Imports . 文件 - > 设置 - > 代码样式 - > Java - > 导入 Then, add your import statements under Packages to Use Import with '*' table. 然后,在“ 要使用导入的包”下的“*”表下添加导入语句。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM