java 使用键盘快捷键重新排列 IntelliJ 中的方法参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35358469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:03:32  来源:igfitidea点击:

Rearrange method parameters in IntelliJ with keyboard shortcut

javaintellij-idea

提问by Stefan Haberl

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

在 IntelliJ 中,我可以通过按?+ Shift+ (或?+ 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 signaturething.

如果 IntelliJ 只是重新排序参数,那对我来说就足够了。也就是说,这不需要触发整个Refactor > Change 方法签名的事情。

回答by Peter Gromov

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

从 IDEA 16 EAP 开始,就有了一个行动。它在菜单中:

Code | Move Element Left/Right

代码 | 向左/向右移动元素

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

键盘快捷键Alt+ Ctrl+ Shift+ Left/RightAlt+ Cmd+ Shift+Left/Right的OSX)。

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

有关更多详细信息,请参阅http://blog.jetbrains.com/idea/2016/01/intellij-idea-16-eap-improves-editor-and-vcs-integration/

回答by Lyubomyr Shaydariv

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

我不确定在 IDEA 中是否可行,但您可以尝试将编辑器宏用于双参数方法:

  • Editor -> Macros -> Start Macro Recording
  • Record your macro:
    • Ctrl+F6to open the Change Signaturedialog
    • Alt+to swap the first and the second parameter
    • Tabto deactivate the Parameterstab and let the Refactorbutton get the focus
    • Enterto close the dialog at perform refactoring
  • Editor -> Macros -> Stop Macro Recordingand give a name to your macro, let's say "Swap method parameters 1 and 2".
  • 编辑器 -> 宏 -> 开始宏录制
  • 记录你的宏:
    • Ctrl+F6打开更改签名对话框
    • Alt+交换第一个和第二个参数
    • Tab停用“参数”选项卡并让“重构”按钮获得焦点
    • Enter在执行重构时关闭对话框
  • 编辑器 -> 宏 -> 停止宏录制并为您的宏命名,例如“交换方法参数 1 和 2”。

And then access your macro via the Editor -> Macrosmenu 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.

然后通过Editor -> Macros菜单访问您的宏,或在Settings -> Keymap -> Main menu/Edit/Macros/Swap method parameters 1 和 2 中为其分配自定义快捷方式。对我来说,它看起来是一种非常肮脏且与上下文无关的方式,但也许它可以帮助你一点。

回答by Timmeh

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

在 Ubuntu 18.04 上,默认快捷键是 Ctrl+Alt+Shift+Super+Left/Right 来向左或向右移动参数。

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

这是因为它被设置为不与一些新的操作系统级别的快捷方式发生冲突