Added option to avoid C style. #354

This commit is contained in:
maxvetrenko 2015-01-06 00:28:04 +03:00 committed by Roman Ivanov
parent dc4e97d024
commit 6a9e1b3f93
5 changed files with 101 additions and 4 deletions

View File

@ -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<ExpressionHandler> 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.
*

View File

@ -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);
}
}
}
}

View File

@ -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
{

View File

@ -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
}
}

View File

@ -852,6 +852,13 @@ messages.properties: Key 'ok' missing.
<td><a href="property_types.html#integer">Integer</a></td>
<td>4</td>
</tr>
<tr>
<td>forceStrictCondition</td>
<td>force strict condition in line wrapping case. If value is true, line wrap indent
have to be same as lineWrappingIndentation parameter</td>
<td><a href="property_types.html#boolean">Boolean</a></td>
<td>false</td>
</tr>
</table>
</subsection>
@ -873,6 +880,28 @@ messages.properties: Key 'ok' missing.
&lt;property name=&quot;caseIndent&quot; value=&quot;0&quot;/&gt;
&lt;/module&gt;
</source>
<p>
To configure the Check to enforce strict condition in line-wrapping validation.
</p>
<source>
&lt;module name=&quot;Indentation&quot;&gt;
&lt;property name=&quot;forceStrictCondition&quot; value=&quot;true&quot;/&gt;
&lt;/module&gt;
</source>
<p>
Such config doesn't allow next cases:
</p>
<source>
void foo(String aFooString,
int aFooInt) {} // indent:8 ; exp: 4; warn, because 8 != 4
</source>
<p>
But if forceStrictCondition = false, this code is valid:
</p>
<source>
void foo(String aFooString,
int aFooInt) {} // indent:8 ; exp: > 4; ok, because 8 > 4
</source>
</subsection>
<subsection name="Package">