1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.checks.indentation;
21
22 import com.puppycrawl.tools.checkstyle.api.DetailAST;
23
24
25
26
27
28 public class LambdaHandler extends AbstractExpressionHandler {
29
30
31
32
33
34
35 private boolean isLambdaCorrectlyIndented = true;
36
37
38
39
40
41
42
43
44
45 public LambdaHandler(IndentationCheck indentCheck,
46 DetailAST ast, AbstractExpressionHandler parent) {
47 super(indentCheck, "lambda", ast, parent);
48 }
49
50 @Override
51 public IndentLevel getSuggestedChildIndent(AbstractExpressionHandler child) {
52 IndentLevel childIndent = getIndent();
53 if (isLambdaCorrectlyIndented) {
54 childIndent = IndentLevel.addAcceptable(childIndent, getLineStart(getMainAst()),
55 getLineStart(getMainAst().getFirstChild()));
56 }
57
58 return childIndent;
59 }
60
61
62
63
64
65
66
67
68 @Override
69 protected IndentLevel getIndentImpl() {
70 if (getParent() instanceof MethodCallHandler) {
71 return getParent().getSuggestedChildIndent(this);
72 }
73
74 DetailAST parent = getMainAst().getParent();
75 if (getParent() instanceof NewHandler) {
76 parent = parent.getParent();
77 }
78
79
80 IndentLevel level = new IndentLevel(getLineStart(parent));
81
82
83
84 final DetailAST firstChild = getMainAst().getFirstChild();
85 if (getLineStart(firstChild) == expandedTabsColumnNo(firstChild)) {
86 level = new IndentLevel(level, getIndentCheck().getLineWrappingIndentation());
87 }
88
89 return level;
90 }
91
92 @Override
93 public void checkIndentation() {
94 final DetailAST mainAst = getMainAst();
95 final DetailAST firstChild = mainAst.getFirstChild();
96
97
98
99 final boolean isSwitchRuleLambda = firstChild == null;
100
101 if (!isSwitchRuleLambda
102 && getLineStart(firstChild) == expandedTabsColumnNo(firstChild)) {
103 final int firstChildColumnNo = expandedTabsColumnNo(firstChild);
104 final IndentLevel level = getIndent();
105
106 if (isNonAcceptableIndent(firstChildColumnNo, level)) {
107 isLambdaCorrectlyIndented = false;
108 logError(firstChild, "arguments", firstChildColumnNo, level);
109 }
110 }
111
112
113 final int mainAstColumnNo = expandedTabsColumnNo(mainAst);
114 final boolean isLineWrappedLambda = mainAstColumnNo == getLineStart(mainAst);
115 if (isLineWrappedLambda) {
116 checkLineWrappedLambda(isSwitchRuleLambda, mainAstColumnNo);
117 }
118 }
119
120
121
122
123
124
125
126
127 private boolean isNonAcceptableIndent(int astColumnNo, IndentLevel level) {
128 return astColumnNo < level.getFirstIndentLevel()
129 || getIndentCheck().isForceStrictCondition()
130 && !level.isAcceptable(astColumnNo);
131 }
132
133
134
135
136
137
138
139
140 private void checkLineWrappedLambda(final boolean isSwitchRuleLambda,
141 final int mainAstColumnNo) {
142 final IndentLevel level;
143 final DetailAST mainAst = getMainAst();
144
145 if (isSwitchRuleLambda) {
146
147
148
149 final DetailAST previousSibling = mainAst.getPreviousSibling();
150 final int previousLineStart = getLineStart(previousSibling);
151
152 level = new IndentLevel(new IndentLevel(previousLineStart),
153 getIndentCheck().getLineWrappingIndentation());
154 }
155 else {
156 level = new IndentLevel(getIndent(),
157 getIndentCheck().getLineWrappingIndentation());
158 }
159
160 if (isNonAcceptableIndent(mainAstColumnNo, level)) {
161 isLambdaCorrectlyIndented = false;
162 logError(mainAst, "", mainAstColumnNo, level);
163 }
164 }
165 }