diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/CheckStyleTask.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/CheckStyleTask.java index fa9a28c66..0813dcef2 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/CheckStyleTask.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/CheckStyleTask.java @@ -573,6 +573,18 @@ public class CheckStyleTask }); } + /** @param aIgnore whether to ignore operator wrapping **/ + public void setIgnoreOpWrap(final boolean aIgnore) + { + mOptionMemory.add(new Runnable() + { + public void run() + { + mConfig.setIgnoreOpWrap(aIgnore); + } + }); + } + /** @param aIgnore whether to ignore braces **/ public void setIgnoreBraces(final boolean aIgnore) { diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/Configuration.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/Configuration.java index 377baa50d..97d8a857a 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Configuration.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/Configuration.java @@ -162,6 +162,8 @@ public class Configuration private boolean mIgnoreWhitespace = false; /** whether to ignore cast whitespace **/ private boolean mIgnoreCastWhitespace = false; + /** whether to ignore op wrapping **/ + private boolean mIgnoreOpWrap = false; /** whether to ignore braces **/ private boolean mIgnoreBraces = false; /** whether to ignore 'public' keyword in interface definitions **/ @@ -262,6 +264,9 @@ public class Configuration setIgnoreCastWhitespace(getBooleanProperty(aProps, IGNORE_CAST_WHITESPACE_PROP, mIgnoreCastWhitespace)); + setIgnoreOpWrap(getBooleanProperty(aProps, + IGNORE_OP_WRAP_PROP, + mIgnoreOpWrap)); setIgnoreBraces(getBooleanProperty(aProps, IGNORE_BRACES_PROP, mIgnoreBraces)); @@ -583,6 +588,12 @@ public class Configuration return mIgnoreCastWhitespace; } + /** @return whether to ignore checks for operator wrapping **/ + public boolean isIgnoreOpWrap() + { + return mIgnoreOpWrap; + } + /** @return whether to ignore checks for braces **/ public boolean isIgnoreBraces() { @@ -900,6 +911,14 @@ public class Configuration mIgnoreCastWhitespace = aTo; } + /** + * @param aTo whether to ignore operator wrapping + */ + public void setIgnoreOpWrap(boolean aTo) + { + mIgnoreOpWrap = aTo; + } + /** * @param aTo whether to ignore checks for braces */ diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/Defn.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/Defn.java index 0a1dfb687..00c970ce7 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Defn.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/Defn.java @@ -84,6 +84,8 @@ public interface Defn String IGNORE_CAST_WHITESPACE_PROP = "checkstyle.ignore.whitespace.cast"; /** property name for ignoring braces **/ String IGNORE_BRACES_PROP = "checkstyle.ignore.braces"; + /** property name for ignoring wrapping lines on operators **/ + String IGNORE_OP_WRAP_PROP = "checkstyle.ignore.opwrap"; /** property name for ignoring 'public' in interface definitions **/ String IGNORE_PUBLIC_IN_INTERFACE_PROP = "checkstyle.ignore.public.in.interface"; diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java index 0a565797e..8a3701613 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java @@ -965,22 +965,18 @@ class Verifier */ void verifyOpBegin(int aLineNo, int aColNo, String aText) { - if (mConfig.isIgnoreWhitespace()) { - return; - } - verifyWSAroundBegin(aLineNo, aColNo, aText); // Check if rest of line is whitespace, and not just the operator by // itself. This last bit is to handle the example of the check below.:-) -// if (!aText.equals(mLines[aLineNo - 1].trim()) -// && -// (mLines[aLineNo - 1].substring(aColNo + aText.length() - 1) -// .trim().length() == 0)) -// { -// log(aLineNo, aColNo - 1, -// "'" + aText + "' should be on a new line."); -// } + if (!mConfig.isIgnoreOpWrap() + && !aText.equals(mLines[aLineNo - 1].trim()) + && (mLines[aLineNo - 1].substring(aColNo + aText.length() - 1) + .trim().length() == 0)) + { + log(aLineNo, aColNo - 1, + "'" + aText + "' should be on a new line."); + } } diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/java.g b/src/checkstyle/com/puppycrawl/tools/checkstyle/java.g index 3085b6213..11e6ce60a 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/java.g +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/java.g @@ -903,14 +903,14 @@ shiftExpression // binary addition/subtraction (level 3) additiveExpression : multiplicativeExpression - ((p:PLUS^ {ver.verifyOpBegin(p.getLine(), p.getColumn(), p.getText());}| m:MINUS^ {ver.verifyWSAroundBegin(m.getLine(), m.getColumn(), m.getText());} ) + ((p:PLUS^ {ver.verifyOpBegin(p.getLine(), p.getColumn(), p.getText());}| m:MINUS^ {ver.verifyOpBegin(m.getLine(), m.getColumn(), m.getText());} ) multiplicativeExpression)* ; // multiplication/division/modulo (level 2) multiplicativeExpression - : unaryExpression ((s:STAR^ {ver.verifyWSAroundBegin(s.getLine(), s.getColumn(), s.getText());} | DIV^ | MOD^ ) unaryExpression)* + : unaryExpression ((s:STAR^ {ver.verifyOpBegin(s.getLine(), s.getColumn(), s.getText());} | DIV^ | MOD^ ) unaryExpression)* ; unaryExpression diff --git a/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java b/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java index 6382af5e3..72c7332ed 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java @@ -809,4 +809,33 @@ public class CheckerTest verify(c, filepath, expected); } + public void testOpWrapOn() + throws Exception + { + mConfig.setJavadocScope(Scope.NOTHING); + mConfig.setIgnoreOpWrap(false); + final Checker c = createChecker(); + final String filepath = getPath("InputOpWrap.java"); + assertNotNull(c); + final String[] expected = { + filepath + ":15:19: '+' should be on a new line.", + filepath + ":16:15: '-' should be on a new line.", + filepath + ":24:18: '&&' should be on a new line.", + }; + verify(c, filepath, expected); + } + + public void testOpWrapOff() + throws Exception + { + mConfig.setJavadocScope(Scope.NOTHING); + mConfig.setIgnoreOpWrap(true); + final Checker c = createChecker(); + final String filepath = getPath("InputOpWrap.java"); + assertNotNull(c); + final String[] expected = { + }; + verify(c, filepath, expected); + } + }