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:
Classes with no contents require no blank lines:
public class SomeClass {}
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.
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.
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.
Could Checkstyle please feature a rule that enforces n (0, 1, 2, ...) blank lines in this context?
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 https://github.com/checkstyle/checkstyle/issues/3089#issuecomment-278135207 . @robertpainsi .
Quick solution with multiline regex (the code should be well formatted):
<module name="RegexpMultiline">
<property name="format" value="^([^\n ]+ )*(class|interface|enum) [^{]*\{\n[^\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="[^\n{]\n\}\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>
@ati90ati for Windows support, I added \r:
<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: [^[\r]?\n ]
Bracket in bracket not supposed to be used.
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">
<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">
<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>
Most helpful comment
To enforce no blank lines at the beginning and end of any block, we use multi-line regular expression checks:
"^$" signifies the blank line.