diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheck.java index 9f1d45c03..00c7e19ac 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheck.java @@ -127,6 +127,13 @@ public class IndentationCheck extends Check /** how far continuation line should be indented when line-wrapping is present */ private int mLineWrappingIndentation = DEFAULT_INDENTATION; + /** + * Force strict condition in line wrapping case. If value is true, line wrap indent + * have to be same as lineWrappingIndentation parameter, if value is false, line wrap indent + * have to be not less than mLineWrappingIndentation parameter. + */ + private boolean mForceStrictCondition; + /** handlers currently in use */ private final FastStack mHandlers = FastStack.newInstance(); @@ -139,6 +146,24 @@ public class IndentationCheck extends Check { } + /** + * Get forcing strict condition. + * @return mForceStrictCondition value. + */ + public boolean getForceStrictCondition() + { + return mForceStrictCondition; + } + + /** + * Set forcing strict condition. + * @param aValue user's value of mForceStrictCondition. + */ + public void setForceStrictCondition(boolean aValue) + { + mForceStrictCondition = aValue; + } + /** * Set the basic offset. * diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/LineWrappingHandler.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/LineWrappingHandler.java index f0629477c..d06875ae5 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/LineWrappingHandler.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/LineWrappingHandler.java @@ -59,6 +59,11 @@ public class LineWrappingHandler */ private int mIndentLevel; + /** + * Force strict condition in line wrapping case. + */ + private boolean mForceStrictCondition; + /** * Sets values of class field, finds last node and calculates indentation level. * @@ -73,6 +78,7 @@ public class LineWrappingHandler mFirstNode = aFirstNode; mLastNode = findLastNode(mFirstNode); mIndentLevel = mIndentCheck.getLineWrappingIndentation(); + mForceStrictCondition = mIndentCheck.getForceStrictCondition(); } /** @@ -301,10 +307,19 @@ public class LineWrappingHandler */ private void logWarningMessage(DetailAST aCurrentNode, int aCurrentIndent) { - if (aCurrentNode.getColumnNo() < aCurrentIndent) { - mIndentCheck.indentationLog(aCurrentNode.getLineNo(), - "indentation.error", aCurrentNode.getText(), - aCurrentNode.getColumnNo(), aCurrentIndent); + if (mForceStrictCondition) { + if (aCurrentNode.getColumnNo() != aCurrentIndent) { + mIndentCheck.indentationLog(aCurrentNode.getLineNo(), + "indentation.error", aCurrentNode.getText(), + aCurrentNode.getColumnNo(), aCurrentIndent); + } + } + else { + if (aCurrentNode.getColumnNo() < aCurrentIndent) { + mIndentCheck.indentationLog(aCurrentNode.getLineNo(), + "indentation.error", aCurrentNode.getText(), + aCurrentNode.getColumnNo(), aCurrentIndent); + } } } } 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 823b99483..712a20f1f 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 @@ -30,6 +30,21 @@ import org.junit.Test; public class IndentationCheckTest extends BaseCheckTestSupport { + @Test + public void forbidCStyle() throws Exception + { + final DefaultConfiguration checkConfig = createCheckConfig(IndentationCheck.class); + checkConfig.addAttribute("basicOffset", "4"); + checkConfig.addAttribute("lineWrappingIndentation", "8"); + checkConfig.addAttribute("throwsIndent", "8"); + checkConfig.addAttribute("forceStrictCondition", "true"); + final String[] expected = { + "5: 'int' have incorrect indentation level 29, expected level should be 12.", + "6: 'int' have incorrect indentation level 29, expected level should be 12.", + }; + verify(checkConfig, getPath("indentation/InputMethodCStyle.java"), expected); + } + @Test public void testZeroCaseLevel() throws Exception { diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/indentation/InputMethodCStyle.java b/src/test/resources/com/puppycrawl/tools/checkstyle/indentation/InputMethodCStyle.java new file mode 100644 index 000000000..f6cdb95be --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/indentation/InputMethodCStyle.java @@ -0,0 +1,13 @@ +package com.puppycrawl.tools.checkstyle.indentation; + +public class InputMethodCStyle { + public InputMethodCStyle(int appleCount, + int bananaCount, // warn + int pearsCount) { // warn + } + + public InputMethodCStyle(String appleCount, + int bananaCount, //ok + int pearsCount) { //ok + } +} \ No newline at end of file diff --git a/src/xdocs/config_misc.xml b/src/xdocs/config_misc.xml index 3ec30b19e..3590cd3ad 100755 --- a/src/xdocs/config_misc.xml +++ b/src/xdocs/config_misc.xml @@ -852,6 +852,13 @@ messages.properties: Key 'ok' missing. Integer 4 + + forceStrictCondition + force strict condition in line wrapping case. If value is true, line wrap indent + have to be same as lineWrappingIndentation parameter + Boolean + false + @@ -873,6 +880,28 @@ messages.properties: Key 'ok' missing. <property name="caseIndent" value="0"/> </module> +

+ To configure the Check to enforce strict condition in line-wrapping validation. +

+ +<module name="Indentation"> + <property name="forceStrictCondition" value="true"/> +</module> + +

+ Such config doesn't allow next cases: +

+ +void foo(String aFooString, + int aFooInt) {} // indent:8 ; exp: 4; warn, because 8 != 4 + +

+ But if forceStrictCondition = false, this code is valid: +

+ +void foo(String aFooString, + int aFooInt) {} // indent:8 ; exp: > 4; ok, because 8 > 4 +