Generate local variable to receive the return value of a method eclipse
Asked Answered
V

9

14

for example I have this code

categoryCT.getInsertedItems();

and I want shortcut to generate code like this

List<Category> insertedItems=   categoryCT.getInsertedItems();
Vertigo answered 23/7, 2013 at 10:51 Comment(7)
You can use Ctrl+1 it will give hints under this you can choose assign statement to new local VariableAbduce
I see add finally blockVertigo
try This shortcut it will automatically assign (ctrl+2,l)Abduce
this shortcut didn't do anything!Vertigo
shortcuts like "ctrl+2, l" are a bit tricky since it depends a bit on timing here. One approach that works pretty good for me: ctrl down, 2 down (ctrl+2 at that point), release ctrl, L down (2+L at this point), release 2 and L. The cursor will also need to be inside categoryCT.getInsertedItems() since that's what you want to assign.Wooley
I saw quick assest menu and it also didn't workVertigo
Ctrl+1 works even in Netbeans if shortcuts are selected in Eclipse style.Nickell
B
25

Eclipse can't help you with the variable name but you can write:

insertedItems = categoryCT.getInsertedItems();

This will give you a compile error.

If you press Ctrl+1 anywhere in this line, Eclipse will offer "Create local variable 'insertedItems'"

Fewest keystrokes to get the desired result:

  • catCTCtrl+Space -> categoryCT
  • .getIICtrl+Space -> categoryCT.getInsertedItems()
  • ;
  • Shift+Alt+Left to select the whole method invocation
  • Ctrl+1 + select "Create new local variable"
Banausic answered 23/7, 2013 at 11:48 Comment(1)
Shift+Alt+Left to select the whole method invocation Ctrl+1 + select "Create new local variable" these two steps do the jobVertigo
H
7

I'd add for windows users:

Press ctrl+2 for the available options.

For ex. ctrl+2+L will assign your statement to a local variable.

Hollie answered 2/5, 2018 at 3:53 Comment(0)
O
4

The shortcut that works on my Mac is 2 + l (lowercase L key)

Steps:

  • Write the variable, Ex: categoryCT.getInsertedItems();;
  • Select the entire variable definition;
  • Press 2, leave the press and then press l(lowercase L key);

This will create the line List<Category> insertedItems= categoryCT.getInsertedItems();.

Olia answered 2/4, 2016 at 0:20 Comment(0)
B
3

Ctrl+2, L will do what you are looking for.

Boccioni answered 22/11, 2019 at 6:45 Comment(0)
P
2

I don't think I saw Alt+Shift+L, but that works and if your right-hand assignment is the only thing on the line, you don't even have to highlight it

Papaverine answered 20/5, 2021 at 14:9 Comment(0)
C
0

Just uses eclipse's quick fix: Cmd+1. after type categoryCT.getInsertedItems(), then click Cmd+1 when the cursor is at the end.

Courtund answered 14/7, 2018 at 0:4 Comment(0)
B
0

Alt + Shift + V: in NetBeans Extracts an existing expression or statement into a new variable. For example, the statement appears multiple times so it should be introduced to a variable:

textEmail.getText()

String text = textEmail.getText()

Select statement then the Introduce Variable dialog appears. Introduce Variable dialog Enter the variable name and click OK.

Note that the scope of the newly introduced variable is local.

Burdened answered 11/12, 2019 at 9:40 Comment(0)
D
0

In IntelliJ IDEA you can use ctrl + alt + l.

Dylane answered 18/9, 2022 at 6:5 Comment(0)
L
0

for me option+command+L worked on mac with apple chip.

Alternatively, you can select your statement that you want to extract a local variable and right click then from menu options go to refactors and you should be able to find the Extract Local Variable as option.

you can refer the following image. enter image description here

Lorrielorrimer answered 11/12, 2023 at 18:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.