如何阻止Eclipse格式化程序将所有枚举放在一行中 [英] How to stop Eclipse formatter from placing all enums on one line

查看:1419
本文介绍了如何阻止Eclipse格式化程序将所有枚举放在一行中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有枚举:

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:


  1. 窗口> 首选项> Java > code>> Formatter

  2. 点击编辑

  3. 选择行包装选项卡

  4. 选择枚举声明treenode

  5. 设置换行策略将所有元素,新行上的每个元素...)所以现在说在括号中3的3。

  6. 取消选中强制拆分,即使线短于最大行宽度(...)所以现在说括号中3的3。

  7. 选择常量 treenode

  8. 检查强制拆分,即使线小于最大线宽

  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

将枚举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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆