[clang] 8fa743a - [clang-format] C# property formatting can be controlled by config options

Jonathan Coe via cfe-commits cfe-commits at lists.llvm.org
Fri May 15 06:21:16 PDT 2020


Author: Jonathan Coe
Date: 2020-05-15T14:08:40+01:00
New Revision: 8fa743ab82027da443bac050e86b70bcdb78cbee

URL: https://github.com/llvm/llvm-project/commit/8fa743ab82027da443bac050e86b70bcdb78cbee
DIFF: https://github.com/llvm/llvm-project/commit/8fa743ab82027da443bac050e86b70bcdb78cbee.diff

LOG: [clang-format] C# property formatting can be controlled by config options

Summary:
Allow brace wrapping in C# property accessors to be controlled by configuration options.

Add new tests and revert old test results for Microsoft style to their previous state (as intended).

`FormatStyle.BraceWrapping.AfterFunction = true;` will change automatic property formatting from

```
Type MyType { get; set }
```

to

```
Type MyType
{ get; set }
```

Reviewers: krasimir, MyDeveloperDay

Reviewed By: krasimir, MyDeveloperDay

Subscribers: cfe-commits

Tags: #clang-format, #clang

Differential Revision: https://reviews.llvm.org/D79000

Added: 
    

Modified: 
    clang/lib/Format/UnwrappedLineParser.cpp
    clang/unittests/Format/FormatTestCSharp.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index b303758f1cb8..58206a06ea1a 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -1531,6 +1531,8 @@ bool UnwrappedLineParser::tryToParsePropertyAccessor() {
   // Try to parse the property accessor:
   // https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties
   Tokens->setPosition(StoredPosition);
+  if (Style.BraceWrapping.AfterFunction == true)
+    addUnwrappedLine();
   nextToken();
   do {
     switch (FormatTok->Tok.getKind()) {

diff  --git a/clang/unittests/Format/FormatTestCSharp.cpp b/clang/unittests/Format/FormatTestCSharp.cpp
index 6f0b1966767d..5567e19e5bdd 100644
--- a/clang/unittests/Format/FormatTestCSharp.cpp
+++ b/clang/unittests/Format/FormatTestCSharp.cpp
@@ -245,11 +245,13 @@ TEST_F(FormatTestCSharp, Attributes) {
                "}");
 
   verifyFormat("[TestMethod]\n"
-               "public string Host { set; get; }");
+               "public string Host\n"
+               "{ set; get; }");
 
   verifyFormat("[TestMethod(\"start\", HelpText = \"Starts the server "
                "listening on provided host\")]\n"
-               "public string Host { set; get; }");
+               "public string Host\n"
+               "{ set; get; }");
 
   verifyFormat(
       "[DllImport(\"Hello\", EntryPoint = \"hello_world\")]\n"
@@ -675,6 +677,32 @@ class MyClass {
     set => veryLongNamedField = value;
   } = VeryLongNamedTypeIndeed.Create(DefaultFirstArgument, DefaultSecondArgument,
                                      DefaultThirdArgument);
+})",
+               Style);
+
+  // Brace wrapping and single-lining of accessor can be controlled by config.
+  Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
+  Style.BreakBeforeBraces = FormatStyle::BS_Custom;
+  Style.BraceWrapping.AfterFunction = true;
+
+  verifyFormat(R"(//
+public class SaleItem {
+  public decimal Price
+  { get; set; }
+})",
+               Style);
+
+  verifyFormat(R"(//
+class TimePeriod {
+  public double Hours
+  {
+    get {
+      return _seconds / 3600;
+    }
+    set {
+      _seconds = value * 3600;
+    }
+  }
 })",
                Style);
 }


        


More information about the cfe-commits mailing list