From 82c3f19cdcb29b5936be2fe186b4cf9bee372bc4 Mon Sep 17 00:00:00 2001 From: maxvetrenko Date: Fri, 14 Nov 2014 10:44:46 +0300 Subject: [PATCH] Indentation check fails for switch statements #341 --- .../checks/indentation/SlistHandler.java | 13 ++++++++++- .../indentation/IndentationCheckTest.java | 13 +++++++++++ .../indentation/InputSwitchCustom.java | 22 +++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/test/resources/com/puppycrawl/tools/checkstyle/indentation/InputSwitchCustom.java diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/SlistHandler.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/SlistHandler.java index d13755d5d..70a844d4f 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/SlistHandler.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/SlistHandler.java @@ -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(); + } } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java index cfea2efee..7cbf3209e 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java @@ -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); + } } diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/indentation/InputSwitchCustom.java b/src/test/resources/com/puppycrawl/tools/checkstyle/indentation/InputSwitchCustom.java new file mode 100644 index 000000000..683eab9de --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/indentation/InputSwitchCustom.java @@ -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; + } +} \ No newline at end of file