Skip to content

Commit

Permalink
SeparatorWrapCheck #10
Browse files Browse the repository at this point in the history
  • Loading branch information
maxvetrenko committed Jun 30, 2014
1 parent c3e98e5 commit 68e4af5
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 0 deletions.
@@ -0,0 +1,88 @@
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2014 Oliver Burn
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////

package com.puppycrawl.tools.checkstyle.checks.whitespace;

import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
/**
* <p>
* Checks line wrapping for separators.
* </p>
* <p> By default the check will check the following operators:
* {@link TokenTypes#TYPE_EXTENSION_AND},
* {@link TokenTypes#DOT},
* {@link TokenTypes#COMMA},
* </p>
* <p>
* An example of how to configure the check is:
* </p>
* <pre>
* &lt;module name="SeparatorWrap"/&gt;
* </pre>
* <p>
* Good wrapping example:
* </p>
* <pre><code>s
* .isEmpty();
* </code></pre>
* <p>
* Bad wrapping example:
* </p>
* <pre><code>s.
* isEmpty();
* </code></pre>
*
* @author maxvetrenko
*/
public class SeparatorWrapCheck extends Check
{

@Override
public int[] getDefaultTokens()
{
return new int[] {
TokenTypes.TYPE_EXTENSION_AND,
TokenTypes.DOT,
TokenTypes.COMMA,
};
}

@Override
public void visitToken(DetailAST aAST)
{
final String text = aAST.getText();
final int colNo = aAST.getColumnNo();
final int lineNo = aAST.getLineNo();
final String currentLine = getLines()[lineNo - 1];

if (aAST.getType() == TokenTypes.COMMA) {
if ((currentLine.substring(0, colNo).trim().length() == 0)) {
log(lineNo, colNo, "line.previous", text);
}
}

else if (currentLine.substring(colNo + text.length())
.trim().length() == 0)
{
log(lineNo, colNo, "line.new", text);
}
}
}
@@ -0,0 +1,48 @@
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2014 Oliver Burn
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.checks.whitespace;

import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import org.junit.Before;
import org.junit.Test;

public class SeparatorWrapCheckTest
extends BaseCheckTestSupport
{
private DefaultConfiguration checkConfig;

@Before
public void setUp()
{
checkConfig = createCheckConfig(SeparatorWrapCheck.class);
}

@Test
public void testDefault()
throws Exception
{
final String[] expected = {
"25:29: '&' should be on a new line.",
"30:10: '.' should be on a new line.",
"38:25: ',' should be on the previous line.",
};
verify(checkConfig, getPath("whitespace/InputSeparatorWrap.java"), expected);
}
}
@@ -0,0 +1,47 @@
package com.puppycrawl.tools.checkstyle.whitespace;

public class InputSeparatorWrap<T extends Foo
& Bar> { //good wrapping
public void goodCase()
{
int i = 0;
String s = "ffffooooString";
s
.isEmpty(); //good wrapping
s.isEmpty();
try {

} catch (FooException |
BarException e) {}
foo(i,
s); //good wrapping
}
public static void foo(int i, String s)
{

}
}

class badCase<T extends Foo &
Bar> { //bad wrapping
public void goodCase()
{
String s = "ffffooooString";
s.
isEmpty(); //bad wrapping
try {

} catch (FooException
| BarException e) {}

foo(i
,s); //bad wrapping
}
public static void foo(int i, String s)
{
return new StringBuilder(maxLength)
.append(seq, 0, truncationLength)
.append(truncationIndicator)
.toString();
}
}
5 changes: 5 additions & 0 deletions src/xdocs/availablechecks.xml
Expand Up @@ -578,6 +578,11 @@
<td>
Checks the placement of right curly braces.</td>
</tr>
<tr>
<td><a href="config_coding.html#SeparatorWrap">SeparatorWrap</a></td>
<td>
Checks line wrapping for separators.</td>
</tr>
<tr>
<td><a href="config_coding.html#SimplifyBooleanExpression">SimplifyBooleanExpression</a></td>
<td>
Expand Down
46 changes: 46 additions & 0 deletions src/xdocs/config_whitespace.xml
Expand Up @@ -1171,5 +1171,51 @@ public void func() {} // empty method</source>
</p>
</subsection>
</section>

<section name="SeparatorWrap">
<subsection name="Description">
<p>
Checks line wrapping for separators.
</p>

<p>
For example the following wrapping is legal:
</p>

<source>
s
.isEmpty();
</source>

<p>
But the following wrapping example is not:
</p>
<source>
s.
isEmpty();
</source>
</subsection>

<subsection name="Examples">
<p>
To configure the check:
</p>
<source>
&lt;module name=&quot;SeparatorWrap&quot;/&gt;
</source>
</subsection>

<subsection name="Package">
<p>
com.puppycrawl.tools.checkstyle.checks.whitespace
</p>
</subsection>

<subsection name="Parent Module">
<p>
<a href="config.html#TreeWalker">TreeWalker</a>
</p>
</subsection>
</section>
</body>
</document>

0 comments on commit 68e4af5

Please sign in to comment.