Skip to content

Commit

Permalink
NoLineWrap #20
Browse files Browse the repository at this point in the history
  • Loading branch information
maxvetrenko committed Jun 22, 2014
1 parent bf8ebef commit f2e9f4d
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 0 deletions.
@@ -0,0 +1,74 @@
////////////////////////////////////////////////////////////////////////////////
// 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
* restrict to wrap import and package statements, but it's possible to check
* any statement.
* </p>
* <h4>Examples</h4>
* <p class="body">
*
* To configure the check only for import statements:
*
* </p>
* <pre class="body">
* &lt;module name=&quot;LineWrap&quot;&gt;
* &lt;property name=&quot;tokens&quot; value=&quot;IMPORT&quot;&gt;
* &lt;/module&gt;
* </pre>
*
* Examples of line-wrapped import and package:
* <pre><code> package com.puppycrawl.
* tools.checkstyle.checks;
*
* import com.puppycrawl.tools.
* checkstyle.api.Check;
* </code></pre>
*
* Examples without line-wrapping import and package statements:
* <pre><code> package com.puppycrawl.tools.checkstyle.checks;
*
* import com.puppycrawl.tools.checkstyle.api.Check;
* </code></pre>
*
* @author Max Vetrenko
*/
public class NoLineWrapCheck extends Check
{

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

@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,57 @@
////////////////////////////////////////////////////////////////////////////////
// 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 testGoodCase() throws Exception
{
final DefaultConfiguration checkConfig = createCheckConfig(NoLineWrapCheck.class);
final String[] expected = {};
verify(checkConfig, getPath("whitespace/NoLineWrapGoodInput.java"), expected);
}

@Test
public void testBadCase() 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 testBadCaseWithTokenConfig() throws Exception
{
final DefaultConfiguration checkConfig = createCheckConfig(NoLineWrapCheck.class);
checkConfig.addAttribute("tokens", "IMPORT");
final String[] expected = {
"6: import statement should not be line-wrapped.",
};
verify(checkConfig, getPath("whitespace/NoLineWrapBadInput.java"), expected);
}
}
@@ -0,0 +1,15 @@
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() {
//
}
}
@@ -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
51 changes: 51 additions & 0 deletions src/xdocs/config_whitespace.xml
Expand Up @@ -1171,5 +1171,56 @@ 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
restrict to wrap import and package statements, but it's possible to check
any statement.
</p>
</subsection>

<subsection name="Examples">
<p>
To configure the check:
</p>
<source>
&lt;module name=&quot;LineWrap&quot;&gt;
&lt;property name=&quot;tokens&quot; value=&quot;IMPORT&quot;&gt;
&lt;/module&gt;
</source>
<p>
Examples of line-wrapped import and package:
</p>
<source>
package com.puppycrawl.
tools.checkstyle.checks;

import com.puppycrawl.tools.
checkstyle.api.Check;
</source>
<p>
Examples without line-wrapping import and package statements:
</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 f2e9f4d

Please sign in to comment.