Indentation check fails for switch statements #341

This commit is contained in:
maxvetrenko 2014-11-14 10:44:46 +03:00 committed by Roman Ivanov
parent bae7fdded6
commit 82c3f19cdc
3 changed files with 47 additions and 1 deletions

View File

@ -125,9 +125,20 @@ public class SlistHandler extends BlockParentHandler
{
// only need to check this if parent is not
// an if, else, while, do, ctor, method
if (hasBlockParent()) {
if (hasBlockParent() || isSameLineCaseGroup()) {
return;
}
super.checkIndentation();
}
/**
* Checks if SLIST node is placed at the same line as CASE_GROUP node.
* @return true, if SLIST node is places at the same line as CASE_GROUP node.
*/
private boolean isSameLineCaseGroup()
{
final DetailAST parentNode = getMainAst().getParent();
return parentNode.getType() == TokenTypes.CASE_GROUP
&& getMainAst().getLineNo() == parentNode.getLineNo();
}
}

View File

@ -866,4 +866,17 @@ public class IndentationCheckTest extends BaseCheckTestSupport
verify(checkConfig, getPath("indentation/InputValidTryResourcesIndent.java"),
expected);
}
@Test
public void testSwitchCustom() throws Exception
{
final DefaultConfiguration checkConfig =
createCheckConfig(IndentationCheck.class);
checkConfig.addAttribute("basicOffset", "4");
checkConfig.addAttribute("throwsIndent", "8");
checkConfig.addAttribute("lineWrappingIndentation", "8");
final String[] expected = {};
verify(checkConfig, getPath("indentation/InputSwitchCustom.java"),
expected);
}
}

View File

@ -0,0 +1,22 @@
public class MyClass {
public int getValue(int value) {
switch (value) {
case 0: return ABC1;
case 1: return ABC2;
case 2: return ABC3;
}
return 0;
}
public int getValue1(int value) {
switch (value) {
case 0:
return ABC1;
case 1:
return ABC2;
case 2:
return ABC3;
}
return 0;
}
}