Index: clang/lib/Format/UnwrappedLineParser.cpp =================================================================== --- clang/lib/Format/UnwrappedLineParser.cpp +++ clang/lib/Format/UnwrappedLineParser.cpp @@ -2001,10 +2001,16 @@ } 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.BraceWrapping.AfterFunction) + FormatTok->MustBreakBefore = true; + parseChildBlock(); + } + } else { nextToken(); + } break; case tok::kw_class: if (Style.Language == FormatStyle::LK_JavaScript) 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);