简体   繁体   English

IntelliJ IDEA自动完成带有泛型的集合类型声明

[英]IntelliJ IDEA auto-completion for collection type declaration with generics

I'm trying to figure out how it would be more efficient to type a declaration of a collection type with a generic. 我试图弄清楚如何用泛型来输入集合类型的声明会更有效。

Typing: 打字:

List<

Automatically expands to: 自动扩展为:

List<|>

(cursor position is marked with "|") (光标位置标有“|”)

Typing 打字

List<St|>

using the autocomplete it gets to 使用它获得的自动完成

List<String|>

notice the cursor position 注意光标位置

Now the question: how could it be possible to get to the following state without using the arrow keys: 现在的问题是:如何在不使用箭头键的情况下进入以下状态:

List<String> |

Ideally, id like to use statement autocompletion for this, but it rather jumps to the new line which is not desired in this situation. 理想情况下,id喜欢使用语句自动完成功能,但它会跳转到新行,这在这种情况下是不可取的。

Why don't you make a Live Template for yourself? 你为什么不为自己制作一个Live Template

For example, go to Live Templates , add a new template under other , give it an abbreviation list , a description as you like, and enter the template text: 例如,转到“ 实时模板” ,在其他模板下添加新模板,为其指定缩写list ,根据需要添加说明,然后输入模板文本:

List<$TYPE$> $VAR$ = new $END$

Where it says No applicable contexts , define: Java -> Statement 如果没有适用的上下文 ,请定义: Java - > Statement

Now click Edit variables and bind as follows 现在单击Edit variables并按如下方式绑定

TYPE -> expectedType()
VAR -> suggestVariableName()

Et voila. 瞧瞧。 The keypresses are now: list tab St enter enter (varname) enter 现在按键是:列表选项卡 St 输入 enter (varname) enter

result, with cursor: 结果,用光标:

List<String> myvar = new |

And from here you can hit ctrl-shift-space to autocomplete like so: 从这里你可以点击ctrl-shift-space自动完成,如下所示:

List<String> myvar = new ArrayList<>(|);

Hope this helps :) 希望这可以帮助 :)

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

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