Left curly update. Force line break #247
This commit is contained in:
parent
6f6179fb52
commit
a123763c92
|
|
@ -59,9 +59,18 @@ import com.puppycrawl.tools.checkstyle.checks.AbstractOptionCheck;
|
|||
* value="nlow"/> <property name="maxLineLength" value="120"/> <
|
||||
* /module>
|
||||
* </pre>
|
||||
* <p>
|
||||
* An example of how to configure the check to validate enum definitions:
|
||||
* </p>
|
||||
* <pre>
|
||||
* <module name="LeftCurly">
|
||||
* <property name="ignoreEnums" value="false"/>
|
||||
* </module>
|
||||
* </pre>
|
||||
*
|
||||
* @author Oliver Burn
|
||||
* @author lkuehne
|
||||
* @author maxvetrenko
|
||||
* @version 1.0
|
||||
*/
|
||||
public class LeftCurlyCheck
|
||||
|
|
@ -73,6 +82,9 @@ public class LeftCurlyCheck
|
|||
/** TODO: replace this ugly hack **/
|
||||
private int mMaxLineLength = DEFAULT_MAX_LINE_LENGTH;
|
||||
|
||||
/** If true, Check will ignore enums*/
|
||||
private boolean mIgnoreEnums = true;
|
||||
|
||||
/**
|
||||
* Creates a default instance and sets the policy to EOL.
|
||||
*/
|
||||
|
|
@ -270,6 +282,9 @@ public class LeftCurlyCheck
|
|||
log(aBrace.getLineNo(), aBrace.getColumnNo(),
|
||||
"line.previous", "{");
|
||||
}
|
||||
if (!hasLineBreakAfter(aBrace)) {
|
||||
log(aBrace.getLineNo(), aBrace.getColumnNo(), "line.break.after");
|
||||
}
|
||||
}
|
||||
else if (getAbstractOption() == LeftCurlyOption.NLOW) {
|
||||
if (aStartToken.getLineNo() == aBrace.getLineNo()) {
|
||||
|
|
@ -291,4 +306,33 @@ public class LeftCurlyCheck
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if left curly has line break after.
|
||||
* @param aLeftCurly
|
||||
* Left curly token.
|
||||
* @return
|
||||
* True, left curly has line break after.
|
||||
*/
|
||||
private boolean hasLineBreakAfter(DetailAST aLeftCurly)
|
||||
{
|
||||
DetailAST nextToken = null;
|
||||
if (aLeftCurly.getType() == TokenTypes.SLIST) {
|
||||
nextToken = aLeftCurly.getFirstChild();
|
||||
}
|
||||
else {
|
||||
if (aLeftCurly.getParent().getParent().getType() == TokenTypes.ENUM_DEF)
|
||||
{
|
||||
if (!mIgnoreEnums) {
|
||||
nextToken = aLeftCurly.getNextSibling();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (nextToken != null && nextToken.getType() != TokenTypes.RCURLY) {
|
||||
if (aLeftCurly.getLineNo() == nextToken.getLineNo()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,3 +9,6 @@ line.same=''{0}'' should be on the same line.
|
|||
|
||||
needBraces=''{0}'' construct must use '''{}'''s.
|
||||
|
||||
line.break.after='''{''' should have line break after.
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -134,6 +134,7 @@ public class LeftCurlyCheckTest extends BaseCheckTestSupport
|
|||
"63:9: '{' should be on the previous line.",
|
||||
"83:5: '{' should be on the previous line.",
|
||||
"89:5: '{' should be on the previous line.",
|
||||
"97:19: '{' should have line break after.",
|
||||
"106:1: '{' should be on the previous line.",
|
||||
"109:9: '{' should be on the previous line.",
|
||||
"118:1: '{' should be on the previous line.",
|
||||
|
|
@ -197,4 +198,29 @@ public class LeftCurlyCheckTest extends BaseCheckTestSupport
|
|||
};
|
||||
verify(mCheckConfig, getPath("InputLeftCurlyAnnotations.java"), expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLineBreakAfter() throws Exception
|
||||
{
|
||||
mCheckConfig.addAttribute("option", LeftCurlyOption.EOL.toString());
|
||||
mCheckConfig.addAttribute("maxLineLength", "100");
|
||||
final String[] expected = {
|
||||
"9:1: '{' should be on the previous line.",
|
||||
"12:5: '{' should be on the previous line.",
|
||||
"16:9: '{' should be on the previous line.",
|
||||
"18:13: '{' should be on the previous line.",
|
||||
"20:17: '{' should be on the previous line.",
|
||||
"26:22: '{' should have line break after.",
|
||||
"28:17: '{' should be on the previous line.",
|
||||
"35:33: '{' should have line break after.",
|
||||
"36:21: '{' should have line break after.",
|
||||
"39:29: '{' should have line break after.",
|
||||
"39:34: '{' should have line break after.",
|
||||
"45:37: '{' should have line break after.",
|
||||
"53:5: '{' should be on the previous line.",
|
||||
"54:19: '{' should have line break after.",
|
||||
"64:1: '{' should be on the previous line.",
|
||||
};
|
||||
verify(mCheckConfig, getPath("InputLeftCurlyLineBreakAfter.java"), expected);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,72 @@
|
|||
package com.puppycrawl.tools.checkstyle;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Target;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class InputLeftCurlyOther
|
||||
{
|
||||
/** @see test method **/
|
||||
int foo() throws InterruptedException
|
||||
{
|
||||
int x = 1;
|
||||
int a = 2;
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (x > 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else if (x < 0) {
|
||||
;
|
||||
}
|
||||
else { break; }
|
||||
switch (a)
|
||||
{
|
||||
case 0:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception e) { break; }
|
||||
finally { break; }
|
||||
}
|
||||
|
||||
synchronized (this) { do { x = 2; } while (x == 2); }
|
||||
|
||||
synchronized (this) {
|
||||
do {} while (x == 2);
|
||||
}
|
||||
|
||||
for (int k = 0; k < 1; k++) { String innerBlockVariable = ""; }
|
||||
|
||||
for (int k = 0; k < 1; k++) {}
|
||||
}
|
||||
|
||||
static { int x = 1; }
|
||||
|
||||
void method2()
|
||||
{
|
||||
if (flag) { System.err.println("foo"); }
|
||||
}
|
||||
}
|
||||
|
||||
class Absent_CustomFieldSerializer {
|
||||
|
||||
public static void serialize() {}
|
||||
}
|
||||
|
||||
class Absent_CustomFieldSerializer
|
||||
{
|
||||
public Absent_CustomFieldSerializer() {}
|
||||
}
|
||||
|
||||
class EmptyClass {}
|
||||
|
||||
interface EmptyInterface {}
|
||||
|
||||
enum KnownOrder { KNOWN_ORDER, UNKNOWN_ORDER }
|
||||
|
|
@ -116,6 +116,12 @@
|
|||
<td><a href="property_types.html#lcurly">left curly brace policy</a></td>
|
||||
<td><code>eol</code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>ignoreEnums</td>
|
||||
<td>If true, Check will ignore enums</td>
|
||||
<td><a href="property_types.html#boolean">boolean</a></td>
|
||||
<td>true</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>maxLineLength</td>
|
||||
<td>maximum number of characters in a line</td>
|
||||
|
|
@ -174,6 +180,14 @@
|
|||
<module name="LeftCurly">
|
||||
<property name="option" value="nl"/>
|
||||
<property name="tokens" value="CLASS_DEF,INTERFACE_DEF"/>
|
||||
</module>
|
||||
</source>
|
||||
<p>
|
||||
An example of how to configure the check to validate enum definitions:
|
||||
</p>
|
||||
<source>
|
||||
<module name="LeftCurly">
|
||||
<property name="ignoreEnums" value="false"/>
|
||||
</module>
|
||||
</source>
|
||||
</subsection>
|
||||
|
|
|
|||
Loading…
Reference in New Issue