35

As a long time Eclipse user, I'm playing around a little bit with IntelliJ IDEA 10. I can't seem to find out how to perform an "Assign statement to new local variable" code completion.

Feature explanation:

I type something like

new BufferedOutputStream(out)

and then hit Cmd (or Ctrl)+1 and enter, and Eclipse changes the line into:

BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(out);

At the same time, I can type over "bufferedOutputStream" immediately to rename it (or select from the options "bufferedOutputStream", "outputStream" and "stream" from a dropdown).

I use this feature for absolutely every assignment, and it's an enormous time saver - this must be available in IDEA, too - but where is it hidden?

Chris Lercher
  • 37,264
  • 20
  • 99
  • 131

4 Answers4

58

Refactor | Introduce Variable (Ctrl+Alt+V on Windows). Note that you don't need to select the text if it's the only text in the current line. Then you can change the variable name in-line just like you've described and press Enter to complete editing.

Another way is to use the Postfix Completion:

Type .var (or just .v to select it from the list) and confirm it with Enter.

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
  • Finally something that works in Android Studio similar to Eclipse. – Bevor Sep 05 '16 at 18:07
  • Might be obvious, but for Apple users—`Cmd` + `Option` + `v` does the trick. Also, the resolve all is nice—it assigns the variable in the first expression and then replaces the other expressions in that scope with the new variable. – MrColes Dec 01 '16 at 17:13
  • 1
    Another possibility is to use the Postfix Completion feature of IntelliJ. Typing ".v+ENTER" would normally choose the ".var" postfix completion and introduce the variable. – Lari Hotari Aug 21 '17 at 16:20
  • @LariHotari's comment should be part of the answer, it is much faster this way : https://blog.jetbrains.com/idea/2014/03/postfix-completion/ – FredBoutin May 31 '18 at 13:16
11

As CrazyCoder mentions you can use Ctrl+Alt+V. Also instead of selecting the expression, clicking into somewhere in your expression and using Ctrl+W to expand scope is very useful while using introduce refactorings. Extract refactorings are:

  • Extract variable: Ctrl+Alt+V
  • Extract field: Ctrl+Alt+F
  • Extract method: Ctrl+Alt+M
  • Extract parameter: Ctrl+Alt+P
  • Extract constant: Ctrl+Alt+C

Also, Idea is a polygot editor so you can use these extract refactorings for other file types like js or html also (not all refactorings work in all file types but Ctrl+W works mostly).

There are more extract refactorings which do not have shortcuts which you can access from Refactor|Extract menu (both menu bar and context menu). To quick access all refactorings you can use Ctrl+Alt+Shift+T for a popup menu.

As a last word, I highly encourage you using "Tip of the Day" (Help|Tip of the Day). It is a fast way to learn many helpful features of Idea.

dmSherazi
  • 3,743
  • 5
  • 37
  • 62
Gökçer Gökdal
  • 950
  • 1
  • 11
  • 18
4

It's not as nice as Eclipse, but you can try the following:

new BufferedOutputStream(out)
  • Select the expression above, either with your mouse (or by using Ctrl+W).
  • Then hit Ctrl+Alt+V to Introduce a Variable or (Ctrl+Alt+F to Introduce a Field)
Hulk1991
  • 3,079
  • 13
  • 31
  • 46
dogbane
  • 266,786
  • 75
  • 396
  • 414
0

Easiest is, hit Alt+Enter, you will be offered with a list of options, and just select "Introduce local Variable".

Zeljko Kozina
  • 129
  • 1
  • 3