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

Jonathan B Coe via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 28 06:24:18 PDT 2020


jbcoe created this revision.
jbcoe added reviewers: krasimir, MyDeveloperDay.
jbcoe added a project: clang-format.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

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

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D79000

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


Index: clang/unittests/Format/FormatTestCSharp.cpp
===================================================================
--- clang/unittests/Format/FormatTestCSharp.cpp
+++ clang/unittests/Format/FormatTestCSharp.cpp
@@ -245,11 +245,13 @@
                "}");
 
   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"
@@ -669,6 +671,32 @@
     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);
 }
Index: clang/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineParser.cpp
+++ clang/lib/Format/UnwrappedLineParser.cpp
@@ -1531,6 +1531,8 @@
   // 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()) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D79000.260610.patch
Type: text/x-patch
Size: 2116 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200428/9a27e3b2/attachment-0001.bin>


More information about the cfe-commits mailing list