Builder 模式的 Eclipse 格式化程序设置

发布于 2024-08-18 23:23:19 字数 2937 浏览 4 评论 0原文

我对一系列合格调用(即生成器模式样式)的 Eclipse 格式化规则感到非常沮丧。例如,以下是我对创建新的 Apache Commons CLI Options 对象的某些代码的首选格式:

  Options options = new Options()
      .addOption(OPTION_HELP_SHORT, OPTION_HELP, false, "print usage information")
      .addOption(OPTION_VERSION_SHORT, OPTION_VERSION, false,
          "print version and exit")
      .addOption(OptionBuilder.withLongOpt(OPTION_PROPERTIES)
                     .hasArg()
                     .withArgName("FILE")
                     .withType(File.class)
                     .withDescription("specify a user properties file")
                     .create());

即,如有必要,参数将被包装并缩进,并且除第一个之外的所有合格调用(除非必要)都会被包装如果有多个,则缩进。如果参数列表包装在限定调用内,则该调用应首先包装。

Eclipse 中的默认格式(对于参数和调用“仅在必要时换行”)会产生以下混乱:

  Options options = new Options().addOption(
      OPTION_HELP_SHORT, OPTION_HELP, false, "print usage information")
      .addOption(OPTION_VERSION_SHORT, OPTION_VERSION, false,
          "print version and exit").addOption(
          OptionBuilder.withLongOpt(OPTION_PROPERTIES).hasArg().withArgName(
              "FILE").withType(File.class).withDescription(
              "specify a user properties file").create());

进入“Java 代码样式 -> 格式化程序 -> 换行”并将换行设置为“换行所有元素,除了第一个元素,如果不需要”调用会产生:

  Options options = new Options().addOption(
      OPTION_HELP_SHORT, OPTION_HELP, false, "print usage information")
      .addOption(OPTION_VERSION_SHORT, OPTION_VERSION, false,
          "print version and exit")
      .addOption(
          OptionBuilder.withLongOpt(OPTION_PROPERTIES).hasArg().withArgName(
              "FILE").withType(File.class).withDescription(
              "specify a user properties file").create());

我不喜欢 OptionBuilder 表达式没有被包装,或者 "FILE" 被包装而不包装 <代码>withArgName。

将缩进更改为“列上缩进”会产生:

  Options options = new Options().addOption(OPTION_HELP_SHORT, OPTION_HELP,
                                     false, "print usage information")
                                 .addOption(OPTION_VERSION_SHORT,
                                     OPTION_VERSION, false,
                                     "print version and exit")
                                 .addOption(
                                     OptionBuilder.withLongOpt(
                                                      OPTION_PROPERTIES)
                                                  .hasArg()
                                                  .withArgName("FILE")
                                                  .withType(File.class)
                                                  .withDescription(
                                                      "specify a user properties file")
                                                  .create());

在我想要的地方打破行,但将内容推到右侧太远。

有什么方法可以说服 Eclipse 应用我喜欢的格式化样式或比上述任何一种更接近的格式化样式吗?

I'm extremely frustrated with the Eclipse formatting rules for a series of qualified invocations (i.e., the Builder pattern style). For example, here is my preferred formatting for some code that creates a new Apache Commons CLI Options object:

  Options options = new Options()
      .addOption(OPTION_HELP_SHORT, OPTION_HELP, false, "print usage information")
      .addOption(OPTION_VERSION_SHORT, OPTION_VERSION, false,
          "print version and exit")
      .addOption(OptionBuilder.withLongOpt(OPTION_PROPERTIES)
                     .hasArg()
                     .withArgName("FILE")
                     .withType(File.class)
                     .withDescription("specify a user properties file")
                     .create());

I.e., parameters are wrapped and indented if necessary and all qualified invocations except the first, unless necessary, are wrapped and indented if there is more than one. If a parameter list wraps inside a qualified invocation, the invocation should wrap first.

The default formatting in Eclipse ("Wrap only when necessary" for arguments and invocations) yields the following mess:

  Options options = new Options().addOption(
      OPTION_HELP_SHORT, OPTION_HELP, false, "print usage information")
      .addOption(OPTION_VERSION_SHORT, OPTION_VERSION, false,
          "print version and exit").addOption(
          OptionBuilder.withLongOpt(OPTION_PROPERTIES).hasArg().withArgName(
              "FILE").withType(File.class).withDescription(
              "specify a user properties file").create());

Going into "Java Code Style -> Formatter -> Line Wrapping" and the line wrapping setting to "Wrap all elements, except first element if not necessary" for invocations yields:

  Options options = new Options().addOption(
      OPTION_HELP_SHORT, OPTION_HELP, false, "print usage information")
      .addOption(OPTION_VERSION_SHORT, OPTION_VERSION, false,
          "print version and exit")
      .addOption(
          OptionBuilder.withLongOpt(OPTION_PROPERTIES).hasArg().withArgName(
              "FILE").withType(File.class).withDescription(
              "specify a user properties file").create());

I don't like that the OptionBuilder expression isn't being wrapped, or that "FILE" gets wrapped without also wrapping withArgName.

Changing the indentation to "Indent on column" yields:

  Options options = new Options().addOption(OPTION_HELP_SHORT, OPTION_HELP,
                                     false, "print usage information")
                                 .addOption(OPTION_VERSION_SHORT,
                                     OPTION_VERSION, false,
                                     "print version and exit")
                                 .addOption(
                                     OptionBuilder.withLongOpt(
                                                      OPTION_PROPERTIES)
                                                  .hasArg()
                                                  .withArgName("FILE")
                                                  .withType(File.class)
                                                  .withDescription(
                                                      "specify a user properties file")
                                                  .create());

The is breaking the lines where I'd prefer, but pushing things over much too far to the right.

Is there any way to convince Eclipse to apply my preferred formatting style or something closer to it than any of the above?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

半步萧音过轻尘 2024-08-25 23:23:20

关闭带注释的格式,或者插入行注释太繁琐了。

最好的方法在此处进行了描述:

...或者您可以选择“换行>从不加入已换行”
lines”全局。然后,您可以手动打破它,格式化程序
只会格式化内部行(或者添加额外的换行符,如果
必要)。

通过此设置,Eclipse 格式化程序将停止破坏您的构建器语句。

输入图像描述这里

Turning off formatting with comments, or inserting line comments is too tedious.

The best way is described here:

... or you can select "Line Wrapping > Never join already wrapped
lines" globally. Then, you can break it manually and the formatter
will only format inside lines (or add additional line breaks if
necessary).

With this setting Eclipse formatter will stop ruining your builder statements.

enter image description here

回梦 2024-08-25 23:23:20

使用评论:

   Object o = foo() //
      .bar() //
      .toString();

Use comments:

   Object o = foo() //
      .bar() //
      .toString();
街角卖回忆 2024-08-25 23:23:20

2021 年更新。可以更改,导航到:代码样式 ->格式化程序->换行 ->环绕设置->函数调用->合格的调用并将值更改为“包裹所有元素,如果不需要,除了第一个元素

Update for 2021. It is possible to change, navigate to: Code Style -> Formatter -> Line Wrapping -> Wrapping settings -> Function Calls -> Qualified invocations and change value to "Wrap all elements, except first element if not necessary"

月牙弯弯 2024-08-25 23:23:20

在 Eclipse 3.6 中,可以关闭代码区域的格式设置。请参阅我对

如何关闭Java代码某些部分的Eclipse代码格式化程序?

In Eclipse 3.6 It's possible to turn off formatting for a region of code. See my answer to

How to turn off the Eclipse code formatter for certain sections of Java code?

楠木可依 2024-08-25 23:23:20

AFAIK,这是 Eclipse Formatter 的已知问题:
https://bugs.eclipse.org/bugs/show_bug.cgi?id= 59891

AFAIK, this is known problem with Eclipse Formatter:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=59891

撕心裂肺的伤痛 2024-08-25 23:23:20

在菜单上选择“窗口”->“窗口”窗口打开时首选项选择 Java ->代码风格->格式化程序,您可以通过选择新选项或编辑选项来创建自己的格式样式。编辑格式配置文件时,会打开一个新窗口,为您提供许多不同的选项供您使用。

On the menu select Window -> Preferences when the window opens select Java -> Code Style -> Formatter and from there you can create your own format style to use by selecting the new or edit option. When editing a formatting profile a new window opens that gives you a lot of different options to use.

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