Index: clang/lib/Format/UnwrappedLineFormatter.cpp =================================================================== --- clang/lib/Format/UnwrappedLineFormatter.cpp +++ clang/lib/Format/UnwrappedLineFormatter.cpp @@ -292,6 +292,13 @@ } } + // Try to merge a CSharp property declaration like `{ get; private set }`. + if (Style.isCSharp()) { + unsigned CSPA = tryMergeCSharpPropertyAccessor(I, E, Limit); + if (CSPA > 0) + return CSPA; + } + // Try to merge a function block with left brace unwrapped if (TheLine->Last->is(TT_FunctionLBrace) && TheLine->First != TheLine->Last) { @@ -421,6 +428,47 @@ return 0; } + unsigned tryMergeCSharpPropertyAccessor( + SmallVectorImpl::const_iterator I, + SmallVectorImpl::const_iterator E, unsigned /*Limit*/) { + // Does line start with `{` + if (I[0]->Last->isNot(TT_FunctionLBrace)) + return 0; + + // Does line start with `[access-modifier] get` + if (I + 1 == E) + return 0; + auto *GetToken = I[1]->First; + if (GetToken && + GetToken->isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private)) + GetToken = GetToken->Next; + if (!GetToken || GetToken->TokenText != "get") + return 0; + + // Does line start with `[access-modifier] set` or `{` + if (I + 2 == E) + return 0; + auto *SetToken = I[2]->First; + if (SetToken && + SetToken->isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private)) + SetToken = SetToken->Next; + + if (SetToken && SetToken->is(tok::r_brace)) + return 2; + + if (!SetToken || SetToken->TokenText != "set") + return 0; + + // Does line start with `}` + if (I + 3 == E) + return 0; + auto *RBrace = I[3]->First; + if (!RBrace || !RBrace->is(tok::r_brace)) + return 0; + + return 3; + } + unsigned tryMergeSimplePPDirective(SmallVectorImpl::const_iterator I, SmallVectorImpl::const_iterator E, Index: clang/unittests/Format/FormatTestCSharp.cpp =================================================================== --- clang/unittests/Format/FormatTestCSharp.cpp +++ clang/unittests/Format/FormatTestCSharp.cpp @@ -554,5 +554,16 @@ Style); } +TEST_F(FormatTestCSharp, CSharpPropertyAccessors) { + FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp); + + verifyFormat(R"(// +int Value { get; set } +string Name { get; private set } +string AnotherName { protected get; private set } +double Sum { get })", + Style); +} + } // namespace format } // end namespace clang