[英]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 :
为防止这种情况,您可以选中名为“ 从不加入已包装的行”的框:
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引起的:
How should I proceed to fix this?
我应该如何解决这个问题?
You need to modify the settings of your formatter: 您需要修改格式化程序的设置:
There are three icons on that line, each of which must be set correctly: 该行上有三个图标,必须正确设置每个图标:
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.