Skip to content

Instantly share code, notes, and snippets.

@ekpyron
Last active August 17, 2024 18:34
Show Gist options
  • Save ekpyron/07d3796f27527b41784546df20200fda to your computer and use it in GitHub Desktop.
Save ekpyron/07d3796f27527b41784546df20200fda to your computer and use it in GitHub Desktop.
AlwaysBlockIndent for clang-format
diff -Naur clang-18.1.8.src.orig/include/clang/Format/Format.h clang-18.1.8.src/include/clang/Format/Format.h
--- clang-18.1.8.src.orig/include/clang/Format/Format.h 2024-06-15 19:21:32.000000000 +0200
+++ clang-18.1.8.src/include/clang/Format/Format.h 2024-08-16 02:23:04.288426013 +0200
@@ -97,6 +97,7 @@
/// ``Cpp11BracedListStyle`` is ``true``) and parentheses.
/// \endnote
BAS_BlockIndent,
+ BAS_AlwaysBlockIndent,
};
/// If ``true``, horizontally aligns arguments after an open bracket.
diff -Naur clang-18.1.8.src.orig/lib/Format/ContinuationIndenter.cpp clang-18.1.8.src/lib/Format/ContinuationIndenter.cpp
--- clang-18.1.8.src.orig/lib/Format/ContinuationIndenter.cpp 2024-06-15 19:21:32.000000000 +0200
+++ clang-18.1.8.src/lib/Format/ContinuationIndenter.cpp 2024-08-17 18:45:20.460776157 +0200
@@ -376,6 +376,11 @@
Style.ColumnLimit > 0)))) {
return true;
}
+ if (Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBlockIndent &&
+ Previous.is(tok::l_paren) &&
+ (getLengthToMatchingParen(Previous, State.Stack) + State.Column - 1 >
+ getColumnLimit(State)))
+ return true;
if (CurrentState.BreakBeforeClosingBrace &&
(Current.closesBlockOrBlockTypeList(Style) ||
(Current.is(tok::r_brace) &&
@@ -1156,7 +1161,8 @@
if (PreviousNonComment && PreviousNonComment->is(tok::l_paren)) {
CurrentState.BreakBeforeClosingParen =
- Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
+ (Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent ||
+ Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBlockIndent);
}
if (CurrentState.AvoidBinPacking) {
@@ -1274,7 +1280,8 @@
Current.Next->isOneOf(tok::semi, tok::kw_const, tok::l_brace))) {
return State.Stack[State.Stack.size() - 2].LastSpace;
}
- if (Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent &&
+ if ((Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent ||
+ Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBlockIndent) &&
(Current.is(tok::r_paren) ||
(Current.is(tok::r_brace) && Current.MatchingParen &&
Current.MatchingParen->is(BK_BracedInit))) &&
diff -Naur clang-18.1.8.src.orig/lib/Format/Format.cpp clang-18.1.8.src/lib/Format/Format.cpp
--- clang-18.1.8.src.orig/lib/Format/Format.cpp 2024-06-15 19:21:32.000000000 +0200
+++ clang-18.1.8.src/lib/Format/Format.cpp 2024-08-17 18:45:20.460776157 +0200
@@ -223,6 +223,7 @@
IO.enumCase(Value, "DontAlign", FormatStyle::BAS_DontAlign);
IO.enumCase(Value, "AlwaysBreak", FormatStyle::BAS_AlwaysBreak);
IO.enumCase(Value, "BlockIndent", FormatStyle::BAS_BlockIndent);
+ IO.enumCase(Value, "AlwaysBlockIndent", FormatStyle::BAS_AlwaysBlockIndent);
// For backward compatibility.
IO.enumCase(Value, "true", FormatStyle::BAS_Align);
diff -Naur clang-18.1.8.src.orig/lib/Format/FormatToken.cpp clang-18.1.8.src/lib/Format/FormatToken.cpp
--- clang-18.1.8.src.orig/lib/Format/FormatToken.cpp 2024-06-15 19:21:32.000000000 +0200
+++ clang-18.1.8.src/lib/Format/FormatToken.cpp 2024-08-17 18:45:20.460776157 +0200
@@ -78,7 +78,8 @@
bool FormatToken::isBlockIndentedInitRBrace(const FormatStyle &Style) const {
assert(is(tok::r_brace));
if (!Style.Cpp11BracedListStyle ||
- Style.AlignAfterOpenBracket != FormatStyle::BAS_BlockIndent) {
+ (Style.AlignAfterOpenBracket != FormatStyle::BAS_BlockIndent &&
+ Style.AlignAfterOpenBracket != FormatStyle::BAS_AlwaysBlockIndent)) {
return false;
}
const auto *LBrace = MatchingParen;
diff -Naur clang-18.1.8.src.orig/lib/Format/TokenAnnotator.cpp clang-18.1.8.src/lib/Format/TokenAnnotator.cpp
--- clang-18.1.8.src.orig/lib/Format/TokenAnnotator.cpp 2024-06-15 19:21:32.000000000 +0200
+++ clang-18.1.8.src/lib/Format/TokenAnnotator.cpp 2024-08-17 20:33:45.217013064 +0200
@@ -5678,10 +5678,13 @@
// We only break before r_paren if we're in a block indented context.
if (Right.is(tok::r_paren)) {
- if (Style.AlignAfterOpenBracket != FormatStyle::BAS_BlockIndent ||
+ if ((Style.AlignAfterOpenBracket != FormatStyle::BAS_BlockIndent &&
+ Style.AlignAfterOpenBracket != FormatStyle::BAS_AlwaysBlockIndent) ||
!Right.MatchingParen) {
return false;
}
+ if (Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBlockIndent)
+ return true;
auto Next = Right.Next;
if (Next && Next->is(tok::r_paren))
Next = Next->Next;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment