|
| 1 | +/* |
| 2 | + * Copyright 2017-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.spring.javaformat.checkstyle.check; |
| 18 | + |
| 19 | +import java.util.Locale; |
| 20 | + |
| 21 | +import com.puppycrawl.tools.checkstyle.api.DetailAST; |
| 22 | +import com.puppycrawl.tools.checkstyle.checks.whitespace.PadOption; |
| 23 | +import com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheck; |
| 24 | +import com.puppycrawl.tools.checkstyle.utils.CommonUtil; |
| 25 | + |
| 26 | +/** |
| 27 | + * {@link ParenPadCheck} variant that allows whitespace after {@code (} if before |
| 28 | + * {@code //}. |
| 29 | + * |
| 30 | + * @author Phillip Webb |
| 31 | + */ |
| 32 | +public class SpringParenPadCheck extends ParenPadCheck { |
| 33 | + |
| 34 | + private static final char OPEN_PARENTHESIS = '('; |
| 35 | + |
| 36 | + private static final char CLOSE_PARENTHESIS = ')'; |
| 37 | + |
| 38 | + private PadOption option = PadOption.NOSPACE; |
| 39 | + |
| 40 | + @Override |
| 41 | + public void setOption(String optionStr) { |
| 42 | + this.option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH)); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + protected void processLeft(DetailAST ast) { |
| 47 | + String line = getLines()[ast.getLineNo() - 1]; |
| 48 | + int[] codePoints = line.codePoints().toArray(); |
| 49 | + int after = ast.getColumnNo() + 1; |
| 50 | + if (after < codePoints.length) { |
| 51 | + boolean hasWhitespaceAfter = isConsideredWhitespace(codePoints, after); |
| 52 | + if (this.option == PadOption.NOSPACE && hasWhitespaceAfter) { |
| 53 | + log(ast, MSG_WS_FOLLOWED, OPEN_PARENTHESIS); |
| 54 | + } |
| 55 | + else if (this.option == PadOption.SPACE && !hasWhitespaceAfter && line.charAt(after) != CLOSE_PARENTHESIS) { |
| 56 | + log(ast, MSG_WS_NOT_FOLLOWED, OPEN_PARENTHESIS); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private boolean isConsideredWhitespace(int[] codePoints, int after) { |
| 62 | + if (CommonUtil.isCodePointWhitespace(codePoints, after)) { |
| 63 | + return !isSlashSlash(codePoints, after + 1); |
| 64 | + } |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + private boolean isSlashSlash(int[] codePoints, int index) { |
| 69 | + if (index + 1 < codePoints.length) { |
| 70 | + char c1 = Character.toChars(codePoints[index])[0]; |
| 71 | + char c2 = Character.toChars(codePoints[index + 1])[0]; |
| 72 | + return c1 == '/' && c2 == '/'; |
| 73 | + } |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments