如何阻止Eclipse格式化程序将所有枚举放在一行中 [英] How to stop Eclipse formatter from placing all enums on one line
问题描述
我有枚举:
public static enum Command
{
login,
register,
logout,
newMessage
}
格式化时该文件,输出成为:
When formatting the file, the output becomes:
public static enum Command
{
login, register, logout, newMessage
}
推荐答案
@wjans的答案对正常枚举工作正常,但不是有参数的枚举。为了扩展他的答案,这里的设置为Eclipse Juno中为我提供了最明智的格式:
The answer by @wjans worked fine for normal enums, but not for enums with arguments. To expand on his answer a bit, here's the settings that provided the most sensible formatting for me in Eclipse Juno:
-
窗口
>首选项
>Java
>code>>
Formatter
- 点击
编辑
- 选择
行包装
选项卡 - 选择
枚举
声明treenode - 设置
换行策略
到将所有元素,新行上的每个元素...)
所以现在说在括号中3的3。 - 取消选中
强制拆分,即使线短于最大行宽度(...)
所以现在说括号中3的3。 - 选择
常量
treenode - 检查
强制拆分,即使线小于最大线宽
Window
>Preferences
>Java
>Code Style
>Formatter
- Click
Edit
- Select the
Line Wrapping
tab - Select the
enum
declaration treenode - Set
Line wrapping policy
toWrap all elements, every element on a new line (...)
so it now says 3 of 3 in the parenthesis. - Uncheck
Force split, even if line shorter than maximum line width (...)
so it now says 3 of 3 in the parenthesis. - Select the
Constants
treenode - Check
Force split, even if line shorter than maximum line width
将枚举treenode的3个子节点设置为相同的包装策略,同样的forc e分割策略,除了常量
treenode,所以你的枚举与参数将被格式化,每一行都在自己的行上。
This sets the 3 subnodes for the enum treenode to the same wrapping policy, and the same force split policy except for the Constants
treenode, so your enums with arguments will be formatted each on their own line. The arguments will only wrap if they exceed maximum line width.
示例:
@wjans
enum Example {
CANCELLED,
RUNNING,
WAITING,
FINISHED
}
enum Example {
GREEN(
0,
255,
0),
RED(
255,
0,
0)
}
上述解决方案
enum Example {
CANCELLED,
RUNNING,
WAITING,
FINISHED
}
enum Example {
GREEN(0, 255, 0),
RED(255, 0, 0)
}
这篇关于如何阻止Eclipse格式化程序将所有枚举放在一行中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!