简体   繁体   English

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

[英]Eclipse formatter with checkstyle and sonarlint

I have this plugins installed and it seems that the Eclipse Formatter is not taken into consideration. 我已经安装了此插件,并且似乎未考虑Eclipse Formatter。

For instance, I have my chain funtions to look like the below code in Formatter: 例如,我的链功能看起来像Formatter中的以下代码:

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

But when I ask eclipse to autoformat (I guess using the Formatter) the code is placed like: 但是,当我要求eclipse自动格式化(我猜是使用格式化程序)时,代码如下所示:

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

Any idea why is this happening? 知道为什么会这样吗? Is CheckStyle overriding my Eclipse defined Formatter. CheckStyle是否覆盖了Eclipse定义的Formatter。 How should I proceed to fix this? 我应该如何解决这个问题?

Any idea why is this happening? 知道为什么会这样吗?

It is probably happening because that is probably the default behavior of the formatter you are using. 这可能是发生的,因为这可能是您使用的格式化程序的默认行为。 To prevent it you could check the box named Never joined already wrapped lines : 为防止这种情况,您可以选中名为“ 从不加入已包装的行”的框:

neverJoinImage

However, there is a better solution (detailed below) that doesn't even require that box to be checked. 但是,有一个更好的解决方案(在下面详细介绍),甚至不需要选中该框。

Is CheckStyle overriding my Eclipse defined Formatter. CheckStyle是否覆盖了Eclipse定义的Formatter。

No, but you can easily confirm that this issue is not caused by Checkstyle: 不,但是您可以轻松确认此问题不是由Checkstyle引起的:

  • Format some code with each chained method on a new line. 使用每个链接方法在新行上格式化一些代码。
  • Temporarily turn off CheckStyle: {project} -> right click -> CheckStyle -> Deactivate Checkstyle 暂时关闭CheckStyle: {项目}->右键单击-> CheckStyle->停用Checkstyle
  • Format your code ( Ctrl-Shift-F ). 格式化代码( Ctrl-Shift-F )。 You should see that the formatting problem persists. 您应该看到格式问题仍然存在。

How should I proceed to fix this? 我应该如何解决这个问题?

You need to modify the settings of your formatter: 您需要修改格式化程序的设置:

  • Window > Preferences > Java > Code Style > Formatter 窗口>首选项> Java>代码样式>格式化程序
  • Click the Edit... button. 单击编辑...按钮。 (This assumes that you have already created your own Active Profile for formatting. If not, you will need to click the New... button and do that first.) (这假设您已经创建了自己的Active Profile进行格式化。如果没有,则需要单击New ...按钮,然后首先执行该操作。)
  • In the window that opens for modifying your profile settings, in the left column navigate to Line Wrapping > Wrapping settings > Function Calls > Qualified invocations . 在打开的用于修改配置文件设置的窗口中,在左列中导航至Line Wrapping> Wrapping settings> Function Calls> Qualified invocations
  • There are three icons on that line, each of which must be set correctly: 该行上有三个图标,必须正确设置每个图标:

    • Click the leftmost icon and select the final option Wrap all elements, except first element if not necessary from the context menu. 单击最左侧的图标,然后从上下文菜单中选择最终选项“ 包装所有元素,但第一个元素除外”
    • Click the middle icon. 单击中间的图标。 This is a toggle switch and it will acquire a border around it when clicked once. 这是一个拨动开关,单击一次将获得一个边框。 This sets the option Force split, even if line shorter than maximum line width . 即使行短于最大行宽,也可以设置选项“ 强制拆分”
    • Click the rightmost icon and select the final option Indent on column from the context menu. 单击最右边的图标,然后从上下文菜单中选择最终选项“ 在列上缩进”

      调用

Apply those changes, and then reformat your code. 应用这些更改,然后重新格式化您的代码。 The formatter should now behave as you specified. 现在,格式化程序应按照您指定的方式运行。 Here's an example of some code with various problems with the formatting of chained method calls: 这是一些代码的示例,这些代码在链接方法调用的格式方面存在各种问题:

    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");

And here's what it looks like after reformatting ( Ctrl-Shift-F ) using the settings described above: 这是使用上述设置重新格式化( 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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM