forked from checkstyle/checkstyle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SingleSpaceSeparatorCheck.java
286 lines (264 loc) · 9.96 KB
/
SingleSpaceSeparatorCheck.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
///////////////////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
// Copyright (C) 2001-2022 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.whitespace;
import java.util.Arrays;
import com.puppycrawl.tools.checkstyle.StatelessCheck;
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.utils.CodePointUtil;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
/**
* <p>
* Checks that non-whitespace characters are separated by no more than one
* whitespace. Separating characters by tabs or multiple spaces will be
* reported. Currently, the check doesn't permit horizontal alignment. To inspect
* whitespaces before and after comments, set the property
* {@code validateComments} to true.
* </p>
*
* <p>
* Setting {@code validateComments} to false will ignore cases like:
* </p>
*
* <pre>
* int i; // Multiple whitespaces before comment tokens will be ignored.
* private void foo(int /* whitespaces before and after block-comments will be
* ignored */ i) {
* </pre>
*
* <p>
* Sometimes, users like to space similar items on different lines to the same
* column position for easier reading. This feature isn't supported by this
* check, so both braces in the following case will be reported as violations.
* </p>
*
* <pre>
* public long toNanos(long d) { return d; } // 2 violations
* public long toMicros(long d) { return d / (C1 / C0); }
* </pre>
* <ul>
* <li>
* Property {@code validateComments} - Control whether to validate whitespaces
* surrounding comments.
* Type is {@code boolean}.
* Default value is {@code false}.
* </li>
* </ul>
* <p>
* To configure the check:
* </p>
*
* <pre>
* <module name="SingleSpaceSeparator"/>
* </pre>
* <p>Example:</p>
* <pre>
* int foo() { // violation, 3 whitespaces
* return 1; // violation, 2 whitespaces
* }
* int fun1() { // OK, 1 whitespace
* return 3; // OK, 1 whitespace
* }
* void fun2() {} // violation, 2 whitespaces
* </pre>
*
* <p>
* To configure the check so that it validates comments:
* </p>
*
* <pre>
* <module name="SingleSpaceSeparator">
* <property name="validateComments" value="true"/>
* </module>
* </pre>
* <p>Example:</p>
* <pre>
* void fun1() {} // violation, 2 whitespaces before the comment starts
* void fun2() { return; } /* violation here, 2 whitespaces before the comment starts */
*
* /* violation, 2 whitespaces after the comment ends */ int a;
*
* String s; /* OK, 1 whitespace */
*
* /**
* * This is a Javadoc comment
* */ int b; // violation, 2 whitespaces after the javadoc comment ends
*
* float f1; // OK, 1 whitespace
*
* /**
* * OK, 1 white space after the doc comment ends
* */ float f2;
* </pre>
* <p>
* Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
* </p>
* <p>
* Violation Message Keys:
* </p>
* <ul>
* <li>
* {@code single.space.separator}
* </li>
* </ul>
*
* @since 6.19
*/
@StatelessCheck
public class SingleSpaceSeparatorCheck extends AbstractCheck {
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY = "single.space.separator";
/** Control whether to validate whitespaces surrounding comments. */
private boolean validateComments;
/**
* Setter to control whether to validate whitespaces surrounding comments.
*
* @param validateComments {@code true} to validate surrounding whitespaces at comments.
*/
public void setValidateComments(boolean validateComments) {
this.validateComments = validateComments;
}
@Override
public int[] getDefaultTokens() {
return getRequiredTokens();
}
@Override
public int[] getAcceptableTokens() {
return getRequiredTokens();
}
@Override
public int[] getRequiredTokens() {
return CommonUtil.EMPTY_INT_ARRAY;
}
@Override
public boolean isCommentNodesRequired() {
return validateComments;
}
@Override
public void beginTree(DetailAST rootAST) {
if (rootAST != null) {
visitEachToken(rootAST);
}
}
/**
* Examines every sibling and child of {@code node} for violations.
*
* @param node The node to start examining.
*/
private void visitEachToken(DetailAST node) {
DetailAST currentNode = node;
do {
final int columnNo = currentNode.getColumnNo() - 1;
// in such expression: "j =123", placed at the start of the string index of the second
// space character will be: 2 = 0(j) + 1(whitespace) + 1(whitespace). It is a minimal
// possible index for the second whitespace between non-whitespace characters.
final int minSecondWhitespaceColumnNo = 2;
if (columnNo >= minSecondWhitespaceColumnNo
&& !isTextSeparatedCorrectlyFromPrevious(
getLineCodePoints(currentNode.getLineNo() - 1),
columnNo)) {
log(currentNode, MSG_KEY);
}
if (currentNode.hasChildren()) {
currentNode = currentNode.getFirstChild();
}
else {
while (currentNode.getNextSibling() == null && currentNode.getParent() != null) {
currentNode = currentNode.getParent();
}
currentNode = currentNode.getNextSibling();
}
} while (currentNode != null);
}
/**
* Checks if characters in {@code line} at and around {@code columnNo} has
* the correct number of spaces. to return {@code true} the following
* conditions must be met:
* <ul>
* <li> the character at {@code columnNo} is the first in the line. </li>
* <li> the character at {@code columnNo} is not separated by whitespaces from
* the previous non-whitespace character. </li>
* <li> the character at {@code columnNo} is separated by only one whitespace
* from the previous non-whitespace character. </li>
* <li> {@link #validateComments} is disabled and the previous text is the
* end of a block comment. </li>
* </ul>
*
* @param line Unicode code point array of line in the file to examine.
* @param columnNo The column position in the {@code line} to examine.
* @return {@code true} if the text at {@code columnNo} is separated
* correctly from the previous token.
*/
private boolean isTextSeparatedCorrectlyFromPrevious(int[] line, int columnNo) {
return isSingleSpace(line, columnNo)
|| !CommonUtil.isCodePointWhitespace(line, columnNo)
|| isFirstInLine(line, columnNo)
|| !validateComments && isBlockCommentEnd(line, columnNo);
}
/**
* Checks if the {@code line} at {@code columnNo} is a single space, and not
* preceded by another space.
*
* @param line Unicode code point array of line in the file to examine.
* @param columnNo The column position in the {@code line} to examine.
* @return {@code true} if the character at {@code columnNo} is a space, and
* not preceded by another space.
*/
private static boolean isSingleSpace(int[] line, int columnNo) {
return isSpace(line, columnNo) && !CommonUtil.isCodePointWhitespace(line, columnNo - 1);
}
/**
* Checks if the {@code line} at {@code columnNo} is a space.
*
* @param line Unicode code point array of line in the file to examine.
* @param columnNo The column position in the {@code line} to examine.
* @return {@code true} if the character at {@code columnNo} is a space.
*/
private static boolean isSpace(int[] line, int columnNo) {
return line[columnNo] == ' ';
}
/**
* Checks if the {@code line} up to and including {@code columnNo} is all
* non-whitespace text encountered.
*
* @param line Unicode code point array of line in the file to examine.
* @param columnNo The column position in the {@code line} to examine.
* @return {@code true} if the column position is the first non-whitespace
* text on the {@code line}.
*/
private static boolean isFirstInLine(int[] line, int columnNo) {
return CodePointUtil.isBlank(Arrays.copyOfRange(line, 0, columnNo));
}
/**
* Checks if the {@code line} at {@code columnNo} is the end of a comment,
* '*/'.
*
* @param line Unicode code point array of line in the file to examine.
* @param columnNo The column position in the {@code line} to examine.
* @return {@code true} if the previous text is an end comment block.
*/
private static boolean isBlockCommentEnd(int[] line, int columnNo) {
final int[] strippedLine = CodePointUtil
.stripTrailing(Arrays.copyOfRange(line, 0, columnNo));
return CodePointUtil.endsWith(strippedLine, "*/");
}
}