Easy way to reorder Java class methods in IntelliJ?

Is there an easier way to reorder in the source class file in IntelliJ than to cut and paste the code manually? Currently, I often need this, refactoring legacy code, for example. to move related methods close together in the source code.

Eclipse AFAIK has a view similar to the IntelliJ framework view, where I can drag and drop methods around. However, this does not work in IntelliJ, and I could not find any help with it either.

I am using IntelliJ 9.0.2 to be specific.

+44
java methods intellij-idea refactoring
May 13 '10 at 1:54 pm
source share
5 answers

You can select a method name and press: Ctrl + Shift + Up or Ctrl + Shift + Down to move it up and down.

On OS X: Cmd + Shift + Up or Cmd + Shift + Down

In addition, the Backranger plugin allows you to quickly move methods and even define a standard order based on your coding convention.

+69
May 13 '10 at 2:00
source share
β€” -

Use the cursor on the line of the method definition (you do not need and press ctrl + shift + up or ctrl + shift + down to move up or down, respectively.

You can also ctrl + shift + numberpad - quickly collapse everything so that you can focus on movement (normal - it works on my laptop, but not sure why) and ctrl + shift + numberpad + to see everything (ctrl-shift-equals works on my laptop as well).

+7
May 13, '10 at 15:21
source share

Not an ideal answer yet , due to a bug in IntelliJ.

Although IntelliJ offers this feature implicitly, it must be enabled as well as fixed. The proposed OP method is the technical organization of methods in depth-first order. However, if you use the pre-order Breadth (which works correctly), it should significantly reduce the manual work of moving functions, organizing all the calling and called methods together.

Link to the problem: https://youtrack.jetbrains.com/issue/IDEA-149524 . Please vote for his permission.

IntellijRearrangeMethodsInDFO

The appropriate action for this is the reordering code. It does not have a key assigned to it, but you can define your own using Preferences-> Keymap.

Change play order

enter image description here

+5
Nov 27 '16 at 19:38
source share

IntelliJ has a built-in system that allows you to specify the order of your methods. You need to go to Settings (Ctrl + Alt + S) β†’ Editor β†’ Code Style β†’ Java β†’ Location (tab) and scroll down until you find the icons with methods . There you can manipulate the parameters to sort them visibly or alphabetically or to save related groupings together. Here is a screenshot of my settings that will automatically order methods using visibility (public, protected, private) and alphabetically (az). Blue highlights show the currently selected rules.

enter image description here

+3
Jan 30 '17 at 14:55
source share

Select a block of text (press Ctrl-W several times), and then use Ctrl-Shift-Up and Ctrl-Shift-Down to move it.

+2
May 13 '10 at 1:58 p.m.
source share



All Articles