Last active
September 26, 2015 07:54
-
-
Save pietern/6f80967e15931c7b8d8e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/LambdaHandler.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/LambdaHandler.java | |
index ae7392a..efa985f 100644 | |
--- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/LambdaHandler.java | |
+++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/LambdaHandler.java | |
@@ -47,17 +47,26 @@ public class LambdaHandler extends AbstractExpressionHandler { | |
@Override | |
protected IndentLevel getLevelImpl() { | |
- // If the start of the lambda isn't the first element on the line, | |
- // use the start of the line as the reference indentation level. | |
- final DetailAST firstChild = getMainAst().getFirstChild(); | |
- if (getLineStart(firstChild) != firstChild.getColumnNo()) { | |
- return new IndentLevel(getLineStart(firstChild)); | |
+ if (getParent() instanceof MethodCallHandler) { | |
+ return getParent().suggestedChildLevel(this); | |
} | |
- // The start of the lambda is the first element on the line; | |
+ DetailAST parent = getMainAst().getParent(); | |
+ if (getParent() instanceof NewHandler) { | |
+ parent = parent.getParent(); | |
+ } | |
+ | |
+ // Use the start of the parent's line as the reference indentation level. | |
+ IndentLevel level = new IndentLevel(getLineStart(parent)); | |
+ | |
+ // If the start of the lambda is the first element on the line; | |
// assume line wrapping with respect to its parent. | |
- final int parentLineStart = getLineStart(getParent().getMainAst()); | |
- return new IndentLevel(parentLineStart + getIndentCheck().getLineWrappingIndentation()); | |
+ final DetailAST firstChild = getMainAst().getFirstChild(); | |
+ if (getLineStart(firstChild) == firstChild.getColumnNo()) { | |
+ level = new IndentLevel(level, getIndentCheck().getLineWrappingIndentation()); | |
+ } | |
+ | |
+ return level; | |
} | |
@Override | |
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/MethodCallHandler.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/MethodCallHandler.java | |
index 804a253..228ac85 100644 | |
--- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/MethodCallHandler.java | |
+++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/MethodCallHandler.java | |
@@ -123,11 +123,19 @@ public class MethodCallHandler extends AbstractExpressionHandler { | |
// new String[] {"method"}).toString()); | |
// will not have the right line num, so just get the child name | |
+ // If the right parenthesis is at the start of a line; | |
+ // add line wrapping to child indent level. | |
+ final DetailAST rparen = getMainAst().findFirstToken(TokenTypes.RPAREN); | |
+ if (getLineStart(rparen) == rparen.getColumnNo()) { | |
+ final IndentLevel myLevel = getParent().suggestedChildLevel(this); | |
+ return new IndentLevel(myLevel, this.getIndentCheck().getLineWrappingIndentation()); | |
+ } | |
+ | |
final DetailAST first = getMainAst().getFirstChild(); | |
int indentLevel = getLineStart(first); | |
if (!areOnSameLine(child.getMainAst().getFirstChild(), | |
getMainAst().getFirstChild())) { | |
- indentLevel += getBasicOffset(); | |
+ indentLevel += this.getIndentCheck().getLineWrappingIndentation(); | |
} | |
return new IndentLevel(indentLevel); | |
} | |
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 4c28991..2dfb925 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 | |
@@ -1574,7 +1574,7 @@ public class IndentationCheckTest extends BaseCheckTestSupport { | |
} | |
@Test | |
- public void testLambda() throws Exception { | |
+ public void testLambda1() throws Exception { | |
final DefaultConfiguration checkConfig = createCheckConfig(IndentationCheck.class); | |
checkConfig.addAttribute("tabWidth", "2"); | |
checkConfig.addAttribute("basicOffset", "2"); | |
@@ -1595,4 +1595,15 @@ public class IndentationCheckTest extends BaseCheckTestSupport { | |
verifyWarns(checkConfig, new File("src/test/resources-noncompilable/com/puppycrawl/tools/" | |
+ "checkstyle/indentation/InputLambda1.java").getCanonicalPath(), expected, 1); | |
} | |
+ | |
+ @Test | |
+ public void testLambda2() throws Exception { | |
+ final DefaultConfiguration checkConfig = createCheckConfig(IndentationCheck.class); | |
+ checkConfig.addAttribute("tabWidth", "4"); | |
+ checkConfig.addAttribute("basicOffset", "4"); | |
+ checkConfig.addAttribute("lineWrappingIndentation", "8"); | |
+ final String[] expected = {}; | |
+ verifyWarns(checkConfig, new File("src/test/resources-noncompilable/com/puppycrawl/tools/" | |
+ + "checkstyle/indentation/InputLambda2.java").getCanonicalPath(), expected, 0); | |
+ } | |
} | |
diff --git a/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/indentation/InputLambda1.java b/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/indentation/InputLambda1.java | |
index 61f4daf..ee3942d 100644 | |
--- a/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/indentation/InputLambda1.java | |
+++ b/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/indentation/InputLambda1.java | |
@@ -197,4 +197,25 @@ public class InputLambda1 { //indent:0 exp:0 | |
.flatMap(f -> f.bars.stream()) //indent:8 exp:8 | |
.forEach(b -> System.out.println(b.name)); //indent:8 exp:8 | |
} //indent:2 exp:2 | |
+ | |
+ Runnable r2r(Runnable x) { //indent:2 exp:2 | |
+ return x; //indent:4 exp:4 | |
+ } //indent:2 exp:2 | |
+ | |
+ void function2(Runnable x) { //indent:2 exp:2 | |
+ Runnable r0 = r2r(() -> { //indent:4 exp:4 | |
+ int i = 1; //indent:6 exp:6 | |
+ }); //indent:4 exp:4 | |
+ | |
+ Runnable r1 = r2r(() -> { //indent:4 exp:4 | |
+ int i = 1; //indent:10 exp:10 | |
+ } //indent:8 exp:8 | |
+ ); //indent:4 exp:4 | |
+ | |
+ Runnable r2 = r2r(r2r(() -> { //indent:4 exp:4 | |
+ int i = 1; //indent:14 exp:14 | |
+ } //indent:12 exp:12 | |
+ ) //indent:8 exp:8 | |
+ ); //indent:4 exp:4 | |
+ } //indent:2 exp:2 | |
} //indent:0 exp:0 | |
diff --git a/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/indentation/InputLambda2.java b/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/indentation/InputLambda2.java | |
new file mode 100644 | |
index 0000000..268bbcb | |
--- /dev/null | |
+++ b/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/indentation/InputLambda2.java | |
@@ -0,0 +1,42 @@ | |
+package com.puppycrawl.tools.checkstyle.indentation; //indent:0 exp:0 | |
+ | |
+import java.util.function.BinaryOperator; //indent:0 exp:0 | |
+import java.util.function.Consumer; //indent:0 exp:0 | |
+ | |
+public class InputLambda2 { //indent:0 exp:0 | |
+ public <T> Consumer<Integer> params(Consumer<Integer> f1, Consumer<Integer> f2) { //indent:4 exp:4 | |
+ return f2; //indent:8 exp:8 | |
+ } //indent:4 exp:4 | |
+ | |
+ private void print(int i) { //indent:4 exp:4 | |
+ } //indent:4 exp:4 | |
+ | |
+ public Consumer<Integer> returnFunctionOfLambda() { //indent:4 exp:4 | |
+ return params( //indent:8 exp:8 | |
+ (x) -> print(x * 1), //indent:16 exp:16 | |
+ (x) -> print(x * 2) //indent:16 exp:16 | |
+ ); //indent:8 exp:8 | |
+ } //indent:4 exp:4 | |
+ | |
+ public static <T> BinaryOperator<T> returnLambda() { //indent:4 exp:4 | |
+ return (t1, t2) -> { //indent:8 exp:8 | |
+ return t1; //indent:12 exp:12 | |
+ }; //indent:8 exp:8 | |
+ } //indent:4 exp:4 | |
+ | |
+ class TwoParams { //indent:4 exp:4 | |
+ TwoParams(Consumer<Integer> c1, Consumer<Integer> c2) { //indent:8 exp:8 | |
+ } //indent:8 exp:8 | |
+ } //indent:4 exp:4 | |
+ | |
+ public void makeTwoParams() { //indent:4 exp:4 | |
+ TwoParams t0 = new TwoParams( //indent:8 exp:8 | |
+ (x) -> print(x * 1), //indent:16 exp:16 | |
+ (x) -> print(x * 2) //indent:16 exp:16 | |
+ ); //indent:8 exp:8 | |
+ | |
+ TwoParams t1 = new TwoParams( //indent:8 exp:8 | |
+ (x) -> print(x * 1), //indent:16 exp:16 | |
+ (x) -> print(x * 2)); //indent:16 exp:16 | |
+ } //indent:4 exp:4 | |
+} //indent:0 exp:0 | |
\ No newline at end of file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment