001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018///////////////////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.whitespace;
021
022import java.util.Arrays;
023
024import com.puppycrawl.tools.checkstyle.StatelessCheck;
025import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
026import com.puppycrawl.tools.checkstyle.api.DetailAST;
027import com.puppycrawl.tools.checkstyle.utils.CodePointUtil;
028import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
029
030/**
031 * <p>
032 * Checks that non-whitespace characters are separated by no more than one
033 * whitespace. Separating characters by tabs or multiple spaces will be
034 * reported. Currently, the check doesn't permit horizontal alignment. To inspect
035 * whitespaces before and after comments, set the property
036 * {@code validateComments} to true.
037 * </p>
038 *
039 * <p>
040 * Setting {@code validateComments} to false will ignore cases like:
041 * </p>
042 *
043 * <pre>
044 * int i;  &#47;&#47; Multiple whitespaces before comment tokens will be ignored.
045 * private void foo(int  &#47;* whitespaces before and after block-comments will be
046 * ignored *&#47;  i) {
047 * </pre>
048 *
049 * <p>
050 * Sometimes, users like to space similar items on different lines to the same
051 * column position for easier reading. This feature isn't supported by this
052 * check, so both braces in the following case will be reported as violations.
053 * </p>
054 *
055 * <pre>
056 * public long toNanos(long d)  { return d;             } &#47;&#47; 2 violations
057 * public long toMicros(long d) { return d / (C1 / C0); }
058 * </pre>
059 * <ul>
060 * <li>
061 * Property {@code validateComments} - Control whether to validate whitespaces
062 * surrounding comments.
063 * Type is {@code boolean}.
064 * Default value is {@code false}.
065 * </li>
066 * </ul>
067 * <p>
068 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
069 * </p>
070 * <p>
071 * Violation Message Keys:
072 * </p>
073 * <ul>
074 * <li>
075 * {@code single.space.separator}
076 * </li>
077 * </ul>
078 *
079 * @since 6.19
080 */
081@StatelessCheck
082public class SingleSpaceSeparatorCheck extends AbstractCheck {
083
084    /**
085     * A key is pointing to the warning message text in "messages.properties"
086     * file.
087     */
088    public static final String MSG_KEY = "single.space.separator";
089
090    /** Control whether to validate whitespaces surrounding comments. */
091    private boolean validateComments;
092
093    /**
094     * Setter to control whether to validate whitespaces surrounding comments.
095     *
096     * @param validateComments {@code true} to validate surrounding whitespaces at comments.
097     * @since 6.19
098     */
099    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}