简体   繁体   English

在IntelliJ中重新排序Java类方法的简单方法?

[英]Simple way to reorder methods of a Java class in IntelliJ?

Is there a simpler way of reordering methods within a class source file in IntelliJ than cutting and pasting the code manually? 有没有比在IntelliJ中的类源文件中重新排序方法更简单的方法,而不是手动剪切和粘贴代码? Nowadays I often need this while refactoring legacy code, eg 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. 在Eclipse AFAIK中,有一个类似于IntelliJ的Structure视图的视图,我可以在其中拖放方法。 However, this does not work in IntelliJ and I couldn't find any hints from its help either. 但是,这在IntelliJ中不起作用,我也无法从其帮助中找到任何提示。

I am using IntelliJ 9.0.2 to be specific. 我使用IntelliJ 9.0.2是具体的。

You can select a method name and hit: Ctrl + Shift + Up or Ctrl + Shift + Down to move it up and down. 您可以选择方法名称并按下: Ctrl + Shift + 向上Ctrl + Shift + 向下移动它。

On OS X: Cmd + Shift + Up or Cmd + Shift + Down 在OS X上: Cmd + Shift + UpCmd + Shift + Down

Beyond this the Rearranger Plugin lets you move methods around quickly, and even define a standard ordering based on your coding convention. 除此之外, Rearranger插件允许您快速移动方法,甚至可以根据您的编码约定定义标准排序。

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. 虽然IntelliJ隐式提供此功能,但它需要启用和修复。 The OP's suggested way is technically arranging methods in depth-first order. OP建议的方法是从技术上按深度优先顺序排列方法。 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 . 问题链接: 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. 这没有为其分配键,但您可以使用Preferences-> Keymap定义自己的键。

RearrangeActionMenuButton

在此输入图像描述

IntelliJ has a built in system that allows you to specify how to order your methods. IntelliJ具有内置系统,允许您指定如何订购方法。 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 . 您需要转到设置(Ctrl + Alt + S) - >编辑器 - >代码样式 - > Java - >排列(选项卡)并向下滚动,直到找到带有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 (az). 这是我的设置的屏幕截图,它将通过可见性(公共,受保护,私有)和按字母顺序(az)自动排序方法。 The blue highlights show the currently selected rules. 蓝色突出显示当前选定的规则。

在此输入图像描述

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. 将光标放在方法定义行上(您不必按,然后按ctrl + shift + 向上ctrl + shift + 向下 ,分别向上或向下移动。

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 -shift-equals也适用于我的笔记本电脑)。

选择一个文本块(按Ctrl-W几次),然后使用Ctrl-Shift-Up和Ctrl-Shift-Down移动它。

There is an automated way, which you can later tweak 有一种自动方式,您可以稍后进行调整

Code -> show reformat file dialog 代码 - >显示重新格式化文件对话框

and tick "rearrange code" box 并勾选“重新排列代码”框

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM