-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rule to enforce blank lines as wrap of class declaration #3379
Comments
I can understand the blank line at the start of the class def to give some space before the first field or method. But why at the end? A closing brace followed by another with proper indentation seems fine. }
} I can tell the class is closed as the last brace in the file, also no indent clues me in. If you wanted to enforce this under the current Checkstyle, you could leverage the fact that the brace is at column 0 and write a RegexMultiline check. This would not help you for inner classes/interfaces/enums though. |
Interesting enough, we have exact the opposite code style convention of removing an empty line between closing braces, but I can't find a way to enforce it with the checkstyle. |
Awesome! I'd love to see support for enforcing both styles in Checkstyle configuration. |
To enforce no blank lines at the beginning and end of any block, we use multi-line regular expression checks: <module name="RegexpMultiline">
<property name="message" value="Blank line at start of block should be removed" />
<property name="format" value="(?<=\{\s{0,99}$)^$" />
<property name="fileExtensions" value="groovy,java" />
</module>
<module name="RegexpMultiline">
<property name="message" value="Blank line at end of block should be removed" />
<property name="format" value="(?<!\{\s{0,99}$)^$(?=^\s{0,99}\})" />
<property name="fileExtensions" value="groovy,java" />
</module> "^$" signifies the blank line. |
@mcandre , we have one more request for the same feature https://groups.google.com/forum/#!topic/checkstyle/rzNJDM_uvyg , and looks like I lost tracking of this issue.
please explain why you need multiple lines (2 empty lines or more) ? |
This issue is hard to read and understand what is requested. I created new issue #3923 with clear request for new functionality. request for "rule that enforces n (0, 1, 2, ...) blank lines in this context" is skipped as author did not explain a reason of it. Implementation of new option as described in new issue will not brake compatibility with existing functionality. If somebody care about NoEmptyLineSeparator - please create new issue and define usecases. @plastiv , please do. There was similar request at #3089 (comment) . @robertpainsi . |
UPDATE: for better answer check this: #3379 (comment) Quick solution with multiline regex (the code should be well formatted):
|
@ati90ati for Windows support, I added <module name="RegexpMultiline">
<property name="format" value="^([^[\r]?\n ]+ )*(class|interface|enum) [^{]*\{[\r]?\n[^\r\n}]"/>
<property name="message" value="Leave empty row after class/interface/enum definition!"/>
<property name="severity" value="error"/>
<property name="fileExtensions" value="groovy,java"/>
</module>
<module name="RegexpMultiline">
<property name="format" value="[^\r\n{][\r]?\n\}[\r]?\n"/>
<property name="message" value="Leave empty row before end of class/interface/enum!"/>
<property name="severity" value="error"/>
<property name="fileExtensions" value="groovy,java"/>
</module> |
@GuyLewin I've checked your suggestion. For me, the first stopped working. It's not supposed to have this in regex: My current approach is this (I adapted your Windows support, this is my best guess however I have not tested it on Windows): <module name="RegexpMultiline">
<!--
To fix all occurrences (with IntelliJ IDEA 2018 on Linux tested)
Add newline at class start (file mask *.java)
Find: (^([^\r\n ]+ )*(class|interface|@interface|enum) [^{]*\{[\r]?\n)([^\r\n}])
Replace: $1\n$4
-->
<property name="id" value="RegexpMultilineEmptyRowAfterClassDef"/>
<property name="format" value="^([^\r\n ]+ )*(class|interface|@interface|enum) [^{]*\{[\r]?\n[^\r\n}]"/>
<property name="message" value="Leave empty row after class/interface/@interface/enum definition!"/>
<property name="severity" value="error"/>
<property name="fileExtensions" value="groovy,java"/>
</module>
<module name="RegexpMultiline">
<!--
To fix all occurrences (with IntelliJ IDEA 2018 on Linux tested)
Add newline at class end (file mask *.java)
Find: ([^\r\n{][\r]?\n)(\}[\r]?\n)
Replace: $1\n$2
-->
<property name="id" value="RegexpMultilineEmptyRowBeforeClassEnd"/>
<property name="format" value="[^\r\n{][\r]?\n\}[\r]?\n"/>
<property name="message" value="Leave empty row before end of class/interface/@interface/enum!"/>
<property name="severity" value="error"/>
<property name="fileExtensions" value="groovy,java"/>
</module> UPDATE 2: |
@ati90ati, thank you for the solution! If I also want to check inner/nested classes for
(added |
it would be awesome if somebody find a time to formalize #5313 and even implement it. |
@pavelicii I updated my comment with the latest regex I have. I think your suggestion is accurate if you want to check inner classes too. |
Extended version of @pavelicii pattern: I added annotation checks and empty class declaration ignoring Demo can be checked here: |
Oracle, IBM, Google, and Mozilla Java code often inserts a blank line between the opening curly brace of a class declaration and the class contents, as well as a blank line between the contents and the closing class curly brace. For example:
https://github.com/oracle/atg-build-tools/blob/master/atg-maven-plugin/src/main/java/com/oracle/atg/ateam/RunAssemblerMojo.java
Classes with no contents require no blank lines:
Could Checkstyle please feature a rule that enforces n (0, 1, 2, ...) blank lines in this context?
There appears to be a rule for enforcing a blank line at the beginning of class declarations, but I can't find one to enforce a blank line at the end as well.
The text was updated successfully, but these errors were encountered: