Skip to content

Commit 0bb60e2

Browse files
author
Jonathan Coe
committed
[clang-format] Fixes for spaces around C# object initializers
Summary: Fix spaces around typename and [] in C# object initializers. Reviewers: MyDeveloperDay, klimek, krasimir Reviewed By: MyDeveloperDay, krasimir Subscribers: cfe-commits Tags: #clang-format, #clang Differential Revision: https://reviews.llvm.org/D72401
1 parent 789beee commit 0bb60e2

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2873,6 +2873,13 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
28732873
if (Left.is(tok::kw_using))
28742874
return Style.SpaceBeforeParens == FormatStyle::SBPO_ControlStatements ||
28752875
spaceRequiredBeforeParens(Right);
2876+
// space between ']' and '{'
2877+
if (Left.is(tok::r_square) && Right.is(tok::l_brace))
2878+
return true;
2879+
// space before '{' in "new MyType {"
2880+
if (Right.is(tok::l_brace) && Left.Previous &&
2881+
Left.Previous->is(tok::kw_new))
2882+
return true;
28762883
} else if (Style.Language == FormatStyle::LK_JavaScript) {
28772884
if (Left.is(TT_JsFatArrow))
28782885
return true;

clang/unittests/Format/FormatTestCSharp.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,5 +457,34 @@ var x = foo(className, $@"some code:
457457
EXPECT_EQ(Code, format(Code, Style));
458458
}
459459

460+
TEST_F(FormatTestCSharp, CSharpObjectInitializers) {
461+
FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp);
462+
463+
// Start code fragemnts with a comment line so that C++ raw string literals
464+
// as seen are identical to expected formatted code.
465+
466+
verifyFormat(R"(//
467+
Shape[] shapes = new[] {
468+
new Circle {
469+
Radius = 2.7281,
470+
Colour = Colours.Red,
471+
},
472+
new Square {
473+
Side = 101.1,
474+
Colour = Colours.Yellow,
475+
},
476+
};)",
477+
Style);
478+
479+
// Omitted final `,`s will change the formatting.
480+
verifyFormat(R"(//
481+
Shape[] shapes = new[] {new Circle {Radius = 2.7281, Colour = Colours.Red},
482+
new Square {
483+
Side = 101.1,
484+
Colour = Colours.Yellow,
485+
}};)",
486+
Style);
487+
}
488+
460489
} // namespace format
461490
} // end namespace clang

0 commit comments

Comments
 (0)