如何阻止 Eclipse 格式化程序将所有枚举放在一行上

发布于 2024-11-19 22:42:34 字数 223 浏览 2 评论 0原文

我有这样的枚举:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

反目相谮 2024-11-26 22:42:35

您需要在“常量”的枚举声明下设置换行策略。

将换行策略设置为

  • 换行所有元素,新行上的每个元素

并且

  • 选中“强制拆分,即使行短于,,,,,

You need to set the line wrapping policy under enum declaration for "Constants."

Set the wrapping policy to

  • Wrap all elements, every element on a new line

AND

  • Check the box that says "Force Split, even if line shorter than,,,,,
海未深 2024-11-26 22:42:35

只需添加最新的 Eclipse 2018.9

  1. Window >首选项>爪哇>代码风格> Formatter - Edit
  2. 展开Line Wrapping 树节点。
  3. 展开包装设置
  4. 展开'enum'声明
  5. 编辑常量常量参数

常量需要将所有元素换行,每个元素都在新行上
常量参数需要必要时换行

Just adding latest Eclipse 2018.9

  1. Window > Preferences > Java > Code Style > Formatter - Edit
  2. Expand Line Wrapping tree node.
  3. Expand Wrapping settings
  4. Expand 'enum' declaration
  5. Edit Constants and Constant arguments.

Constants need to be Wrap all elements, every element on a new line.
Constant arguments need to be Wrap where necessary.

捶死心动 2024-11-26 22:42:35

就我而言,将 org.eclipse.jdt.core.formatter.alignment_for_enum_constants 设置为 17 与 maven 格式化程序插件一起使用:https://code.revelc.net/formatter-maven-plugin/

    <setting
      id="org.eclipse.jdt.core.formatter.alignment_for_enum_constants"
      value="17"
    />

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 to 17 works with maven formatter plugin: https://code.revelc.net/formatter-maven-plugin/

    <setting
      id="org.eclipse.jdt.core.formatter.alignment_for_enum_constants"
      value="17"
    />

https://gitlab.inria.fr/stopcovid19/submission-code-server/-/blob/develop/.etc/eclipse-formatter-java.xml

梦中的蝴蝶 2024-11-26 22:42:34

@wjans 的答案对于普通枚举来说效果很好,但对于带有参数的枚举则不然。为了稍微扩展一下他的答案,以下是在 Eclipse Juno 中为我提供最合理格式的设置:

  1. Window > 首选项 > Java > > Java 代码风格 > Formatter
  2. 单击Edit
  3. 选择Line Wrapping 选项卡
  4. 选择enum 声明树节点
  5. 设置换行策略 code> 到 将所有元素换行,每个元素都在新行上 (...),因此现在括号中显示 3 of 3。
  6. 取消选中强制分割,即使行短于最大行宽 (...),因此现在括号中显示 3 of 3。
  7. 选择Constants树节点
  8. 检查强制分割,即使行短于最大线宽

这将枚举树节点的3个子节点设置为相同的包装策略和相同的强制分割除了 Constants 树节点之外的策略,因此带有参数的枚举将在各自的行上进行格式化。仅当参数超过最大行宽时,参数才会换行。

示例:

@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)
}

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:

  1. Window > Preferences > Java > Code Style > Formatter
  2. Click Edit
  3. Select the Line Wrapping tab
  4. Select the enum declaration treenode
  5. Set Line wrapping policy to Wrap all elements, every element on a new line (...) so it now says 3 of 3 in the parenthesis.
  6. Uncheck Force split, even if line shorter than maximum line width (...) so it now says 3 of 3 in the parenthesis.
  7. Select the Constants treenode
  8. Check Force 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

enum Example {
    CANCELLED,
    RUNNING,
    WAITING,
    FINISHED
}

enum Example {
    GREEN(
        0,
        255,
        0),
    RED(
        255,
        0,
        0)
}

Solution described above:

enum Example {
    CANCELLED,
    RUNNING,
    WAITING,
    FINISHED
}

enum Example {
    GREEN(0, 255, 0),
    RED(255, 0, 0)
}
黎夕旧梦 2024-11-26 22:42:34

您可以在格式化程序首选项中指定这一点:

  • 首选项:Java -- 代码样式 -- 格式化程序
  • 单击编辑
  • 选择“换行”选项卡
  • 选择“枚举”声明 ->左侧框中的常量
  • 将换行策略设置为“换行所有元素,每个元素在新行上”
  • 选中“强制拆分...”

You can specify this in your formatter preferences:

  • Preferences: Java -- Code Style -- Formatter
  • Click Edit
  • Select the 'Line Wrapping' tab
  • Select 'enum' declaration -> Constants in the box on the left
  • Set Line wrapping policy to 'Wrap all elements, every element on a new line'
  • Check 'Force split...'
深府石板幽径 2024-11-26 22:42:34

它也有点难看,但是如果您的公司政策阻止您更改格式化程序,您可以将注释放在您不想换行的行末尾。

public static enum Command 
{
    login,//
    register,//
    logout,//
    newMessage//
};

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.

public static enum Command 
{
    login,//
    register,//
    logout,//
    newMessage//
};
撑一把青伞 2024-11-26 22:42:34

这不太好,但您可以关闭某些代码部分的 Eclipse 格式化程序...

// @formatter:off
public static enum Command {
    login,
    register,
    logout,
    newMessage
};
// @formatter:on

该选项位于 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...

// @formatter:off
public static enum Command {
    login,
    register,
    logout,
    newMessage
};
// @formatter:on

the option is in the Windows->Preferences->Java->Code Style->Formatter->Edit->Off/On Tags panel

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文