代码之家  ›  专栏  ›  技术社区  ›  Charles Duffy

构建器模式的Eclipse格式化程序设置

  •  36
  • Charles Duffy  · 技术社区  · 15 年前

    对于一系列合格调用(即构建器模式样式)的Eclipse格式化规则,我非常失望。例如,下面是我对一些创建新的ApacheCommonsCLI的代码的首选格式 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代码样式-gt;格式化程序-gt;行封装”和“行包装”设置,以“调用所有元素,如果没有必要的话,除了第一个元素”。

      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应用我喜欢的格式样式或者比上面任何一种更接近它的样式?

    5 回复  |  直到 15 年前
        1
  •  31
  •   Thorbjørn Ravn Andersen    15 年前

    使用注释:

       Object o = foo() //
          .bar() //
          .toString();
    
        2
  •  29
  •   Jakub Bochenski    8 年前

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

    最好的方法被描述为 这里 ::

    < Buff行情>

    或者您可以选择“换行”>“从不加入已换行” 行“全球。然后,您可以手动断开它和格式化程序 将只设置行内格式(或添加其他换行符,如果 必要的)

    < /块引用>

    使用此设置,Eclipse格式化程序将停止破坏您的生成器语句。

    :

    …或者您可以选择“换行”>“从不加入已换行” 行“全球。然后,您可以手动断开它和格式化程序 将只设置行内格式(或添加其他换行符,如果 必要的)。

    使用此设置,Eclipse格式化程序将停止破坏构建器语句。

    enter image description here

        3
  •  4
  •   Community CDub    7 年前

    在Eclipse3.6中,可以关闭代码区域的格式设置。看看我的答案

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

        4
  •  3
  •   nanda    15 年前

    Afaik,这是Eclipse格式化程序的已知问题: https://bugs.eclipse.org/bugs/show_bug.cgi?id=59891

        5
  •  -3
  •   ChadNC    15 年前

    在菜单中选择Window & Gt;窗口打开时的首选项,选择Java -Gt;代码样式-G.FATATER,从那里您可以通过选择新的或编辑选项来创建自己的格式样式。编辑格式化配置文件时,会打开一个新窗口,为您提供许多不同的选项。