简体   繁体   English

IntelliJ 静态导入完成

[英]IntelliJ Static Import Completion

In Eclipse I was able to register a set of classes for static import auto-completion, eg Math.在 Eclipse 中,我能够为静态导入自动完成注册一组类,例如 Math。 , Assert. ,断言。 , EasyMock.* , 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.我正在努力在 IntelliJ 中找到它,但我确信它一定存在。 Can anyone help?有人可以帮忙吗?

Thanks!谢谢!

Version >= 11:版本 >= 11:

Settings -> Code Style -> Java -> Imports (tab) . Settings -> Code Style -> Java -> Imports (tab)

Version < 11版本 < 11

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.) (我不确定静态导入如何与指定包一起工作,因为我从未尝试过,但我不明白为什么它不会。指定一个超级包并勾选“带有子包”选项可以更有趣。)

Alt + Enter will do the trick. Alt + Enter可以解决问题。 You have to have the whole expression at first, and the hit Alt + Enter on the method you want to statically import.您首先必须拥有整个表达式,然后在要静态导入的方法上Alt + Enter

静态导入 IntelliJ

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)中。

For Intellij 12 just hit <ctrl + spacebar(twice)> .对于 Intellij 12,只需点击<ctrl + spacebar(twice)> spacebar <ctrl + spacebar(twice)> Then to import the method statically hit <alt + return> .然后静态地导入方法点击<alt + return> Otherwise just hitting enter will insert the fully qualified name of the method.否则只需按 Enter 键将插入方法的完全限定名称。

Update: Sometimes just using <alt + return> works too.更新:有时只使用<alt + return>也可以。

在 OS X 中,您需要执行选项 + 返回。

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.到文件的顶部。

Now its possible to add live templates with static imports :现在可以使用静态导入添加实时模板

You have to check static import in Options您必须在选项中检查静态导入

@org.junit.Test
public void should$EXPR$when$CONDITION$() {
    org.junit.Assert.assertThat(null, org.hamcrest.CoreMatchers.is(org.hamcrest.CoreMatchers.nullValue())); 
}

在此处输入图片说明

In IntelliJ 2022.3 on Mac, go to Preferences > Code Style > Java > tab 'Imports' > section 'Packages to Use Import with "*" > click '+' > search for desired class > hit Enter and tick the box next to the new entry to indicate it is static.在 Mac 上的 IntelliJ 2022.3 中,转到“首选项”>“代码样式”>“Java”>“导入”选项卡>“使用带有“*”的导入的包”部分>单击“+”>搜索所需的类>按 Enter 并勾选旁边的框新条目表明它是静态的。

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

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