Skip to content

Commit

Permalink
[clang-format] Fixes for spaces around C# object initializers
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jbcoe committed Jan 31, 2020
1 parent 789beee commit 0bb60e2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
7 changes: 7 additions & 0 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2873,6 +2873,13 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
if (Left.is(tok::kw_using))
return Style.SpaceBeforeParens == FormatStyle::SBPO_ControlStatements ||
spaceRequiredBeforeParens(Right);
// space between ']' and '{'
if (Left.is(tok::r_square) && Right.is(tok::l_brace))
return true;
// space before '{' in "new MyType {"
if (Right.is(tok::l_brace) && Left.Previous &&
Left.Previous->is(tok::kw_new))
return true;
} else if (Style.Language == FormatStyle::LK_JavaScript) {
if (Left.is(TT_JsFatArrow))
return true;
Expand Down
29 changes: 29 additions & 0 deletions clang/unittests/Format/FormatTestCSharp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,5 +457,34 @@ var x = foo(className, $@"some code:
EXPECT_EQ(Code, format(Code, Style));
}

TEST_F(FormatTestCSharp, CSharpObjectInitializers) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp);

// Start code fragemnts with a comment line so that C++ raw string literals
// as seen are identical to expected formatted code.

verifyFormat(R"(//
Shape[] shapes = new[] {
new Circle {
Radius = 2.7281,
Colour = Colours.Red,
},
new Square {
Side = 101.1,
Colour = Colours.Yellow,
},
};)",
Style);

// Omitted final `,`s will change the formatting.
verifyFormat(R"(//
Shape[] shapes = new[] {new Circle {Radius = 2.7281, Colour = Colours.Red},
new Square {
Side = 101.1,
Colour = Colours.Yellow,
}};)",
Style);
}

} // namespace format
} // end namespace clang

0 comments on commit 0bb60e2

Please sign in to comment.