如何阻止 Eclipse 格式化程序将所有枚举放在一行上
我有这样的枚举:
public static enum Command
{
login,
register,
logout,
newMessage
}
格式化文件时,输出变为:
public static enum Command
{
login, register, logout, newMessage
}
I have enums like:
public static enum Command
{
login,
register,
logout,
newMessage
}
When formatting the file, the output becomes:
public static enum Command
{
login, register, logout, newMessage
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您需要在“常量”的枚举声明下设置换行策略。
将换行策略设置为
并且
You need to set the line wrapping policy under enum declaration for "Constants."
Set the wrapping policy to
AND
只需添加最新的 Eclipse 2018.9
Window >首选项>爪哇>代码风格> Formatter
-Edit
Line Wrapping
树节点。包装设置
'enum'声明
常量
和常量参数
。常量需要
将所有元素换行,每个元素都在新行上
。常量参数需要
必要时换行
。Just adding latest Eclipse 2018.9
Window > Preferences > Java > Code Style > Formatter
-Edit
Line Wrapping
tree node.Wrapping settings
'enum' declaration
Constants
andConstant arguments
.Constants need to be
Wrap all elements, every element on a new line
.Constant arguments need to be
Wrap where necessary
.就我而言,将 org.eclipse.jdt.core.formatter.alignment_for_enum_constants 设置为 17 与 maven 格式化程序插件一起使用:https://code.revelc.net/formatter-maven-plugin/
https://gitlab .inria.fr/stopcovid19/submission-code-server/-/blob/develop/.etc/eclipse-formatter-java.xml
in my case set
org.eclipse.jdt.core.formatter.alignment_for_enum_constants
to17
works with maven formatter plugin: https://code.revelc.net/formatter-maven-plugin/https://gitlab.inria.fr/stopcovid19/submission-code-server/-/blob/develop/.etc/eclipse-formatter-java.xml
@wjans 的答案对于普通枚举来说效果很好,但对于带有参数的枚举则不然。为了稍微扩展一下他的答案,以下是在 Eclipse Juno 中为我提供最合理格式的设置:
Window
>首选项
>Java
> > Java代码风格
>Formatter
Edit
Line Wrapping
选项卡enum
声明树节点换行策略
code> 到将所有元素换行,每个元素都在新行上 (...)
,因此现在括号中显示 3 of 3。强制分割,即使行短于最大行宽 (...)
,因此现在括号中显示 3 of 3。Constants
树节点强制分割,即使行短于最大线宽
这将枚举树节点的3个子节点设置为相同的包装策略和相同的强制分割除了
Constants
树节点之外的策略,因此带有参数的枚举将在各自的行上进行格式化。仅当参数超过最大行宽时,参数才会换行。示例:
@wjans
上述解决方案:
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:
Window
>Preferences
>Java
>Code Style
>Formatter
Edit
Line Wrapping
tabenum
declaration treenodeLine wrapping policy
toWrap all elements, every element on a new line (...)
so it now says 3 of 3 in the parenthesis.Force split, even if line shorter than maximum line width (...)
so it now says 3 of 3 in the parenthesis.Constants
treenodeForce split, even if line shorter than maximum line width
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.Examples:
@wjans
Solution described above:
您可以在格式化程序首选项中指定这一点:
You can specify this in your formatter preferences:
它也有点难看,但是如果您的公司政策阻止您更改格式化程序,您可以将注释放在您不想换行的行末尾。
It's slightly ugly too, but if your company policy prevents you from changing the formatter, you can just put comments at the end of lines you don't want to be wrapped.
这不太好,但您可以关闭某些代码部分的 Eclipse 格式化程序...
该选项位于 Windows->Preferences->Java->Code Style->Formatter->Edit->Off /在标签面板上
It's not nice but you can turn the Eclipse formatter off for some sections of code...
the option is in the Windows->Preferences->Java->Code Style->Formatter->Edit->Off/On Tags panel