Issue #2433: Exclude lines with imports in LineLength check

This commit is contained in:
Michal Kordas 2015-10-24 23:28:58 +02:00 committed by Roman Ivanov
parent 617178cb1f
commit ff45368856
5 changed files with 29 additions and 10 deletions

View File

@ -53,9 +53,6 @@
<!-- Suppressions to remove over time -->
<suppress checks="." files=".*[\\/]src[\\/]it[\\/]" lines="28-999"/>
<!-- Suppressions for long import statics -->
<suppress checks="LineLength" files=".*[\\/]src[\\/]test[\\/]" lines="22-29"/>
<!--
Turn off all checks for Generated and Test code. Fixes issues with using
Eclipse plug-in.

View File

@ -37,9 +37,7 @@ import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
* </p>
*
* <p>
* Note: Support for the special handling of imports in CheckStyle Version 2
* has been dropped as it is a special case of regexp: The user can set
* the ignorePattern to "^import" and achieve the same effect.
* Import statements (lines matching pattern {@code ^import .*}) are not verified by this check.
* </p>
* <p>
* The default maximum allowable line length is 80 characters. To change the
@ -86,6 +84,9 @@ public class LineLengthCheck extends Check {
/** Default maximum number of columns in a line. */
private static final int DEFAULT_MAX_COLUMNS = 80;
/** Pattern matching import and import static statements. */
private static final Pattern IMPORT_PATTERN = Pattern.compile("^import .*");
/** The maximum number of columns in a line. */
private int max = DEFAULT_MAX_COLUMNS;
@ -123,7 +124,7 @@ public class LineLengthCheck extends Check {
final int realLength = CommonUtils.lengthExpandedTabs(
line, line.length(), getTabWidth());
if (realLength > max
if (realLength > max && !IMPORT_PATTERN.matcher(line).find()
&& !ignorePattern.matcher(line).find()) {
log(i + 1, MSG_KEY, max, realLength);
}

View File

@ -78,4 +78,15 @@ public class LineLengthCheckTest extends BaseCheckTestSupport {
};
verify(checkConfig, getPath("InputSimple.java"), expected);
}
@Test
public void shouldNotLogLongImportStatements() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(LineLengthCheck.class);
checkConfig.addAttribute("max", "80");
final String[] expected = {
"9: " + getCheckMessage(MSG_KEY, 80, 87),
};
verify(checkConfig, getPath("InputLongImportStatements.java"), expected);
}
}

View File

@ -0,0 +1,11 @@
package com.puppycrawl.tools.checkstyle.checks.sizes;
import com.puppycrawl.tools.checkstyle.grammars.comments.InputFullOfSinglelineComments;
import static com.puppycrawl.tools.checkstyle.grammars.comments.InputFullOfSinglelineComments.main;
public class InputLongImportStatements {
@Override
public String toString() {
return "This is very long line that should be logged because it is not import";
}
}

View File

@ -245,9 +245,8 @@
property <code>tabWidth</code> for <code>LineLength</code> alone.
</li>
<li>
Support for the special handling of imports in CheckStyle Version
2 has been dropped as it is a special case of regexp: The user can
set property <code>ignorePattern</code> to <code>^import</code> and achieve the same effect.
Import statements (lines matching pattern <code>^import .*</code>) are not verified by
this check.
</li>
</ul>
</subsection>