在 IntelliJ 中重新排序 Java 类方法的简单方法?
在 IntelliJ 中,是否有比手动剪切和粘贴代码更简单的方法来重新排序类源文件中的方法?如今,我在重构遗留代码时经常需要这个,例如将源代码中的相关方法移至彼此靠近的位置。
在 Eclipse AFAIK 中,有一个类似于 IntelliJ 的结构视图的视图,我可以在其中拖放方法。然而,这在 IntelliJ 中不起作用,我也无法从它的帮助中找到任何提示。
具体来说,我正在使用 IntelliJ 9.0.2。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以选择一个方法名称并点击:Ctrl+Shift+Up 或 Ctrl+Shift< /kbd>+Down 将其上下移动。
在 OS X 上:Cmd+Shift+向上 或 Cmd+Shift+< kbd>向下
除此之外,Rearranger 插件 可以让您快速移动方法,甚至根据您的编码约定定义标准顺序。
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.
IntelliJ 有一个内置系统,允许您指定如何订购方法。您需要转到设置(Ctrl + Alt + S)->编辑->代码风格-> Java->排列(选项卡)并向下滚动,直到找到带有
methods
的图标。在那里,您可以操纵选项以按可见性或字母顺序对它们进行排序,或将相关选项分组在一起。这是我的设置的屏幕截图,它将按可见性(公共、受保护、私有)和字母顺序(az)自动对方法进行排序。
蓝色突出显示当前选择的规则。
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.
由于 IntelliJ 中的错误,尚还不是一个完美的答案。
尽管 IntelliJ 隐式提供了此功能,但需要启用并修复它。 OP 建议的方法是在技术上按深度优先顺序排列方法。但是,如果您使用广度优先排序(可以正常工作),那么通过将所有调用者和被调用者方法排列在一起,应该会大大减少移动函数的手动工作。
问题链接:https://youtrack.jetbrains.com/issue/IDEA-149524。请对其决议投票。
对此的适当操作是重新排列代码。没有为其分配键,但您可以使用“首选项”->“键盘映射”定义自己的键。
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.
The appropriate action for this is Rearrange Code. This has no key assigned to it, but you can define your own using Preferences->Keymap.
将光标放在方法定义行上(不必按 ctrl+shift+up 或 ctrl+shift+down,分别向上或向下移动,
您也可以ctrl+shift+<。 kbd>numberpad - 快速折叠所有内容,以便您可以专注于移动(普通 - 在我的笔记本电脑上也可以使用,不知道为什么)和 ctrl+ shift+数字键盘+ 返回查看所有内容(ctrl-shift-equals 也适用于我的笔记本电脑)。
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).
选择一段文本(按 Ctrl-W 几次),然后使用 Ctrl-Shift-Up 和 Ctrl-Shift-Down 来移动它。
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.
有一种自动化的方式,您可以稍后调整
代码 ->显示重新格式化文件对话框
并勾选“重新排列代码”框
There is an automated way, which you can later tweak
Code -> show reformat file dialog
and tick "rearrange code" box