Skip to content

Commit

Permalink
NoLineWrap #20
Browse files Browse the repository at this point in the history
  • Loading branch information
maxvetrenko committed Jul 3, 2014
1 parent bf8ebef commit f57617f
Show file tree
Hide file tree
Showing 8 changed files with 299 additions and 0 deletions.
@@ -0,0 +1,97 @@
////////////////////////////////////////////////////////////////////////////////
// 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 that chosen statements are not line-wrapped.
* By default this Check restricts wrapping import and package statements,
* but it's possible to check any statement.
* </p>
* <h4>Examples</h4>
* <p class="body">
*
* Examples of line-wrapped statements (bad case):
* <pre><code> package com.puppycrawl.
* tools.checkstyle.checks;
*
* import com.puppycrawl.tools.
* checkstyle.api.Check;
* </code></pre>
*
* <p>
* To configure the check to force no line-wrapping
* in package and import statements (default values):
* </p>
* <pre class="body">
* &lt;module name=&quot;NoLineWrap&quot;/&gt;
* </pre>
*
* <p>
* To configure the check to force no line-wrapping only
* in import statements:
* </p>
* <pre class="body">
* &lt;module name=&quot;NoLineWrap&quot;&gt;
* &lt;property name="tokens" value="IMPORT"&gt;
* &lt;/module&gt;
* </pre>
*
* Examples of not line-wrapped statements (good case):
* <pre><code> package com.puppycrawl.tools.checkstyle.checks;
*
* import com.puppycrawl.tools.checkstyle.api.Check;
* </code></pre>
*
* @author maxvetrenko
*/
public class NoLineWrapCheck extends Check
{

@Override
public int[] getDefaultTokens()
{
return new int[] {TokenTypes.PACKAGE_DEF, TokenTypes.IMPORT};
}

@Override
public int[] getAcceptableTokens()
{
return new int[] {
TokenTypes.IMPORT,
TokenTypes.PACKAGE_DEF,
TokenTypes.CLASS_DEF,
TokenTypes.METHOD_DEF,
TokenTypes.CTOR_DEF,
TokenTypes.ENUM_DEF,
TokenTypes.INTERFACE_DEF,
};
}

@Override
public void visitToken(DetailAST aAST)
{
if (aAST.getLineNo() != aAST.getLastChild().getLineNo()) {
log(aAST.getLineNo(), "no.line.wrap", aAST.getText());
}
}
}
Expand Up @@ -30,3 +30,4 @@ type.file.mismatch=The name of the outer type and the file do not match.

properties.duplicateproperty=Duplicated property ''{0}'' ({1} occurrence(s)).
unable.open.cause=Unable to open ''{0}'': {1}.

Expand Up @@ -6,6 +6,8 @@ line.new=''{0}'' should be on a new line.
line.previous=''{0}'' should be on the previous line.
line.same=''{0}'' should be on the same line.

no.line.wrap={0} statement should not be line-wrapped.

ws.followed=''{0}'' is followed by whitespace.
ws.notFollowed=''{0}'' is not followed by whitespace.
ws.notPreceded=''{0}'' is not preceded with whitespace.
Expand Down
@@ -0,0 +1,61 @@
////////////////////////////////////////////////////////////////////////////////
// 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.Test;

public class NoLineWrapCheckTest
extends BaseCheckTestSupport
{
@Test
public void testCaseWithoutLineWrapping() throws Exception
{
final DefaultConfiguration checkConfig = createCheckConfig(NoLineWrapCheck.class);
final String[] expected = {};
verify(checkConfig, getPath("whitespace/NoLineWrapGoodInput.java"), expected);
}

@Test
public void testDefaultTokensLineWrapping() throws Exception
{
final DefaultConfiguration checkConfig = createCheckConfig(NoLineWrapCheck.class);
final String[] expected = {
"1: package statement should not be line-wrapped.",
"6: import statement should not be line-wrapped.",
};
verify(checkConfig, getPath("whitespace/NoLineWrapBadInput.java"), expected);
}

@Test
public void testCustomTokensLineWrapping()
throws Exception
{
final DefaultConfiguration checkConfig = createCheckConfig(NoLineWrapCheck.class);
checkConfig.addAttribute("tokens", "IMPORT, CLASS_DEF, METHOD_DEF, ENUM_DEF");
final String[] expected = {
"6: import statement should not be line-wrapped.",
"10: CLASS_DEF statement should not be line-wrapped.",
"13: METHOD_DEF statement should not be line-wrapped.",
"20: ENUM_DEF statement should not be line-wrapped.",
};
verify(checkConfig, getPath("whitespace/NoLineWrapBadInput.java"), expected);
}
}
@@ -0,0 +1,27 @@
package com.puppycrawl.tools.
checkstyle;

import com.google.common.annotations.Beta;

import javax.accessibility.
AccessibleAttributeSequence;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

public class
BadLineWrapInput {

public void
fooMethod() {
final int
foo = 0;
}
}

enum
FooFoo {
}

interface
InterFoo {}


@@ -0,0 +1,13 @@
package com.puppycrawl.tools.checkstyle;

import com.google.common.annotations.Beta;

import javax.accessibility.AccessibleAttributeSequence;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

public class GoodLineWrapInput {

public void fooMethod() {
//
}
}
5 changes: 5 additions & 0 deletions src/xdocs/availablechecks.xml
Expand Up @@ -461,6 +461,11 @@
<td>Checks that no method having zero parameters is defined
using the name <em>finalize</em>.</td>
</tr>
<tr>
<td><a href="config_whitespace.html#NoLineWrap">NoLineWrap</a></td>
<td>
Checks that chosen statements are not line-wrapped.</td>
</tr>
<tr>
<td><a href="config_whitespace.html#NoWhitespaceAfter">NoWhitespaceAfter</a></td>
<td>
Expand Down
93 changes: 93 additions & 0 deletions src/xdocs/config_whitespace.xml
Expand Up @@ -1171,5 +1171,98 @@ public void func() {} // empty method</source>
</p>
</subsection>
</section>

<section name="NoLineWrap">
<subsection name="Description">
<p>
Checks that chosen statements are not line-wrapped. By default this
Check restricts wrapping import and package statements, but it's possible to check
any statement.
</p>
</subsection>

<subsection name="Properties">
<table>
<tr>
<th>name</th>
<th>description</th>
<th>type</th>
<th>default value</th>
</tr>
<tr>
<td>tokens</td>
<td>assignments to check</td>
<td>subset of tokens <a
href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#PACKAGE_DEF">PACKAGE_DEF</a>,
<a
href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#IMPORT">IMPORT</a>,
<a
href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF">CLASS_DEF</a>,
<a
href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF">ENUM_DEF</a>
<a
href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INTERFACE_DEF">INTERFACE_DEF</a>,
<a
href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CTOR_DEF">CTOR_DEF</a>,
<a
href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#METHOD_DEF">METHOD_DEF</a>,
</td>
<td><a
href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#PACKAGE_DEF">PACKAGE_DEF</a>,
<a
href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#IMPORT">IMPORT</a></td>
</tr>
</table>
</subsection>

<subsection name="Examples">
<p>
Examples of line-wrapped statements (bad case):
</p>
<source>
package com.puppycrawl.
tools.checkstyle.checks;

import com.puppycrawl.tools.
checkstyle.api.Check;
</source>
<p>
To configure the check to force no line-wrapping
in package and import statements (default values):
</p>
<source>
&lt;module name=&quot;NoLineWrap&quot;/&gt;
</source>
<p>
To configure the check to force no line-wrapping only
in import statements:
</p>
<source>
&lt;module name=&quot;NoLineWrap&quot;&gt;
&lt;property name="tokens" value="IMPORT"&gt;
&lt;/module&gt;
</source>
<p>
Examples of not line-wrapped statements (good case):
</p>
<source>
package com.puppycrawl.tools.checkstyle.checks;

import com.puppycrawl.tools.checkstyle.api.Check;
</source>
</subsection>

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

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

0 comments on commit f57617f

Please sign in to comment.