带有checkstyle和sonarlint的Eclipse格式化程序

问题描述 投票:0回答:1

我安装了这个插件,似乎没有考虑Eclipse Formatter。

例如,我的链函数看起来像Formatter中的下面的代码:

stringB.append("a")
       .append("b")
       .append("c")

但是,当我问eclipse到autoformat(我猜使用Formatter)时,代码放置如下:

stringB.append("a").append("b").append("c")

知道为什么会这样吗? CheckStyle是否覆盖了Eclipse定义的Formatter。我该如何解决这个问题?

eclipse checkstyle formatter sonarlint
1个回答
0
投票

知道为什么会这样吗?

它可能正在发生,因为这可能是您正在使用的格式化程序的默认行为。为了防止这种情况,您可以选中名为“从未加入已包裹的行”的框:

neverJoinImage

但是,有一个更好的解决方案(详见下文),甚至不需要检查该框。

CheckStyle是否覆盖了Eclipse定义的Formatter。

不,但您可以轻松确认此问题不是由Checkstyle引起的:

  • 在新行上使用每个链接方法格式化一些代码。
  • 暂时关闭CheckStyle:{project} - >右键单击 - > CheckStyle - > Deactivate Checkstyle
  • 格式化代码(Ctrl-Shift-F)。您应该看到格式问题仍然存在。

我该如何解决这个问题?

您需要修改格式化程序的设置:

  • 窗口>首选项> Java>代码样式>格式化程序
  • 单击编辑...按钮。 (这假定您已经创建了自己的活动配置文件以进行格式化。如果没有,则需要单击“新建...”按钮并首先执行此操作。)
  • 在打开以修改配置文件设置的窗口中,在左列中导航到“线包裹”>“环绕设置”>“函数调用”>“限定调用”。
  • 该行有三个图标,每个图标必须正确设置: 单击最左侧的图标,然后选择最后一个选项Wrap all elements,如果不需要,则从上下文菜单中选择第一个元素。 单击中间图标。这是一个切换开关,点击一次就可以获得它周围的边框。即使行短于最大行宽,也会设置“强制拆分”选项。 单击最右侧的图标,然后从上下文菜单中选择最后一个选项“缩进”。 Invocations

应用这些更改,然后重新格式化代码。格式化程序现在应该按照您指定的方式运行。下面是一些代码示例,其中包含链式方法调用的格式化的各种问题:

    List<String> months = Arrays.asList("January", "February", "March", "April", "May", "June");
    List<String> months5 = months.stream().filter(s -> s.length() == 5).collect(Collectors.toList());
    StringBuilder stA = new StringBuilder();
    StringBuilder stringB = new StringBuilder();
    StringBuilder thisIsStringC = new StringBuilder();

    stA.append("a").append("b").append("c");

    stringB.append("a")
        .append("b")
        .append("c");

    stringB.append("d").append("e")
                             .append("f");

    thisIsStringC  .append("a")
                   .append("b")
                   .append("c");

    thisIsStringC.append("d")
                 .append("e");

以下是使用上述设置重新格式化(Ctrl-Shift-F)后的样子:

    List<String> months = Arrays.asList("January", "February", "March", "April", "May", "June");
    List<String> months5 = months.stream()
                                 .filter(s -> s.length() == 5)
                                 .collect(Collectors.toList());
    StringBuilder stA = new StringBuilder();
    StringBuilder stringB = new StringBuilder();
    StringBuilder thisIsStringC = new StringBuilder();

    stA.append("a")
       .append("b")
       .append("c");

    stringB.append("a")
           .append("b")
           .append("c");

    stringB.append("d")
           .append("e")
           .append("f");

    thisIsStringC.append("a")
                 .append("b")
                 .append("c");

    thisIsStringC.append("d")
                 .append("e");
© www.soinside.com 2019 - 2024. All rights reserved.