Rearrange method parameters in IntelliJ with keyboard shortcut

JavaIntellij Idea

Java Problem Overview


In IntelliJ I can easily rearrange the order of statements (or whole methods for that part) by pressing + Shift + (or + Shift + ).

I was wondering whether there's a shortcut to change the order of method parameters as easily, so that

public void sth(String a, String b) {...}

will become

public void sth(String b, String a) {...}

with the stroke of a keyboard shortcut (assuming my cursor is positioned on one of the parameters).

It would be enough for me, if IntelliJ would just reorder the parameters. That is, this need not trigger a whole Refactor > Change method signature thing.

Java Solutions


Solution 1 - Java

Since IDEA 16 EAP, there's an action for that. It's in the menu:

Code | Move Element Left/Right

Keyboard shortcuts are Alt+Ctrl+Shift+Left/Right (Alt+Cmd+Shift+Left/Right for OSX).

See http://blog.jetbrains.com/idea/2016/01/intellij-idea-16-eap-improves-editor-and-vcs-integration/ for more details.

Solution 2 - Java

I'm not sure if it's possible in IDEA, but you might try to use editor macros for two-parameter methods:

  • Editor -> Macros -> Start Macro Recording
  • Record your macro:
  • Ctrl+F6 to open the Change Signature dialog
  • Alt+ to swap the first and the second parameter
  • Tab to deactivate the Parameters tab and let the Refactor button get the focus
  • Enter to close the dialog at perform refactoring
  • Editor -> Macros -> Stop Macro Recording and give a name to your macro, let's say "Swap method parameters 1 and 2".

And then access your macro via the Editor -> Macros menu or assign a custom shortcut to it in Settings -> Keymap -> Main menu/Edit/Macros/Swap method parameters 1 and 2. It looks a very dirty and context-free way to me, but maybe it could help you a little.

Solution 3 - Java

On Ubuntu 18.04 the default shortcut is Ctrl+Alt+Shift+Super+Left/Right to move an argument left or right in position.

This is as it is setup to not clash with some of the new OS level shortcuts

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStefan HaberlView Question on Stackoverflow
Solution 1 - JavaPeter GromovView Answer on Stackoverflow
Solution 2 - JavaLyubomyr ShaydarivView Answer on Stackoverflow
Solution 3 - JavaTimmehView Answer on Stackoverflow