Skip to content

[clang-format] Handle Java text blocks #141334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions clang/lib/Format/FormatTokenLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,36 @@ bool FormatTokenLexer::canPrecedeRegexLiteral(FormatToken *Prev) {
return true;
}

void FormatTokenLexer::tryParseJavaTextBlock() {
if (FormatTok->TokenText != "\"\"")
return;

const auto *S = Lex->getBufferLocation();
const auto *End = Lex->getBuffer().end();

if (S == End || *S != '\"')
return;

++S; // Skip the `"""` that begins a text block.

// Find the `"""` that ends the text block.
for (int Count = 0; Count < 3 && S < End; ++S) {
switch (*S) {
case '\\':
Count = -1;
break;
case '\"':
++Count;
break;
default:
Count = 0;
}
}

// Ignore the possibly invalid text block.
resetLexer(SourceMgr.getFileOffset(Lex->getSourceLocation(S)));
}

// Tries to parse a JavaScript Regex literal starting at the current token,
// if that begins with a slash and is in a location where JavaScript allows
// regex literals. Changes the current token to a regex literal and updates
Expand Down Expand Up @@ -1374,6 +1404,8 @@ FormatToken *FormatTokenLexer::getNextToken() {
FormatTok->TokenText = FormatTok->TokenText.substr(0, 1);
++Column;
StateStack.push(LexerState::TOKEN_STASHED);
} else if (Style.isJava() && FormatTok->is(tok::string_literal)) {
tryParseJavaTextBlock();
}

if (Style.isVerilog() && Tokens.size() > 0 &&
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Format/FormatTokenLexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class FormatTokenLexer {

bool canPrecedeRegexLiteral(FormatToken *Prev);

void tryParseJavaTextBlock();

// Tries to parse a JavaScript Regex literal starting at the current token,
// if that begins with a slash and is in a location where JavaScript allows
// regex literals. Changes the current token to a regex literal and updates
Expand Down
57 changes: 57 additions & 0 deletions clang/unittests/Format/FormatTestJava.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,63 @@ TEST_F(FormatTestJava, AlignCaseArrows) {
Style);
}

TEST_F(FormatTestJava, TextBlock) {
verifyNoChange("String myStr = \"\"\"\n"
"hello\n"
"there\n"
"\"\"\";");

verifyNoChange("String tb = \"\"\"\n"
" the new\"\"\";");

verifyNoChange("System.out.println(\"\"\"\n"
" This is the first line\n"
" This is the second line\n"
" \"\"\");");

verifyNoChange("void writeHTML() {\n"
" String html = \"\"\" \n"
" <html>\n"
" <p>Hello World.</p>\n"
" </html>\n"
"\"\"\";\n"
" writeOutput(html);\n"
"}");

verifyNoChange("String colors = \"\"\"\t\n"
" red\n"
" green\n"
" blue\"\"\".indent(4);");

verifyNoChange("String code = \"\"\"\n"
" String source = \\\"\"\"\n"
" String message = \"Hello, World!\";\n"
" System.out.println(message);\n"
" \\\"\"\";\n"
" \"\"\";");

verifyNoChange(
"class Outer {\n"
" void printPoetry() {\n"
" String lilacs = \"\"\"\n"
"Passing the apple-tree blows of white and pink in the orchards\n"
"\"\"\";\n"
" System.out.println(lilacs);\n"
" }\n"
"}");

verifyNoChange("String name = \"\"\"\r\n"
" red\n"
" green\n"
" blue\\\n"
" \"\"\";");

verifyFormat("String name = \"\"\"Pat Q. Smith\"\"\";");

verifyNoChange("String name = \"\"\"\n"
" Pat Q. Smith");
}

} // namespace
} // namespace test
} // namespace format
Expand Down
Loading