Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 335
Anton Arhipov Avatar asked Nov 02 '22 09:11

Anton Arhipov


1 Answers

Why don't you make a Live Template for yourself?

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<$TYPE$> $VAR$ = new $END$

Where it says No applicable contexts, define: Java -> Statement

Now click Edit variables and bind as follows

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

Et voila. The keypresses are now: list tab St enter enter (varname) enter

result, with cursor:

List<String> myvar = new |

And from here you can hit ctrl-shift-space to autocomplete like so:

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

Hope this helps :)

like image 74
vikingsteve Avatar answered Nov 08 '22 08:11

vikingsteve