diff options
author | Owen Pan <owenpiano@gmail.com> | 2025-04-29 19:22:53 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-04-29 19:22:53 -0700 |
commit | b8bb1ccb4f9126d1bc9817be24e17f186a75a08b (patch) | |
tree | 081a4bd5a0cfb0e337a235c720dd7120c6dd85b3 /clang/lib/Format/FormatTokenLexer.cpp | |
parent | f1750300aad0e49383cd4b206e2354f1300a40a8 (diff) | |
download | llvm-b8bb1ccb4f9126d1bc9817be24e17f186a75a08b.zip llvm-b8bb1ccb4f9126d1bc9817be24e17f186a75a08b.tar.gz llvm-b8bb1ccb4f9126d1bc9817be24e17f186a75a08b.tar.bz2 |
[clang-format] Add OneLineFormatOffRegex option (#137577)
Close #54334
Diffstat (limited to 'clang/lib/Format/FormatTokenLexer.cpp')
-rw-r--r-- | clang/lib/Format/FormatTokenLexer.cpp | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp index a4c94ac..bff9db2 100644 --- a/clang/lib/Format/FormatTokenLexer.cpp +++ b/clang/lib/Format/FormatTokenLexer.cpp @@ -83,8 +83,43 @@ FormatTokenLexer::FormatTokenLexer( ArrayRef<FormatToken *> FormatTokenLexer::lex() { assert(Tokens.empty()); assert(FirstInLineIndex == 0); + const llvm::Regex FormatOffRegex(Style.OneLineFormatOffRegex); + enum { FO_None, FO_CurrentLine, FO_NextLine } FormatOff = FO_None; do { Tokens.push_back(getNextToken()); + auto &Tok = *Tokens.back(); + const auto NewlinesBefore = Tok.NewlinesBefore; + switch (FormatOff) { + case FO_CurrentLine: + if (NewlinesBefore == 0) + Tok.Finalized = true; + else + FormatOff = FO_None; + break; + case FO_NextLine: + if (NewlinesBefore > 1) { + FormatOff = FO_None; + } else { + Tok.Finalized = true; + FormatOff = FO_CurrentLine; + } + break; + default: + if (!FormattingDisabled && FormatOffRegex.match(Tok.TokenText)) { + if (Tok.is(tok::comment) && + (NewlinesBefore > 0 || &Tok == Tokens.front())) { + Tok.Finalized = true; + FormatOff = FO_NextLine; + } else { + for (auto *Token : reverse(Tokens)) { + Token->Finalized = true; + if (Token->NewlinesBefore > 0) + break; + } + FormatOff = FO_CurrentLine; + } + } + } if (Style.isJavaScript()) { tryParseJSRegexLiteral(); handleTemplateStrings(); |