17
17
package io .spring .javaformat .checkstyle .check ;
18
18
19
19
import java .io .File ;
20
+ import java .util .ArrayDeque ;
20
21
import java .util .Collections ;
22
+ import java .util .Deque ;
21
23
import java .util .HashMap ;
22
24
import java .util .Map ;
23
25
import java .util .regex .Matcher ;
26
28
import com .puppycrawl .tools .checkstyle .api .DetailAST ;
27
29
import com .puppycrawl .tools .checkstyle .api .FileContents ;
28
30
import com .puppycrawl .tools .checkstyle .api .FileText ;
31
+ import com .puppycrawl .tools .checkstyle .api .TokenTypes ;
29
32
30
33
import io .spring .javaformat .config .IndentationStyle ;
31
34
import io .spring .javaformat .config .JavaFormatConfig ;
@@ -49,14 +52,32 @@ public class SpringLeadingWhitespaceCheck extends AbstractSpringCheck {
49
52
50
53
private IndentationStyle indentationStyle ;
51
54
55
+ private final Deque <TextBlockPair > textBlockPairs = new ArrayDeque <>();
56
+
52
57
@ Override
53
58
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
+ }
55
71
}
56
72
57
73
@ Override
58
74
public void beginTree (DetailAST rootAST ) {
59
75
super .beginTree (rootAST );
76
+ this .textBlockPairs .clear ();
77
+ }
78
+
79
+ @ Override
80
+ public void finishTree (DetailAST rootAST ) {
60
81
FileContents fileContents = getFileContents ();
61
82
FileText fileText = fileContents .getText ();
62
83
File file = fileText .getFile ();
@@ -66,8 +87,11 @@ public void beginTree(DetailAST rootAST) {
66
87
IndentationStyle indentationStyle = (this .indentationStyle != null ) ? this .indentationStyle
67
88
: JavaFormatConfig .findFrom (file .getParentFile ()).getIndentationStyle ();
68
89
for (int i = 0 ; i < fileText .size (); i ++) {
69
- String line = fileText .get (i );
70
90
int lineNo = i + 1 ;
91
+ if (isInTextBlock (lineNo )) {
92
+ continue ;
93
+ }
94
+ String line = fileText .get (i );
71
95
Matcher matcher = PATTERN .matcher (line );
72
96
boolean found = matcher .find (0 );
73
97
while (found
@@ -78,11 +102,36 @@ public void beginTree(DetailAST rootAST) {
78
102
log (lineNo , "leadingwhitespace.incorrect" , indentationStyle .toString ().toLowerCase ());
79
103
}
80
104
}
105
+ super .finishTree (rootAST );
106
+ }
107
+
108
+ private boolean isInTextBlock (int lineNo ) {
109
+ return this .textBlockPairs .stream ().anyMatch ((textBlockPair ) -> textBlockPair .contains (lineNo ));
81
110
}
82
111
83
112
public void setIndentationStyle (String indentationStyle ) {
84
113
this .indentationStyle = (indentationStyle != null && !"" .equals (indentationStyle ))
85
114
? IndentationStyle .valueOf (indentationStyle .toUpperCase ()) : null ;
86
115
}
87
116
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
+
88
137
}
0 commit comments