Skip to content
Permalink
Browse files

Clang-format: Add Whitesmiths indentation style

Summary:
This patch adds support for the Whitesmiths indentation style to clang-format. It’s an update to a patch submitted in 2015 (D6833), but reworks it to use the newer API.

There are still some issues with this patch, primarily around `switch` and `case` support. The added unit test won’t currently pass because of the remaining issues.

Reviewers: mboehme, MyDeveloperDay, djasper

Reviewed By: MyDeveloperDay

Subscribers: krasimir, MyDeveloperDay, echristo, cfe-commits

Patch By: @timwoj (Tim Wojtulewicz)

Tags: #clang

Differential Revision: https://reviews.llvm.org/D67627

llvm-svn: 372497
  • Loading branch information
mydeveloperday committed Sep 22, 2019
1 parent 1b38002 commit a506ed256aeba5f3d71d1cf58f8d193444a3c694
@@ -1128,6 +1128,34 @@ the configuration (without a prefix: ``Auto``).
B
};

* ``BS_Whitesmiths`` (in configuration: ``Whitesmiths``)
Like ``Allman`` but always indent braces and line up code with braces.

.. code-block:: c++

try
{
foo();
}
catch ()
{
}
void foo() { bar(); }
class foo
{
};
if (foo())
{
}
else
{
}
enum X : int
{
A,
B
};

* ``BS_GNU`` (in configuration: ``GNU``)
Always break before braces and add an extra level of indentation to
braces of control statements, not to those of class, function
@@ -733,6 +733,32 @@ struct FormatStyle {
/// B
/// };
/// \endcode
BS_Whitesmiths,
/// Like ``Allman`` but always indent braces and line up code with braces.
/// \code
/// try
/// {
/// foo();
/// }
/// catch ()
/// {
/// }
/// void foo() { bar(); }
/// class foo
/// {
/// };
/// if (foo())
/// {
/// }
/// else
/// {
/// }
/// enum X : int
/// {
/// A,
/// B
/// };
/// \endcode
BS_GNU,
/// Like ``Attach``, but break before functions.
/// \code
@@ -931,6 +931,11 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
return std::max(State.Stack.back().LastSpace,
State.Stack.back().Indent + Style.ContinuationIndentWidth);

if (Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths &&
State.Line->First->is(tok::kw_enum))
return (Style.IndentWidth * State.Line->First->IndentLevel) +
Style.IndentWidth;

if (NextNonComment->is(tok::l_brace) && NextNonComment->BlockKind == BK_Block)
return Current.NestingLevel == 0 ? State.FirstIndent
: State.Stack.back().Indent;
@@ -165,6 +165,7 @@ template <> struct ScalarEnumerationTraits<FormatStyle::BraceBreakingStyle> {
IO.enumCase(Value, "Mozilla", FormatStyle::BS_Mozilla);
IO.enumCase(Value, "Stroustrup", FormatStyle::BS_Stroustrup);
IO.enumCase(Value, "Allman", FormatStyle::BS_Allman);
IO.enumCase(Value, "Whitesmiths", FormatStyle::BS_Whitesmiths);
IO.enumCase(Value, "GNU", FormatStyle::BS_GNU);
IO.enumCase(Value, "WebKit", FormatStyle::BS_WebKit);
IO.enumCase(Value, "Custom", FormatStyle::BS_Custom);
@@ -657,6 +658,19 @@ static FormatStyle expandPresets(const FormatStyle &Style) {
Expanded.BraceWrapping.BeforeCatch = true;
Expanded.BraceWrapping.BeforeElse = true;
break;
case FormatStyle::BS_Whitesmiths:
Expanded.BraceWrapping.AfterCaseLabel = true;
Expanded.BraceWrapping.AfterClass = true;
Expanded.BraceWrapping.AfterControlStatement = true;
Expanded.BraceWrapping.AfterEnum = true;
Expanded.BraceWrapping.AfterFunction = true;
Expanded.BraceWrapping.AfterNamespace = true;
Expanded.BraceWrapping.AfterObjCDeclaration = true;
Expanded.BraceWrapping.AfterStruct = true;
Expanded.BraceWrapping.AfterExternBlock = true;
Expanded.BraceWrapping.BeforeCatch = true;
Expanded.BraceWrapping.BeforeElse = true;
break;
case FormatStyle::BS_GNU:
Expanded.BraceWrapping = {true, true, true, true, true, true, true, true,
true, true, true, true, true, true, true, true};
@@ -1193,6 +1193,12 @@ void UnwrappedLineFormatter::formatFirstToken(
if (Newlines)
Indent = NewlineIndent;

// If in Whitemsmiths mode, indent start and end of blocks
if (Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths) {
if (RootToken.isOneOf(tok::l_brace, tok::r_brace, tok::kw_case))
Indent += Style.IndentWidth;
}

// Preprocessor directives get indented before the hash only if specified
if (Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash &&
(Line.Type == LT_PreprocessorDirective ||

0 comments on commit a506ed2

Please sign in to comment.
You can’t perform that action at this time.