Skip to content

Commit

Permalink
[clang-format] Correct indentation for [key] = value, entries in C+…
Browse files Browse the repository at this point in the history
…+ object initialisers

Summary: Do not use continuation indent for '[' in blocks in C# code.

Reviewers: krasimir

Reviewed By: krasimir

Subscribers: cfe-commits

Tags: #clang-format, #clang

Differential Revision: https://reviews.llvm.org/D75747
  • Loading branch information
jbcoe committed Mar 9, 2020
1 parent 4a1b6b0 commit cb3f20d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions clang/lib/Format/ContinuationIndenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,9 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
if (NextNonComment->is(TT_ArraySubscriptLSquare)) {
if (State.Stack.back().StartOfArraySubscripts != 0)
return State.Stack.back().StartOfArraySubscripts;
else if (Style.isCSharp()) // C# allows `["key"] = value` inside object
// initializers.
return State.Stack.back().Indent;
return ContinuationIndent;
}

Expand Down
9 changes: 9 additions & 0 deletions clang/unittests/Format/FormatTestCSharp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,15 @@ Shape[] shapes = new[] { new Circle { Radius = 2.7281, Colour = Colours.Red },
private Transformer _transformer = new X.Y {
Filler = (Shape shape) => { return new Transform.Fill(shape, RED); },
Scaler = (Shape shape) => { return new Transform.Resize(shape, 0.1); },
};)",
Style);

// Dictionary initialisation.
verifyFormat(R"(//
var myDict = new Dictionary<string, string> {
["name"] = _donald,
["age"] = Convert.ToString(DateTime.Today.Year - 1934),
["type"] = _duck,
};)",
Style);
}
Expand Down

0 comments on commit cb3f20d

Please sign in to comment.