MyDeveloperDay created this revision. MyDeveloperDay added reviewers: owenpan, HazardyKnusperkeks, rymiel. MyDeveloperDay added a project: clang-format. Herald added projects: All, clang. MyDeveloperDay requested review of this revision.
This change allows always breaking before braces on C# setter and getter properties Fixes #61968 https://github.com/llvm/llvm-project/issues/61968 Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D148467 Files: clang/docs/ClangFormatStyleOptions.rst clang/docs/ReleaseNotes.rst clang/include/clang/Format/Format.h clang/lib/Format/Format.cpp clang/lib/Format/TokenAnnotator.cpp clang/unittests/Format/FormatTestCSharp.cpp
Index: clang/unittests/Format/FormatTestCSharp.cpp =================================================================== --- clang/unittests/Format/FormatTestCSharp.cpp +++ clang/unittests/Format/FormatTestCSharp.cpp @@ -1604,5 +1604,51 @@ EXPECT_NE("", format("int where b <")); // reduced from crasher } +TEST_F(FormatTestCSharp, PropertyWrapping) { + FormatStyle Style = getMicrosoftStyle(FormatStyle::LK_CSharp); + + Style.BraceWrapping.AfterCSharpProperty = false; + verifyFormat("class A\n" + "{\n" + " string Foo { set; get; }\n" + "}\n", + Style); + verifyFormat("class A\n" + "{\n" + " string Foo\n" + " {\n" + " set {\n" + " val = value;\n" + " }\n" + " get {\n" + " return value;\n" + " }\n" + " }\n" + "}\n", + Style); + Style.BraceWrapping.AfterCSharpProperty = true; + + verifyFormat("class A\n" + "{\n" + " string Foo { set; get }\n" + "}\n", + Style); + verifyFormat("class A\n" + "{\n" + " string Foo\n" + " {\n" + " set\n" + " {\n" + " val = value;\n" + " }\n" + " get\n" + " {\n" + " return value;\n" + " }\n" + " }\n" + "}\n", + Style); +} + } // namespace format } // end namespace clang Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -4984,6 +4984,12 @@ return true; } + if (Style.isCSharp() && Style.BraceWrapping.AfterCSharpProperty && + (Line.startsWith(Keywords.kw_get) || + Line.startsWith(Keywords.kw_set))) { + return true; + } + // Don't attempt to interpret struct return types as structs. if (Right.isNot(TT_FunctionLBrace)) { return (Line.startsWith(tok::kw_class) && Index: clang/lib/Format/Format.cpp =================================================================== --- clang/lib/Format/Format.cpp +++ clang/lib/Format/Format.cpp @@ -181,6 +181,7 @@ IO.mapOptional("AfterEnum", Wrapping.AfterEnum); IO.mapOptional("AfterExternBlock", Wrapping.AfterExternBlock); IO.mapOptional("AfterFunction", Wrapping.AfterFunction); + IO.mapOptional("AfterCSharpProperty", Wrapping.AfterCSharpProperty); IO.mapOptional("AfterNamespace", Wrapping.AfterNamespace); IO.mapOptional("AfterObjCDeclaration", Wrapping.AfterObjCDeclaration); IO.mapOptional("AfterStruct", Wrapping.AfterStruct); @@ -1188,7 +1189,8 @@ /*IndentBraces=*/false, /*SplitEmptyFunction=*/true, /*SplitEmptyRecord=*/true, - /*SplitEmptyNamespace=*/true}; + /*SplitEmptyNamespace=*/true, + /*AfterCSharpProperty=*/false}; switch (Expanded.BreakBeforeBraces) { case FormatStyle::BS_Linux: Expanded.BraceWrapping.AfterClass = true; @@ -1354,7 +1356,8 @@ /*IndentBraces=*/false, /*SplitEmptyFunction=*/true, /*SplitEmptyRecord=*/true, - /*SplitEmptyNamespace=*/true}; + /*SplitEmptyNamespace=*/true, + /*AfterCSharpProperty=*/false}; LLVMStyle.BreakAfterAttributes = FormatStyle::ABS_Never; LLVMStyle.BreakAfterJavaFieldAnnotations = false; LLVMStyle.BreakArrays = true; Index: clang/include/clang/Format/Format.h =================================================================== --- clang/include/clang/Format/Format.h +++ clang/include/clang/Format/Format.h @@ -1205,6 +1205,19 @@ /// \endcode /// bool SplitEmptyNamespace; + /// Wrap C# setter/getter definitions. + /// \code + /// true: + /// get + /// {} + /// set + /// {} + /// + /// false: + /// get {} + /// set {} + /// \endcode + bool AfterGetSet; }; /// Control of individual brace wrapping cases. Index: clang/docs/ReleaseNotes.rst =================================================================== --- clang/docs/ReleaseNotes.rst +++ clang/docs/ReleaseNotes.rst @@ -461,6 +461,8 @@ - Add additional Qualifier Ordering support for special cases such as templates, requires clauses, long qualified names. - Fix all known issues associated with ``LambdaBodyIndentation: OuterScope``. +- Add ``AfterCSharpProperty`` style to option ``BraceWrapping`` to allow + newline before the opening brace of ``set;get;``. libclang -------- Index: clang/docs/ClangFormatStyleOptions.rst =================================================================== --- clang/docs/ClangFormatStyleOptions.rst +++ clang/docs/ClangFormatStyleOptions.rst @@ -1828,6 +1828,20 @@ {} { } + * ``bool AfterGetSet`` Wrap C# setter/getter definitions. + + .. code-block:: c++ + + true: + get + {} + set + {} + + false: + get {} + set {} + .. _BreakAfterAttributes:
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits