View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.whitespace;
21  
22  import java.util.Arrays;
23  
24  import com.puppycrawl.tools.checkstyle.StatelessCheck;
25  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
26  import com.puppycrawl.tools.checkstyle.api.DetailAST;
27  import com.puppycrawl.tools.checkstyle.utils.CodePointUtil;
28  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
29  
30  /**
31   * <p>
32   * Checks that non-whitespace characters are separated by no more than one
33   * whitespace. Separating characters by tabs or multiple spaces will be
34   * reported. Currently, the check doesn't permit horizontal alignment. To inspect
35   * whitespaces before and after comments, set the property
36   * {@code validateComments} to true.
37   * </p>
38   *
39   * <p>
40   * Setting {@code validateComments} to false will ignore cases like:
41   * </p>
42   *
43   * <pre>
44   * int i;  &#47;&#47; Multiple whitespaces before comment tokens will be ignored.
45   * private void foo(int  &#47;* whitespaces before and after block-comments will be
46   * ignored *&#47;  i) {
47   * </pre>
48   *
49   * <p>
50   * Sometimes, users like to space similar items on different lines to the same
51   * column position for easier reading. This feature isn't supported by this
52   * check, so both braces in the following case will be reported as violations.
53   * </p>
54   *
55   * <pre>
56   * public long toNanos(long d)  { return d;             } &#47;&#47; 2 violations
57   * public long toMicros(long d) { return d / (C1 / C0); }
58   * </pre>
59   * <ul>
60   * <li>
61   * Property {@code validateComments} - Control whether to validate whitespaces
62   * surrounding comments.
63   * Type is {@code boolean}.
64   * Default value is {@code false}.
65   * </li>
66   * </ul>
67   * <p>
68   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
69   * </p>
70   * <p>
71   * Violation Message Keys:
72   * </p>
73   * <ul>
74   * <li>
75   * {@code single.space.separator}
76   * </li>
77   * </ul>
78   *
79   * @since 6.19
80   */
81  @StatelessCheck
82  public class SingleSpaceSeparatorCheck extends AbstractCheck {
83  
84      /**
85       * A key is pointing to the warning message text in "messages.properties"
86       * file.
87       */
88      public static final String MSG_KEY = "single.space.separator";
89  
90      /** Control whether to validate whitespaces surrounding comments. */
91      private boolean validateComments;
92  
93      /**
94       * Setter to control whether to validate whitespaces surrounding comments.
95       *
96       * @param validateComments {@code true} to validate surrounding whitespaces at comments.
97       * @since 6.19
98       */
99      public void setValidateComments(boolean validateComments) {
100         this.validateComments = validateComments;
101     }
102 
103     @Override
104     public int[] getDefaultTokens() {
105         return getRequiredTokens();
106     }
107 
108     @Override
109     public int[] getAcceptableTokens() {
110         return getRequiredTokens();
111     }
112 
113     @Override
114     public int[] getRequiredTokens() {
115         return CommonUtil.EMPTY_INT_ARRAY;
116     }
117 
118     @Override
119     public boolean isCommentNodesRequired() {
120         return validateComments;
121     }
122 
123     @Override
124     public void beginTree(DetailAST rootAST) {
125         if (rootAST != null) {
126             visitEachToken(rootAST);
127         }
128     }
129 
130     /**
131      * Examines every sibling and child of {@code node} for violations.
132      *
133      * @param node The node to start examining.
134      */
135     private void visitEachToken(DetailAST node) {
136         DetailAST currentNode = node;
137 
138         do {
139             final int columnNo = currentNode.getColumnNo() - 1;
140 
141             // in such expression: "j  =123", placed at the start of the string index of the second
142             // space character will be: 2 = 0(j) + 1(whitespace) + 1(whitespace). It is a minimal
143             // possible index for the second whitespace between non-whitespace characters.
144             final int minSecondWhitespaceColumnNo = 2;
145 
146             if (columnNo >= minSecondWhitespaceColumnNo
147                     && !isTextSeparatedCorrectlyFromPrevious(
148                             getLineCodePoints(currentNode.getLineNo() - 1),
149                             columnNo)) {
150                 log(currentNode, MSG_KEY);
151             }
152             if (currentNode.hasChildren()) {
153                 currentNode = currentNode.getFirstChild();
154             }
155             else {
156                 while (currentNode.getNextSibling() == null && currentNode.getParent() != null) {
157                     currentNode = currentNode.getParent();
158                 }
159                 currentNode = currentNode.getNextSibling();
160             }
161         } while (currentNode != null);
162     }
163 
164     /**
165      * Checks if characters in {@code line} at and around {@code columnNo} has
166      * the correct number of spaces. to return {@code true} the following
167      * conditions must be met:
168      * <ul>
169      * <li> the character at {@code columnNo} is the first in the line. </li>
170      * <li> the character at {@code columnNo} is not separated by whitespaces from
171      * the previous non-whitespace character. </li>
172      * <li> the character at {@code columnNo} is separated by only one whitespace
173      * from the previous non-whitespace character. </li>
174      * <li> {@link #validateComments} is disabled and the previous text is the
175      * end of a block comment. </li>
176      * </ul>
177      *
178      * @param line Unicode code point array of line in the file to examine.
179      * @param columnNo The column position in the {@code line} to examine.
180      * @return {@code true} if the text at {@code columnNo} is separated
181      *         correctly from the previous token.
182      */
183     private boolean isTextSeparatedCorrectlyFromPrevious(int[] line, int columnNo) {
184         return isSingleSpace(line, columnNo)
185                 || !CommonUtil.isCodePointWhitespace(line, columnNo)
186                 || isFirstInLine(line, columnNo)
187                 || !validateComments && isBlockCommentEnd(line, columnNo);
188     }
189 
190     /**
191      * Checks if the {@code line} at {@code columnNo} is a single space, and not
192      * preceded by another space.
193      *
194      * @param line Unicode code point array of line in the file to examine.
195      * @param columnNo The column position in the {@code line} to examine.
196      * @return {@code true} if the character at {@code columnNo} is a space, and
197      *         not preceded by another space.
198      */
199     private static boolean isSingleSpace(int[] line, int columnNo) {
200         return isSpace(line, columnNo) && !CommonUtil.isCodePointWhitespace(line, columnNo - 1);
201     }
202 
203     /**
204      * Checks if the {@code line} at {@code columnNo} is a space.
205      *
206      * @param line Unicode code point array of line in the file to examine.
207      * @param columnNo The column position in the {@code line} to examine.
208      * @return {@code true} if the character at {@code columnNo} is a space.
209      */
210     private static boolean isSpace(int[] line, int columnNo) {
211         return line[columnNo] == ' ';
212     }
213 
214     /**
215      * Checks if the {@code line} up to and including {@code columnNo} is all
216      * non-whitespace text encountered.
217      *
218      * @param line Unicode code point array of line in the file to examine.
219      * @param columnNo The column position in the {@code line} to examine.
220      * @return {@code true} if the column position is the first non-whitespace
221      *         text on the {@code line}.
222      */
223     private static boolean isFirstInLine(int[] line, int columnNo) {
224         return CodePointUtil.isBlank(Arrays.copyOfRange(line, 0, columnNo));
225     }
226 
227     /**
228      * Checks if the {@code line} at {@code columnNo} is the end of a comment,
229      * '*&#47;'.
230      *
231      * @param line Unicode code point array of line in the file to examine.
232      * @param columnNo The column position in the {@code line} to examine.
233      * @return {@code true} if the previous text is an end comment block.
234      */
235     private static boolean isBlockCommentEnd(int[] line, int columnNo) {
236         final int[] strippedLine = CodePointUtil
237                 .stripTrailing(Arrays.copyOfRange(line, 0, columnNo));
238         return CodePointUtil.endsWith(strippedLine, "*/");
239     }
240 
241 }