103

Is there a simpler way of reordering methods within a class source file in IntelliJ than cutting and pasting the code manually? Nowadays I often need this while refactoring legacy code, e.g. to move related methods close to each other in the source code.

In Eclipse AFAIK there is a view similar to the Structure view of IntelliJ, where I can drag and drop methods around. However, this does not work in IntelliJ and I couldn't find any hints from its help either.

I am using IntelliJ 9.0.2 to be specific.

Péter Török
  • 114,404
  • 31
  • 268
  • 329
  • I think the best solution would be a formatting tool that orders your methods automatically. Prose: The methods should be orderes in the way they get called. Just like you would read an article in a newspaper. – Chris311 Nov 28 '16 at 09:52

6 Answers6

128

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

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

Beyond this the Rearranger Plugin lets you move methods around quickly, and even define a standard ordering based on your coding convention.

Intrications
  • 16,782
  • 9
  • 50
  • 50
retronym
  • 54,768
  • 12
  • 155
  • 168
  • isnt there something to just reorder alphabetically for example? – Jaime Hablutzel Jun 18 '11 at 21:30
  • The link is dead, this seems to be the correct one: http://java.net/projects/rearranger – stian Dec 06 '12 at 11:58
  • @stian - I've edited the post to point to the plugin page on JetBrains' website, since the link you posted does not seem to have any downloads available – adrian Oct 29 '13 at 17:47
  • 5
    For Mac OS X: Cmd+Shift+Up / Cmd+Shift+Down – Christian García Aug 06 '14 at 15:40
  • The [rearranger plugin does not work well with IntelliJ 2016 versions](https://github.com/DaveKriewall/Rearranger/issues/1) and the bug mentioned in saurabheights' answer is not yet fixed. So it seems there is no way at the moment to rearrange methods automatically in depth-first order. – Suzana Nov 28 '16 at 14:52
  • 4
    Since IDEA 16 it's available through the menu: `Code > Move Statement Up/Down` – Ilya Serbis Feb 02 '17 at 16:10
43

IntelliJ has a built in system that allows you to specify how to order your methods. You need to go to Settings (Ctrl + Alt +S) -> Editor -> Code Style -> Java -> Arrangement (tab) and scroll down until you find the icons with methods. There you can manipulate the options to sort them by visibility, or alphabetically, or to keep related ones grouped together. Here is a screenshot of my settings which will order methods automatically by visibility (public, protected, private) and alphabetically (a-z). The blue highlights show the currently selected rules.

enter image description here

georger
  • 1,568
  • 21
  • 24
30

Not a perfect answer yet, due to a bug in IntelliJ.

Though IntelliJ offers this feature implicitly, but it needs to be enabled as well as fixed. The OP's suggested way is technically arranging methods in depth-first order. However, if you use Breadth-first ordering(which works properly), it should reduce the manual work of moving functions by a lot, by arranging all caller and callee methods together.

Issue Link: https://youtrack.jetbrains.com/issue/IDEA-149524. Please do vote for its resolution.

IntellijRearrangeMethodsInDFO

The appropriate action for this is Rearrange Code. This has no key assigned to it, but you can define your own using Preferences->Keymap.

RearrangeActionMenuButton

enter image description here

saurabheights
  • 3,967
  • 2
  • 31
  • 50
10

With your cursor on the method definition line (you do not have to and press ctrl+shift+up or ctrl+shift+down, to move up or down respectively.

You can also to ctrl+shift+numberpad - to quickly collapse everything so you can focus on moving around (plain - works on my laptop as well, not sure why) and ctrl+shift+numberpad + to get back to see everything (ctrl-shift-equals works on my laptop as well).

bnieland
  • 6,047
  • 4
  • 40
  • 66
Yishai
  • 90,445
  • 31
  • 189
  • 263
7

Select a block of text (hit Ctrl-W a few times) and then use Ctrl-Shift-Up and Ctrl-Shift-Down to move it around.

Jacob Mattison
  • 50,258
  • 9
  • 107
  • 126
2

There is an automated way, which you can later tweak

Code -> show reformat file dialog

and tick "rearrange code" box

Alon
  • 601
  • 9
  • 19