Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get Intellij IDEA to auto-complete the type of an initialized variable?

I write:

x = getFoo();

How can I get IDEA to turn this into:

Foo x = getFoo();

? IDEA certainly knows what the return type of getFoo is, but it refuses to auto-complete it.

The documentation says that Ctrl+Shift+Space should work, but it doesn't.

like image 960
jameshfisher Avatar asked Aug 26 '14 17:08

jameshfisher


People also ask

How do I complete code in IntelliJ IDEA?

By default, IntelliJ IDEA displays the code completion popup automatically as you type. If automatic completion is disabled, press Ctrl+Shift+Space or choose Code | Code Completion | Type-Matching from the main menu. If necessary, press Ctrl+Shift+Space once again. This lets you complete:

How do I create a variable in IntelliJ IDEA?

We can press Alt+Enter at the end of this and have IntelliJ IDEA create a valid variable declaration for us. We even get useful defaults suggested for variable names. Even as we’re typing our variable declaration, IntelliJ IDEA will suggest types and names for us.

How does IntelliJ IDEA change the final constant of a class?

IntelliJ IDEA creates a new public static final constant at the start of our class: IntelliJ IDEA also replaces the original line of code with the new constant: System.out.println ("Number of times the planet rotates around the sun is " + NUMBER_OF_DAYS_IN_A_YEAR);

When does IntelliJ IDEA suggest a list of possible names?

If basic code completion is applied to a part of a field, parameter, or variable declaration, IntelliJ IDEA suggests a list of possible names depending on the item type.


1 Answers

Use the postfix code completion feature instead. What you would write is getFoo().var, and this will auto-expand it to the type and let you modify the variable name.

like image 127
Makoto Avatar answered Nov 15 '22 08:11

Makoto