Skip to content

Commit

Permalink
Revert "Empty Line Separator Check, added option for managing empty l…
Browse files Browse the repository at this point in the history
…ines between class members, issue #530"

This reverts commit 3f853ae.
  • Loading branch information
romani committed Feb 1, 2015
1 parent c22f9f0 commit 209b228
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 207 deletions.
Expand Up @@ -106,66 +106,14 @@
* </module>
* </pre>
*
* <p>
* Example of declarations with multiple empty lines between class members (allowed by default):
* </p>
*
* <pre>
* ///////////////////////////////////////////////////
* //HEADER
* ///////////////////////////////////////////////////
*
*
* package com.puppycrawl.tools.checkstyle.whitespace;
*
*
*
* import java.io.Serializable;
*
*
* class Foo
* {
* public static final int FOO_CONST = 1;
*
*
*
* public void foo() {}
* }
* </pre>
* <p>
* An example how to disallow multiple empty lines between class members:
* </p>
* <pre>
* &lt;module name="EmptyLineSeparator"&gt;
* &lt;property name="allowMultipleEmptyLines" value="false"/&gt;
* &lt;/module&gt;
* </pre>
*
* @author maxvetrenko
* @author <a href="mailto:nesterenko-aleksey@list.ru">Aleksey Nesterenko</a>
*
*/
public class EmptyLineSeparatorCheck extends Check
{

/**
* A key is pointing to the warning message empty.line.separator in "messages.properties"
* file.
*/
public static final String MSG_SHOULD_BE_SEPARATED = "empty.line.separator";

/**
* A key is pointing to the warning message empty.line.separator.multiple.lines
* in "messages.properties"
* file.
*/
public static final String MSG_MULTIPLE_LINES = "empty.line.separator.multiple.lines";

/** */
private boolean allowNoEmptyLineBetweenFields;

/** Allows multiple empty lines between class members. */
private boolean allowMultipleEmptyLines = true;

/**
* Allow no empty line between fields.
* @param allow
Expand All @@ -176,15 +124,6 @@ public final void setAllowNoEmptyLineBetweenFields(boolean allow)
allowNoEmptyLineBetweenFields = allow;
}

/**
* Allow multiple empty lines between class members.
* @param allow User's value.
*/
public void setAllowMultipleEmptyLines(boolean allow)
{
allowMultipleEmptyLines = allow;
}

@Override
public int[] getDefaultTokens()
{
Expand All @@ -207,71 +146,43 @@ public void visitToken(DetailAST ast)
{
final DetailAST nextToken = ast.getNextSibling();

if (nextToken != null) {
if (nextToken != null && nextToken.getType() != TokenTypes.RCURLY) {
final int astType = ast.getType();
switch (astType) {
case TokenTypes.VARIABLE_DEF:
if (isTypeField(ast) && !hasEmptyLineAfter(ast)) {
if (allowNoEmptyLineBetweenFields
&& nextToken.getType() != TokenTypes.VARIABLE_DEF)
{
log(nextToken.getLineNo(), MSG_SHOULD_BE_SEPARATED,
log(nextToken.getLineNo(), "empty.line.separator",
nextToken.getText());
}
else if (!allowNoEmptyLineBetweenFields || !allowMultipleEmptyLines) {
log(nextToken.getLineNo(), MSG_SHOULD_BE_SEPARATED,
else if (!allowNoEmptyLineBetweenFields) {
log(nextToken.getLineNo(), "empty.line.separator",
nextToken.getText());
}
}
if (!allowMultipleEmptyLines && isTypeField(ast)
&& isPrePreviousLineEmpty(ast))
{
log(ast.getLineNo(), MSG_MULTIPLE_LINES, ast.getText());
}
break;
case TokenTypes.IMPORT:
if (astType != nextToken.getType() && !hasEmptyLineAfter(ast)
|| (ast.getLineNo() > 1 && !hasEmptyLineBefore(ast)
&& ast.getPreviousSibling() == null))
{
log(nextToken.getLineNo(), MSG_SHOULD_BE_SEPARATED, nextToken.getText());
}
if (!allowMultipleEmptyLines && isPrePreviousLineEmpty(ast)) {
log(ast.getLineNo(), MSG_MULTIPLE_LINES, ast.getText());
log(nextToken.getLineNo(), "empty.line.separator", nextToken.getText());
}
break;
case TokenTypes.PACKAGE_DEF:
if (ast.getLineNo() > 1 && !hasEmptyLineBefore(ast)) {
log(ast.getLineNo(), MSG_SHOULD_BE_SEPARATED, ast.getText());
}
if (!allowMultipleEmptyLines && isPrePreviousLineEmpty(ast)) {
log(ast.getLineNo(), MSG_MULTIPLE_LINES, ast.getText());
log(ast.getLineNo(), "empty.line.separator", ast.getText());
}
default:
if (nextToken.getType() != TokenTypes.RCURLY && !hasEmptyLineAfter(ast)) {
log(nextToken.getLineNo(), MSG_SHOULD_BE_SEPARATED, nextToken.getText());
}
if (!allowMultipleEmptyLines && isPrePreviousLineEmpty(ast)) {
log(ast.getLineNo(), MSG_MULTIPLE_LINES, ast.getText());
if (!hasEmptyLineAfter(ast)) {
log(nextToken.getLineNo(), "empty.line.separator", nextToken.getText());
}
}
}
}

/**
* Checks if a token has empty pre-previous line.
* @param token DetailAST token.
* @return true, if token has empty lines before.
*/
private boolean isPrePreviousLineEmpty(DetailAST token)
{
final int lineNo = token.getLineNo();
// 3 is the number of the pre-previous line because the numbering starts from zero.
final int number = 3;
final String prePreviousLine = getLines()[lineNo - number];
return prePreviousLine.trim().isEmpty();
}

/**
* Checks if token have empty line after.
* @param token token.
Expand Down
@@ -1,5 +1,4 @@
empty.line.separator=''{0}'' should be separated from previous statement.
empty.line.separator.multiple.lines=''{0}'' has more than 1 empty lines before.

containsTab=Line contains a tab character.
file.containsTab=File contains tab characters (this is the first instance).
Expand Down
Expand Up @@ -66,19 +66,4 @@ public void testHeader() throws Exception
};
verify(checkConfig, getPath("whitespace/InputEmptyLineSeparatorCheckHeader.java"), expected);
}

@Test
public void testMultipleEmptyLinesBetweenClassMembers() throws Exception
{
DefaultConfiguration checkConfig = createCheckConfig(EmptyLineSeparatorCheck.class);
checkConfig.addAttribute("allowMultipleEmptyLines", "false");
final String[] expected = {
"21: 'package' has more than 1 empty lines before.",
"24: 'import' has more than 1 empty lines before.",
"33: 'VARIABLE_DEF' has more than 1 empty lines before.",
"38: 'VARIABLE_DEF' has more than 1 empty lines before.",
"43: 'METHOD_DEF' has more than 1 empty lines before.",
};
verify(checkConfig, getPath("whitespace/InputEmptyLineSeparatorCheckMultipleEmptyLines.java"), expected);
}
}

This file was deleted.

46 changes: 0 additions & 46 deletions src/xdocs/config_whitespace.xml
Expand Up @@ -1351,12 +1351,6 @@ import com.puppycrawl.tools.checkstyle.api.Check;
<th><a href="property_types.html#boolean">boolean</a></th>
<th>false</th>
</tr>
<tr>
<th>allowMultipleEmptyLines</th>
<th>Allow multiple empty lines between class members</th>
<th><a href="property_types.html#boolean">boolean</a></th>
<th><code>true</code></th>
</tr>
</table>
</subsection>

Expand Down Expand Up @@ -1417,46 +1411,6 @@ class Foo
<source>
&lt;module name="EmptyLineSeparator"&gt;
&lt;property name="allowNoEmptyLineBetweenFields" value="true"/&gt;
&lt;/module&gt;
</source>
<p>
Example of declarations with multiple empty lines between class members
(allowed by default):
</p>
<source>
///////////////////////////////////////////////////
//HEADER
///////////////////////////////////////////////////



package com.puppycrawl.tools.checkstyle.whitespace;



import java.io.Serializable;



class Foo
{


public static final int FOO_CONST = 1;




public void foo() {} //should be separated from previous statement.
}
</source>

<p>
An example how to disallow multiple empty lines between class members:
</p>
<source>
&lt;module name=&quot;EmptyLineSeparator&quot;&gt;
&lt;property name=&quot;allowMultipleEmptyLines&quot; value=&quot;true&quot;/&gt;
&lt;/module&gt;
</source>
</subsection>
Expand Down

0 comments on commit 209b228

Please sign in to comment.