Skip to content

Instantly share code, notes, and snippets.

@markbenvenuto
Created January 25, 2016 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markbenvenuto/1c509bd79c150539317d to your computer and use it in GitHub Desktop.
Save markbenvenuto/1c509bd79c150539317d to your computer and use it in GitHub Desktop.
Clang-Format patch
Adds new option
- ElseNewLine - to get the else behavior we want
- New indentation for namespaces
Index: include/clang/Format/Format.h
===================================================================
--- include/clang/Format/Format.h (revision 229164)
+++ include/clang/Format/Format.h (working copy)
@@ -139,6 +139,8 @@
NI_None,
/// Indent only in inner namespaces (nested in other namespaces).
NI_Inner,
+ /// Indent once for namespaces except an additional one for anonymous
+ NI_Mongo,
/// Indent in all namespaces.
NI_All
};
@@ -251,6 +253,10 @@
/// Otherwise puts them into the right-most column.
bool AlignEscapedNewlinesLeft;
+ /// \brief If \c true, always adds new line break after else statement
+ /// Otherwise defaults to BraceBreakingStyle
+ bool ElseNewLine;
+
/// \brief The number of columns to use for indentation.
unsigned IndentWidth;
@@ -441,6 +447,7 @@
ConstructorInitializerIndentWidth ==
R.ConstructorInitializerIndentWidth &&
DerivePointerAlignment == R.DerivePointerAlignment &&
+ ElseNewLine == R.ElseNewLine &&
ExperimentalAutoDetectBinPacking ==
R.ExperimentalAutoDetectBinPacking &&
IndentCaseLabels == R.IndentCaseLabels &&
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp (revision 229164)
+++ lib/Format/Format.cpp (working copy)
@@ -105,6 +105,7 @@
FormatStyle::NamespaceIndentationKind &Value) {
IO.enumCase(Value, "None", FormatStyle::NI_None);
IO.enumCase(Value, "Inner", FormatStyle::NI_Inner);
+ IO.enumCase(Value, "Mongo", FormatStyle::NI_Mongo);
IO.enumCase(Value, "All", FormatStyle::NI_All);
}
};
@@ -208,6 +209,7 @@
IO.mapOptional("ConstructorInitializerIndentWidth",
Style.ConstructorInitializerIndentWidth);
IO.mapOptional("DerivePointerAlignment", Style.DerivePointerAlignment);
+ IO.mapOptional("ElseNewLine", Style.ElseNewLine);
IO.mapOptional("ExperimentalAutoDetectBinPacking",
Style.ExperimentalAutoDetectBinPacking);
IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels);
Index: lib/Format/UnwrappedLineParser.cpp
===================================================================
--- lib/Format/UnwrappedLineParser.cpp (revision 229164)
+++ lib/Format/UnwrappedLineParser.cpp (working copy)
@@ -1135,7 +1135,8 @@
--Line->Level;
}
if (FormatTok->Tok.is(tok::kw_else)) {
- if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
+ if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup ||
+ Style.ElseNewLine)
addUnwrappedLine();
nextToken();
if (FormatTok->Tok.is(tok::l_brace)) {
@@ -1247,12 +1248,24 @@
bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
(Style.NamespaceIndentation == FormatStyle::NI_Inner &&
DeclarationScopeStack.size() > 1);
- parseBlock(/*MustBeDeclaration=*/true, AddLevel);
+
+ unsigned InitialLevel = Line->Level;
+
+ if(Style.NamespaceIndentation == FormatStyle::NI_Mongo) {
+ Line->Level = 0;
+
+ parseBlock(/*MustBeDeclaration=*/true, true);
+ }
+ else
+ parseBlock(/*MustBeDeclaration=*/true, AddLevel);
+
// Munch the semicolon after a namespace. This is more common than one would
// think. Puttin the semicolon into its own line is very ugly.
if (FormatTok->Tok.is(tok::semi))
nextToken();
addUnwrappedLine();
+
+ Line->Level = InitialLevel;
}
// FIXME: Add error handling.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment