Index: clang/lib/Format/UnwrappedLineParser.cpp =================================================================== --- clang/lib/Format/UnwrappedLineParser.cpp +++ clang/lib/Format/UnwrappedLineParser.cpp @@ -2001,9 +2001,15 @@ } break; case tok::equal: - if (Style.isCSharp() && FormatTok->is(TT_FatArrow)) - parseStructuralElement(); - else + if (Style.isCSharp() && FormatTok->is(TT_FatArrow)) { + nextToken(); + if (FormatTok->is(tok::l_brace)) { + if (Style.isCSharp() && Style.BraceWrapping.AfterFunction == true) { + FormatTok->MustBreakBefore = true; + } + parseChildBlock(); + } + } else nextToken(); break; case tok::kw_class: Index: clang/unittests/Format/FormatTestCSharp.cpp =================================================================== --- clang/unittests/Format/FormatTestCSharp.cpp +++ clang/unittests/Format/FormatTestCSharp.cpp @@ -759,6 +759,128 @@ GoogleStyle); } +TEST_F(FormatTestCSharp, CSharpLambdasDontBreakFollowingCodeAlignment) { + FormatStyle GoogleStyle = getGoogleStyle(FormatStyle::LK_CSharp); + FormatStyle MicrosoftStyle = getMicrosoftStyle(FormatStyle::LK_CSharp); + + verifyFormat(R"(// +public class Sample { + public void Test() { + while (true) { + preBindEnumerators.RemoveAll(enumerator => !enumerator.MoveNext()); + CodeThatFollowsLambda(); + IsWellAligned(); + } + } +})", + GoogleStyle); + + verifyFormat(R"(// +public class Sample +{ + public void Test() + { + while (true) + { + preBindEnumerators.RemoveAll(enumerator => !enumerator.MoveNext()); + CodeThatFollowsLambda(); + IsWellAligned(); + } + } +})", + MicrosoftStyle); +} + +TEST_F(FormatTestCSharp, CSharpLambdasComplexLambdasDontBreakAlignment) { + FormatStyle GoogleStyle = getGoogleStyle(FormatStyle::LK_CSharp); + FormatStyle MicrosoftStyle = getMicrosoftStyle(FormatStyle::LK_CSharp); + + verifyFormat(R"(// +public class Test +{ + private static void ComplexLambda(BuildReport protoReport) + { + allSelectedScenes = + veryVeryLongCollectionNameThatPutsTheLineLenghtAboveTheThresholds.Where(scene => scene.enabled) + .Select(scene => scene.path) + .ToArray(); + if (allSelectedScenes.Count == 0) + { + return; + } + Functions(); + AreWell(); + Aligned(); + AfterLambdaBlock(); + } +})", + MicrosoftStyle); + + verifyFormat(R"(// +public class Test { + private static void ComplexLambda(BuildReport protoReport) { + allSelectedScenes = veryVeryLongCollectionNameThatPutsTheLineLenghtAboveTheThresholds + .Where(scene => scene.enabled) + .Select(scene => scene.path) + .ToArray(); + if (allSelectedScenes.Count == 0) { + return; + } + Functions(); + AreWell(); + Aligned(); + AfterLambdaBlock(); + } +})", + GoogleStyle); +} + +TEST_F(FormatTestCSharp, CSharpLambdasMulipleLambdasDontBreakAlignment) { + FormatStyle GoogleStyle = getGoogleStyle(FormatStyle::LK_CSharp); + FormatStyle MicrosoftStyle = getMicrosoftStyle(FormatStyle::LK_CSharp); + + verifyFormat(R"(// +public class Test +{ + private static void MultipleLambdas(BuildReport protoReport) + { + allSelectedScenes = + veryVeryLongCollectionNameThatPutsTheLineLenghtAboveTheThresholds.Where(scene => scene.enabled) + .Select(scene => scene.path) + .ToArray(); + preBindEnumerators.RemoveAll(enumerator => !enumerator.MoveNext()); + if (allSelectedScenes.Count == 0) + { + return; + } + Functions(); + AreWell(); + Aligned(); + AfterLambdaBlock(); + } +})", + MicrosoftStyle); + + verifyFormat(R"(// +public class Test { + private static void MultipleLambdas(BuildReport protoReport) { + allSelectedScenes = veryVeryLongCollectionNameThatPutsTheLineLenghtAboveTheThresholds + .Where(scene => scene.enabled) + .Select(scene => scene.path) + .ToArray(); + preBindEnumerators.RemoveAll(enumerator => !enumerator.MoveNext()); + if (allSelectedScenes.Count == 0) { + return; + } + Functions(); + AreWell(); + Aligned(); + AfterLambdaBlock(); + } +})", + GoogleStyle); +} + TEST_F(FormatTestCSharp, CSharpObjectInitializers) { FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp);