/
IndentationCheck.java
332 lines (292 loc) · 9.84 KB
/
IndentationCheck.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2016 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.checks.indentation;
import java.util.ArrayDeque;
import java.util.Deque;
import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
/**
* Checks correct indentation of Java Code.
*
* <p>
* The basic idea behind this is that while
* pretty printers are sometimes convenient for reformats of
* legacy code, they often either aren't configurable enough or
* just can't anticipate how format should be done. Sometimes this is
* personal preference, other times it is practical experience. In any
* case, this check should just ensure that a minimal set of indentation
* rules are followed.
* </p>
*
* <p>
* Implementation --
* Basically, this check requests visitation for all handled token
* types (those tokens registered in the HandlerFactory). When visitToken
* is called, a new ExpressionHandler is created for the AST and pushed
* onto the handlers stack. The new handler then checks the indentation
* for the currently visiting AST. When leaveToken is called, the
* ExpressionHandler is popped from the stack.
* </p>
*
* <p>
* While on the stack the ExpressionHandler can be queried for the
* indentation level it suggests for children as well as for other
* values.
* </p>
*
* <p>
* While an ExpressionHandler checks the indentation level of its own
* AST, it typically also checks surrounding ASTs. For instance, a
* while loop handler checks the while loop as well as the braces
* and immediate children.
* </p>
* <pre>
* - handler class -to-> ID mapping kept in Map
* - parent passed in during construction
* - suggest child indent level
* - allows for some tokens to be on same line (ie inner classes OBJBLOCK)
* and not increase indentation level
* - looked at using double dispatch for getSuggestedChildIndent(), but it
* doesn't seem worthwhile, at least now
* - both tabs and spaces are considered whitespace in front of the line...
* tabs are converted to spaces
* - block parents with parens -- for, while, if, etc... -- are checked that
* they match the level of the parent
* </pre>
*
* @author jrichard
* @author o_sukhodolsky
* @author Maikel Steneker
* @author maxvetrenko
*/
public class IndentationCheck extends Check {
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_ERROR = "indentation.error";
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_ERROR_MULTI = "indentation.error.multi";
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_CHILD_ERROR = "indentation.child.error";
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_CHILD_ERROR_MULTI = "indentation.child.error.multi";
/** Default indentation amount - based on Sun. */
private static final int DEFAULT_INDENTATION = 4;
/** Handlers currently in use. */
private final Deque<AbstractExpressionHandler> handlers = new ArrayDeque<>();
/** Factory from which handlers are distributed. */
private final HandlerFactory handlerFactory = new HandlerFactory();
/** How many tabs or spaces to use. */
private int basicOffset = DEFAULT_INDENTATION;
/** How much to indent a case label. */
private int caseIndent = DEFAULT_INDENTATION;
/** How far brace should be indented when on next line. */
private int braceAdjustment;
/** How far throws should be indented when on next line. */
private int throwsIndent = DEFAULT_INDENTATION;
/** How much to indent an array initialization when on next line. */
private int arrayInitIndent = DEFAULT_INDENTATION;
/** How far continuation line should be indented when line-wrapping is present. */
private int lineWrappingIndentation = 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 lineWrappingIndentation parameter.
*/
private boolean forceStrictCondition;
/**
* Get forcing strict condition.
* @return forceStrictCondition value.
*/
public boolean isForceStrictCondition() {
return forceStrictCondition;
}
/**
* Set forcing strict condition.
* @param value user's value of forceStrictCondition.
*/
public void setForceStrictCondition(boolean value) {
forceStrictCondition = value;
}
/**
* Set the basic offset.
*
* @param basicOffset the number of tabs or spaces to indent
*/
public void setBasicOffset(int basicOffset) {
this.basicOffset = basicOffset;
}
/**
* Get the basic offset.
*
* @return the number of tabs or spaces to indent
*/
public int getBasicOffset() {
return basicOffset;
}
/**
* Adjusts brace indentation (positive offset).
*
* @param adjustmentAmount the brace offset
*/
public void setBraceAdjustment(int adjustmentAmount) {
braceAdjustment = adjustmentAmount;
}
/**
* Get the brace adjustment amount.
*
* @return the positive offset to adjust braces
*/
public int getBraceAdjustment() {
return braceAdjustment;
}
/**
* Set the case indentation level.
*
* @param amount the case indentation level
*/
public void setCaseIndent(int amount) {
caseIndent = amount;
}
/**
* Get the case indentation level.
*
* @return the case indentation level
*/
public int getCaseIndent() {
return caseIndent;
}
/**
* Set the throws indentation level.
*
* @param throwsIndent the throws indentation level
*/
public void setThrowsIndent(int throwsIndent) {
this.throwsIndent = throwsIndent;
}
/**
* Get the throws indentation level.
*
* @return the throws indentation level
*/
public int getThrowsIndent() {
return throwsIndent;
}
/**
* Set the array initialisation indentation level.
*
* @param arrayInitIndent the array initialisation indentation level
*/
public void setArrayInitIndent(int arrayInitIndent) {
this.arrayInitIndent = arrayInitIndent;
}
/**
* Get the line-wrapping indentation level.
*
* @return the initialisation indentation level
*/
public int getArrayInitIndent() {
return arrayInitIndent;
}
/**
* Get the array line-wrapping indentation level.
*
* @return the line-wrapping indentation level
*/
public int getLineWrappingIndentation() {
return lineWrappingIndentation;
}
/**
* Set the line-wrapping indentation level.
*
* @param lineWrappingIndentation the line-wrapping indentation level
*/
public void setLineWrappingIndentation(int lineWrappingIndentation) {
this.lineWrappingIndentation = lineWrappingIndentation;
}
/**
* Log an error message.
*
* @param line the line number where the error was found
* @param key the message that describes the error
* @param args the details of the message
*
* @see java.text.MessageFormat
*/
public void indentationLog(int line, String key, Object... args) {
log(line, key, args);
}
/**
* Get the width of a tab.
*
* @return the width of a tab
*/
public int getIndentationTabWidth() {
return getTabWidth();
}
@Override
public int[] getDefaultTokens() {
return getAcceptableTokens();
}
@Override
public int[] getAcceptableTokens() {
return handlerFactory.getHandledTypes();
}
@Override
public int[] getRequiredTokens() {
return getAcceptableTokens();
}
@Override
public void beginTree(DetailAST ast) {
handlerFactory.clearCreatedHandlers();
handlers.clear();
final PrimordialHandler primordialHandler = new PrimordialHandler(this);
handlers.push(primordialHandler);
primordialHandler.checkIndentation();
}
@Override
public void visitToken(DetailAST ast) {
final AbstractExpressionHandler handler = handlerFactory.getHandler(this, ast,
handlers.peek());
handlers.push(handler);
handler.checkIndentation();
}
@Override
public void leaveToken(DetailAST ast) {
handlers.pop();
}
/**
* Accessor for the handler factory.
*
* @return the handler factory
*/
final HandlerFactory getHandlerFactory() {
return handlerFactory;
}
}