How to tell Checkstyle to check all lines for linefeed / newline LF and not CRLF?

*爱你&永不变心* 提交于 2021-02-08 03:01:39

问题


Is there a way to check if the file is using LFs only and no CRLFs or vice versa in checkstyle?

The only check which I have found is

NewlineAtEndOfFile

But I search for more as only at the end of the file?


回答1:


Only to supplement the Michal Kordas's good answer.

Below is a slightly modified configuration, which will only match the first wrong newline and forbid also the Mac OS Line Endings (CR):

<module name="RegexpMultiline">
    <property name="format" value="(?s:(\r\n|\r).*)"/>
    <property name="message" value="CRLF and CR line endings are prohibited, but this file uses them."/>
</module>

It has been posted by Michael Vorburger in this discussion, so kudos for him - I've posted it here for completeness.




回答2:


There is nothing out-of-the box in Checkstyle yet. If you want to prohibit CRLF you can use the following config:

<module name="RegexpMultiline">
    <property name="format" value="\r\n"/>
    <property name="message" value="CRLF line endings are prohibited"/>
</module>

and this one to ban LFs:

<module name="RegexpMultiline">
    <property name="format" value="(?<!\r)\n"/>
    <property name="message" value="LF line endings are prohibited"/>
</module>


来源:https://stackoverflow.com/questions/32903412/how-to-tell-checkstyle-to-check-all-lines-for-linefeed-newline-lf-and-not-crlf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!