Skip to content

Commit

Permalink
Skip SpringLeadingWhitespaceCheck in text blocks
Browse files Browse the repository at this point in the history
Closes gh-421
  • Loading branch information
philwebb committed Aug 13, 2024
1 parent 9eeeee1 commit 30ce469
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package io.spring.javaformat.checkstyle.check;

import java.io.File;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
Expand All @@ -26,6 +28,7 @@
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.FileContents;
import com.puppycrawl.tools.checkstyle.api.FileText;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;

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

private IndentationStyle indentationStyle;

private final Deque<TextBlockPair> textBlockPairs = new ArrayDeque<>();

@Override
public int[] getAcceptableTokens() {
return NO_REQUIRED_TOKENS;
return new int[] { TokenTypes.TEXT_BLOCK_LITERAL_BEGIN, TokenTypes.TEXT_BLOCK_LITERAL_END };
}

@Override
public void visitToken(DetailAST ast) {
super.visitToken(ast);
if (ast.getType() == TokenTypes.TEXT_BLOCK_LITERAL_BEGIN) {
this.textBlockPairs.add(new TextBlockPair(ast));
}
else if (ast.getType() == TokenTypes.TEXT_BLOCK_LITERAL_END) {
this.textBlockPairs.getLast().end(ast);
}
}

@Override
public void beginTree(DetailAST rootAST) {
super.beginTree(rootAST);
this.textBlockPairs.clear();
}

@Override
public void finishTree(DetailAST rootAST) {
FileContents fileContents = getFileContents();
FileText fileText = fileContents.getText();
File file = fileText.getFile();
Expand All @@ -66,8 +87,11 @@ public void beginTree(DetailAST rootAST) {
IndentationStyle indentationStyle = (this.indentationStyle != null) ? this.indentationStyle
: JavaFormatConfig.findFrom(file.getParentFile()).getIndentationStyle();
for (int i = 0; i < fileText.size(); i++) {
String line = fileText.get(i);
int lineNo = i + 1;
if (isInTextBlock(lineNo)) {
continue;
}
String line = fileText.get(i);
Matcher matcher = PATTERN.matcher(line);
boolean found = matcher.find(0);
while (found
Expand All @@ -78,11 +102,36 @@ public void beginTree(DetailAST rootAST) {
log(lineNo, "leadingwhitespace.incorrect", indentationStyle.toString().toLowerCase());
}
}
super.finishTree(rootAST);
}

private boolean isInTextBlock(int lineNo) {
return this.textBlockPairs.stream().anyMatch((textBlockPair) -> textBlockPair.contains(lineNo));
}

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

private static class TextBlockPair {

private final DetailAST begin;

private DetailAST end;

TextBlockPair(DetailAST begin) {
this.begin = begin;
}

public boolean contains(int lineNo) {
return (lineNo > this.begin.getLineNo()) && (lineNo <= this.end.getLineNo());
}

void end(DetailAST end) {
this.end = end;
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
+0 errors
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2017-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Leading whitepace with a text block.
*
* @author Phillip Webb
*/
public class LeadingWhitespaceTabsAndTextBlock {

/**
* Comments are ignored.
*/
public void hello() {
System.out.println(""""
Hello
World!""");
}

}

0 comments on commit 30ce469

Please sign in to comment.