在 IntelliJ 中重新排序 Java 类方法的简单方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2827308/
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
Simple way to reorder methods of a Java class in IntelliJ?
提问by Péter T?r?k
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.
有没有比手动剪切和粘贴代码更简单的在 IntelliJ 中的类源文件中重新排序方法的方法?现在我在重构遗留代码时经常需要这个,例如在源代码中将相关方法彼此靠近。
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.
在 Eclipse AFAIK 中有一个类似于 IntelliJ 的结构视图的视图,我可以在其中拖放方法。但是,这在 IntelliJ 中不起作用,我也无法从它的帮助中找到任何提示。
I am using IntelliJ 9.0.2 to be specific.
具体来说,我使用的是 IntelliJ 9.0.2。
采纳答案by retronym
You can select a method name and hit: Ctrl+Shift+Upor Ctrl+Shift+Downto move it up and down.
您可以选择的方法名称和命中:Ctrl+ Shift+Up或Ctrl+ Shift+Down将其上下移动。
On OS X: Cmd+Shift+Upor Cmd+Shift+Down
在OS X:Cmd+ Shift+Up或Cmd+ Shift+Down
Beyond this the Rearranger Pluginlets you move methods around quickly, and even define a standard ordering based on your coding convention.
除此之外,Rearranger Plugin允许您快速移动方法,甚至根据您的编码约定定义标准排序。
回答by Jacob Mattison
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.
选择一个文本块(按几次 Ctrl-W),然后使用 Ctrl-Shift-Up 和 Ctrl-Shift-Down 来移动它。
回答by Yishai
With your cursor on the method definition line (you do not have to and press ctrl+shift+upor ctrl+shift+down, to move up or down respectively.
将光标放在方法定义行上(您不必按ctrl+ shift+up或ctrl+ shift+down分别向上或向下移动。
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+ shift+numberpad -快速崩溃一切,让您可以专注于中移动(纯-的作品在我的笔记本电脑为好,不知道为什么)和ctrl+ shift+numberpad +要回看到的一切(按Ctrl移等于我的笔记本电脑作为作品好)。
回答by saurabheights
Not a perfect answer yet, due to a bug in IntelliJ.
没有一个完美的答案还没有,因为在的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.
尽管 IntelliJ 隐式提供了此功能,但它需要启用和修复。OP 建议的方法是在技术上按深度优先顺序排列方法。但是,如果您使用广度优先排序(它可以正常工作),通过将所有调用方和被调用方方法安排在一起,它应该可以减少大量移动函数的手动工作。
Issue Link: https://youtrack.jetbrains.com/issue/IDEA-149524. Please do vote for its resolution.
问题链接:https: //youtrack.jetbrains.com/issue/IDEA-149524。请为它的决议投票。
The appropriate action for this is Rearrange Code. This has no key assigned to it, but you can define your own using Preferences->Keymap.
对此的适当操作是重新排列代码。这没有分配给它的键,但您可以使用 Preferences->Keymap 定义自己的键。
回答by georger
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 有一个内置系统,允许您指定如何对方法进行排序。您需要转到设置 (Ctrl + Alt +S) -> 编辑器 -> 代码样式 -> Java -> 排列(选项卡)并向下滚动,直到找到带有methods
. 您可以在那里操作选项以按可见性或字母顺序对它们进行排序,或将相关的组合在一起。这是我的设置的屏幕截图,它将按可见性(公共、受保护、私有)和字母顺序 (az) 自动对方法进行排序。蓝色高亮显示当前选择的规则。
回答by Alon
There is an automated way, which you can later tweak
有一种自动化的方式,您可以稍后对其进行调整
Code -> show reformat file dialog
代码 -> 显示重新格式化文件对话框
and tick "rearrange code" box
并勾选“重新排列代码”框