1717package io .spring .javaformat .checkstyle .check ;
1818
1919import java .io .File ;
20+ import java .util .ArrayDeque ;
2021import java .util .Collections ;
22+ import java .util .Deque ;
2123import java .util .HashMap ;
2224import java .util .Map ;
2325import java .util .regex .Matcher ;
2628import com .puppycrawl .tools .checkstyle .api .DetailAST ;
2729import com .puppycrawl .tools .checkstyle .api .FileContents ;
2830import com .puppycrawl .tools .checkstyle .api .FileText ;
31+ import com .puppycrawl .tools .checkstyle .api .TokenTypes ;
2932
3033import io .spring .javaformat .config .IndentationStyle ;
3134import 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}
0 commit comments