Skip to content

Commit 30ce469

Browse files
committed
Skip SpringLeadingWhitespaceCheck in text blocks
Closes gh-421
1 parent 9eeeee1 commit 30ce469

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-2
lines changed

Diff for: spring-javaformat/spring-javaformat-checkstyle/src/main/java/io/spring/javaformat/checkstyle/check/SpringLeadingWhitespaceCheck.java

+51-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
package io.spring.javaformat.checkstyle.check;
1818

1919
import java.io.File;
20+
import java.util.ArrayDeque;
2021
import java.util.Collections;
22+
import java.util.Deque;
2123
import java.util.HashMap;
2224
import java.util.Map;
2325
import java.util.regex.Matcher;
@@ -26,6 +28,7 @@
2628
import com.puppycrawl.tools.checkstyle.api.DetailAST;
2729
import com.puppycrawl.tools.checkstyle.api.FileContents;
2830
import com.puppycrawl.tools.checkstyle.api.FileText;
31+
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
2932

3033
import io.spring.javaformat.config.IndentationStyle;
3134
import io.spring.javaformat.config.JavaFormatConfig;
@@ -49,14 +52,32 @@ public class SpringLeadingWhitespaceCheck extends AbstractSpringCheck {
4952

5053
private IndentationStyle indentationStyle;
5154

55+
private final Deque<TextBlockPair> textBlockPairs = new ArrayDeque<>();
56+
5257
@Override
5358
public int[] getAcceptableTokens() {
54-
return NO_REQUIRED_TOKENS;
59+
return new int[] { TokenTypes.TEXT_BLOCK_LITERAL_BEGIN, TokenTypes.TEXT_BLOCK_LITERAL_END };
60+
}
61+
62+
@Override
63+
public void visitToken(DetailAST ast) {
64+
super.visitToken(ast);
65+
if (ast.getType() == TokenTypes.TEXT_BLOCK_LITERAL_BEGIN) {
66+
this.textBlockPairs.add(new TextBlockPair(ast));
67+
}
68+
else if (ast.getType() == TokenTypes.TEXT_BLOCK_LITERAL_END) {
69+
this.textBlockPairs.getLast().end(ast);
70+
}
5571
}
5672

5773
@Override
5874
public void beginTree(DetailAST rootAST) {
5975
super.beginTree(rootAST);
76+
this.textBlockPairs.clear();
77+
}
78+
79+
@Override
80+
public void finishTree(DetailAST rootAST) {
6081
FileContents fileContents = getFileContents();
6182
FileText fileText = fileContents.getText();
6283
File file = fileText.getFile();
@@ -66,8 +87,11 @@ public void beginTree(DetailAST rootAST) {
6687
IndentationStyle indentationStyle = (this.indentationStyle != null) ? this.indentationStyle
6788
: JavaFormatConfig.findFrom(file.getParentFile()).getIndentationStyle();
6889
for (int i = 0; i < fileText.size(); i++) {
69-
String line = fileText.get(i);
7090
int lineNo = i + 1;
91+
if (isInTextBlock(lineNo)) {
92+
continue;
93+
}
94+
String line = fileText.get(i);
7195
Matcher matcher = PATTERN.matcher(line);
7296
boolean found = matcher.find(0);
7397
while (found
@@ -78,11 +102,36 @@ public void beginTree(DetailAST rootAST) {
78102
log(lineNo, "leadingwhitespace.incorrect", indentationStyle.toString().toLowerCase());
79103
}
80104
}
105+
super.finishTree(rootAST);
106+
}
107+
108+
private boolean isInTextBlock(int lineNo) {
109+
return this.textBlockPairs.stream().anyMatch((textBlockPair) -> textBlockPair.contains(lineNo));
81110
}
82111

83112
public void setIndentationStyle(String indentationStyle) {
84113
this.indentationStyle = (indentationStyle != null && !"".equals(indentationStyle))
85114
? IndentationStyle.valueOf(indentationStyle.toUpperCase()) : null;
86115
}
87116

117+
private static class TextBlockPair {
118+
119+
private final DetailAST begin;
120+
121+
private DetailAST end;
122+
123+
TextBlockPair(DetailAST begin) {
124+
this.begin = begin;
125+
}
126+
127+
public boolean contains(int lineNo) {
128+
return (lineNo > this.begin.getLineNo()) && (lineNo <= this.end.getLineNo());
129+
}
130+
131+
void end(DetailAST end) {
132+
this.end = end;
133+
}
134+
135+
}
136+
88137
}
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,33 @@
1+
/*
2+
* Copyright 2017-2021 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+
* Leading whitepace with a text block.
19+
*
20+
* @author Phillip Webb
21+
*/
22+
public class LeadingWhitespaceTabsAndTextBlock {
23+
24+
/**
25+
* Comments are ignored.
26+
*/
27+
public void hello() {
28+
System.out.println(""""
29+
Hello
30+
World!""");
31+
}
32+
33+
}

0 commit comments

Comments
 (0)