Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intellij: move variables at method start

I'm using IntelliJ Ultimate for working with Java.

There are so many useful functions, but there is one that could be very good for me and that I can't find...

Sometimes I use variables without previously declaring them (at first time). Then, I use the ctrl+Enter functionality on the undeclared variables to open menu and select the option for automatically declaring. Sometimes it's just more rapid.

It works good, the problem is that the variable is automatically declared next the line where is used. Now, I want a function in IntelliJ that automatically moves all the declarations at the start of the methods where they are. Does it exists? Or how can I implement it?

like image 976
Lore Avatar asked May 07 '18 14:05

Lore


People also ask

How do I move a method in IntelliJ?

Open your class in the editor, place a caret at the static method you want to move and press F6 . In the Move Members dialog specify the following options: In the Members to be moved to another class (static only) field, select the checkboxes next to the methods that you want to move to another class.

How do I change variables in IntelliJ?

Press Shift+F6 or from the main menu, select Refactor | Rename. next to the highlighted element. You can press Tab to open the context menu and select the additional rename options. If you want to see the Rename dialog with more options, click the More options link or press Shift+F6 .

How do I navigate to methods in IntelliJ?

Browse through methods Press Alt+Down or Alt+Up . To visually separate methods in code, in the Settings/Preferences dialog ( Ctrl+Alt+S ), go to Editor | General | Appearance and select the Show method separators option. To open the Structure tool window, press Alt+7 .

How do I refactor a method in IntelliJ?

Select a code fragment you want to extract to a method. Press Ctrl+Alt+M or from the main menu, select Refactor | Extract | Method. In the dialog that opens, configure a method options, such as visibility, parameters, and so on. You can also change a name of the method if you need.


1 Answers

There is no feature to move all declarations to the method start (and no plans to add it, because most coding guidelines recommend declaring variables as close to the usage as possible). For situations where you're trying to access a variable which is not visible because it's declared in a too narrow scope, there's a quickfix "Bring variable into scope" which will make this specific variable accessible for this specific usage.

You can of course write a plugin to move all variables to the top; plugin development documentation can be found here.

like image 79
yole Avatar answered Nov 11 '22 19:11

yole