diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -2975,6 +2975,11 @@ if (Style.isCSharp()) { do { + // Handle constructor invocation, e.g. `new(field: value)`. + if (FormatTok->is(tok::l_paren)) + parseParens(); + + // Handle array initialization syntax, e.g. `new[] {10, 20, 30}`. if (FormatTok->is(tok::l_brace)) parseBracedList(); diff --git a/clang/unittests/Format/FormatTestCSharp.cpp b/clang/unittests/Format/FormatTestCSharp.cpp --- a/clang/unittests/Format/FormatTestCSharp.cpp +++ b/clang/unittests/Format/FormatTestCSharp.cpp @@ -617,6 +617,24 @@ EXPECT_EQ(Code, format(Code, Style)); } +TEST_F(FormatTestCSharp, CSharpNewOperator) { + FormatStyle Style = getLLVMStyle(FormatStyle::LK_CSharp); + + verifyFormat("public void F() {\n" + " var v = new C(() => { var t = 5; });\n" + "}", + Style); + verifyFormat("public void F() {\n" + " var v = new C(() => {\n" + " try {\n" + " } catch {\n" + " var t = 5;\n" + " }\n" + " });\n" + "}", + Style); +} + TEST_F(FormatTestCSharp, CSharpLambdas) { FormatStyle GoogleStyle = getGoogleStyle(FormatStyle::LK_CSharp); FormatStyle MicrosoftStyle = getMicrosoftStyle(FormatStyle::LK_CSharp);