Skip to content

Commit

Permalink
Issue checkstyle#2618: refactored indentation import and package logic
Browse files Browse the repository at this point in the history
  • Loading branch information
rnveach committed May 28, 2016
1 parent 0bd2cb6 commit 53c887f
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 51 deletions.
Expand Up @@ -236,9 +236,9 @@ protected final int getLineStart(int lineNo) {
*
* @return the start of the specified line
*/
protected final int getLineStart(String line) {
private int getLineStart(String line) {
int index = 0;
while (index < line.length() && Character.isWhitespace(line.charAt(index))) {
while (Character.isWhitespace(line.charAt(index))) {
index++;
}
return CommonUtils.lengthExpandedTabs(
Expand All @@ -255,27 +255,6 @@ protected boolean shouldIncreaseIndent() {
return true;
}

/**
* Check the indentation of consecutive lines for the expression we are
* handling.
*
* @param startLine the first line to check
* @param endLine the last line to check
* @param indentLevel the required indent level
*/
protected final void checkLinesIndent(int startLine, int endLine,
IndentLevel indentLevel) {
// check first line
checkLineIndent(startLine, indentLevel);

// check following lines
final IndentLevel offsetLevel =
new IndentLevel(indentLevel, getBasicOffset());
for (int i = startLine + 1; i <= endLine; i++) {
checkLineIndent(i, offsetLevel);
}
}

/**
* Check the indentation for a set of lines.
*
Expand Down Expand Up @@ -328,22 +307,6 @@ private void checkLinesIndent(LineSet lines,
}
}

/**
* Check the indent level for a single line.
*
* @param lineNum the line number to check
* @param indentLevel the required indent level
*/
private void checkLineIndent(int lineNum, IndentLevel indentLevel) {
final String line = indentCheck.getLine(lineNum - 1);
if (!line.isEmpty()) {
final int start = getLineStart(line);
if (indentLevel.isGreaterThan(start)) {
logChildError(lineNum, start, indentLevel);
}
}
}

/**
* Check the indentation for a single line.
*
Expand Down
Expand Up @@ -43,12 +43,14 @@ public ImportHandler(IndentationCheck indentCheck,

@Override
public void checkIndentation() {
final int lineStart = getMainAst().getLineNo();
final DetailAST semi = getMainAst().findFirstToken(TokenTypes.SEMI);
final int lineEnd = semi.getLineNo();
final int columnNo = expandedTabsColumnNo(getMainAst());

if (getMainAst().getLineNo() != lineEnd) {
checkLinesIndent(lineStart, lineEnd, getIndent());
if (!getIndent().isAcceptable(columnNo) && isOnStartOfLine(getMainAst())) {
logError(getMainAst(), "", columnNo);
}

final DetailAST semi = getMainAst().findFirstToken(TokenTypes.SEMI);

checkWrappingIndentation(getMainAst(), semi);
}
}
Expand Up @@ -201,7 +201,8 @@ private void checkAnnotationIndentation(DetailAST atNode,
&& node.equals(lastAnnotationNode);
if (isCurrentNodeCloseAnnotationAloneInLine
|| node.getType() == TokenTypes.AT
&& parentNode.getParent().getType() == TokenTypes.MODIFIERS) {
&& (parentNode.getParent().getType() == TokenTypes.MODIFIERS
|| parentNode.getParent().getType() == TokenTypes.ANNOTATIONS)) {
logWarningMessage(node, firstNodeIndent);
}
else {
Expand Down
Expand Up @@ -44,12 +44,13 @@ public PackageDefHandler(IndentationCheck indentCheck,
@Override
public void checkIndentation() {
final int columnNo = expandedTabsColumnNo(getMainAst());
if (!getIndent().isAcceptable(columnNo)) {

if (!getIndent().isAcceptable(columnNo) && isOnStartOfLine(getMainAst())) {
logError(getMainAst(), "", columnNo);
}

checkLinesIndent(getMainAst().getLineNo(),
getMainAst().findFirstToken(TokenTypes.SEMI).getLineNo(),
getIndent());
final DetailAST semi = getMainAst().findFirstToken(TokenTypes.SEMI);

checkWrappingIndentation(getMainAst(), semi);
}
}
Expand Up @@ -1467,7 +1467,8 @@ public void testInvalidImportIndent() throws Exception {
checkConfig.addAttribute("basicOffset", "8");
checkConfig.addAttribute("tabWidth", "4");
final String[] expected = {
"4: " + getCheckMessage(MSG_CHILD_ERROR, "import", 2, 8),
"4: " + getCheckMessage(MSG_ERROR, ".", 2, 4),
"5: " + getCheckMessage(MSG_ERROR, "import", 1, 0),
};
verifyWarns(checkConfig, getPath("InputInvalidImportIndent.java"), expected);
}
Expand Down Expand Up @@ -1607,6 +1608,24 @@ public void testPackageDeclaration() throws Exception {
verifyWarns(checkConfig, getPath("InputPackageDeclaration.java"), expected);
}

@Test
public void testPackageDeclaration2() throws Exception {
final DefaultConfiguration checkConfig = createCheckConfig(IndentationCheck.class);
checkConfig.addAttribute("tabWidth", "4");
final String[] expected = {
"2: " + getCheckMessage(MSG_ERROR, "package def", 1, 0),
};
verifyWarns(checkConfig, getNonCompilablePath("InputPackageDeclaration2.java"), expected);
}

@Test
public void testPackageDeclaration3() throws Exception {
final DefaultConfiguration checkConfig = createCheckConfig(IndentationCheck.class);
checkConfig.addAttribute("tabWidth", "4");
final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
verifyWarns(checkConfig, getPath("InputPackageDeclaration3.java"), expected);
}

@Test
public void testLambda1() throws Exception {
final DefaultConfiguration checkConfig = createCheckConfig(IndentationCheck.class);
Expand Down
@@ -0,0 +1,4 @@
@Deprecated //indent:1 exp:1
package com.puppycrawl.tools.checkstyle.checks.indentation;//indent:1 exp:0 warn

public class InputPackageDeclaration2 {}//indent:0 exp:0
@@ -1,7 +1,10 @@
package com.puppycrawl.tools.checkstyle.checks.indentation; //indent:0 exp:0

import java.util //indent:0 exp:0
.RandomAccess; //indent:2 exp:8 warn
.RandomAccess; import java.util.RandomAccess; //indent:2 exp:4 warn
import java.util.RandomAccess; //indent:1 exp:0 warn
import java.util //indent:0 exp:0
.RandomAccess; //indent:19 exp:>=8

/** //indent:0 exp:0
* This test-input is intended to be checked using following configuration: //indent:1 exp:1
Expand Down
@@ -0,0 +1,3 @@
/** Comment */ package com.puppycrawl.tools.checkstyle.checks.indentation;//indent:0 exp:>=0

public class InputPackageDeclaration3 {}//indent:0 exp:0

0 comments on commit 53c887f

Please sign in to comment.