Skip to content

Commit 8bca211

Browse files
committed
Allow whitespace after '(' and before '//'
Closes gh-270
1 parent ae5e3f2 commit 8bca211

File tree

5 files changed

+114
-1
lines changed

5 files changed

+114
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
}

spring-javaformat/spring-javaformat-checkstyle/src/main/resources/io/spring/javaformat/checkstyle/check/messages.properties

+6
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,9 @@ testfilename.wrongName=Test classes should have a name ending with 'Tests.java'.
3030
leadingwhitespace.incorrect=Indentation should be performed with {0} only.
3131
deprecated.missingSince=@Deprecated has no since attribute.
3232
deprecated.emptySince=@Deprecated has an empty since attribute.
33+
ws.followed=''{0}'' is followed by whitespace.
34+
ws.illegalFollow=''{0}'' is followed by an illegal character.
35+
ws.notFollowed=''{0}'' is not followed by whitespace.
36+
ws.notPreceded=''{0}'' is not preceded with whitespace.
37+
ws.preceded=''{0}'' is preceded with whitespace.
38+
ws.typeCast=''typecast'' is not followed by whitespace.

spring-javaformat/spring-javaformat-checkstyle/src/main/resources/io/spring/javaformat/checkstyle/spring-checkstyle.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@
146146
<property name="tokens" value="BNOT, DEC, DOT, INC, LNOT, UNARY_MINUS, UNARY_PLUS, ARRAY_DECLARATOR"/>
147147
</module>
148148
<module name="com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheck" />
149-
<module name="com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheck" />
150149
<module name="com.puppycrawl.tools.checkstyle.checks.whitespace.TypecastParenPadCheck" />
151150
<module name="com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheck" />
152151
<module name="com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAroundCheck" />
@@ -162,5 +161,6 @@
162161
<module name="io.spring.javaformat.checkstyle.check.SpringLeadingWhitespaceCheck" />
163162
<module name="io.spring.javaformat.checkstyle.check.SpringMethodOrderCheck" />
164163
<module name="io.spring.javaformat.checkstyle.check.SpringMethodVisibilityCheck" />
164+
<module name="io.spring.javaformat.checkstyle.check.SpringParenPadCheck" />
165165
</module>
166166
</module>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+0 errors
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
/**
18+
* This is a valid example.
19+
*
20+
* @author Phillip Webb
21+
*/
22+
public class AsciidoctorCallout {
23+
24+
public void example() {
25+
RestAssured.given(this.spec).filter(document("headers", requestHeaders( // <1>
26+
)));
27+
}
28+
29+
}

0 commit comments

Comments
 (0)