[clang] 5c917bd - [clang-format] No space in `new()` and `this[Type x]` in C#

Jonathan Coe via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 11 05:56:14 PDT 2020


Author: Jonathan Coe
Date: 2020-03-11T12:53:53Z
New Revision: 5c917bd9a7de8fc45401da00cd27661b429887e9

URL: https://github.com/llvm/llvm-project/commit/5c917bd9a7de8fc45401da00cd27661b429887e9
DIFF: https://github.com/llvm/llvm-project/commit/5c917bd9a7de8fc45401da00cd27661b429887e9.diff

LOG: [clang-format] No space in `new()` and `this[Type x]` in C#

Reviewers: krasimir

Reviewed By: krasimir

Subscribers: cfe-commits, MyDeveloperDay

Tags: #clang-format, #clang

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index db230583b6c9..cf481a1dcb62 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2930,6 +2930,14 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
     // interpolated strings. Interpolated strings are merged into a single token
     // so cannot have spaces inserted by this function.
 
+    // No space between 'this' and '['
+    if (Left.is(tok::kw_this) && Right.is(tok::l_square))
+      return false;
+
+    // No space between 'new' and '('
+    if (Left.is(tok::kw_new) && Right.is(tok::l_paren))
+      return false;
+
     // Space before { (including space within '{ {').
     if (Right.is(tok::l_brace))
       return true;

diff  --git a/clang/unittests/Format/FormatTestCSharp.cpp b/clang/unittests/Format/FormatTestCSharp.cpp
index 7f819a61c70e..824eb51bd693 100644
--- a/clang/unittests/Format/FormatTestCSharp.cpp
+++ b/clang/unittests/Format/FormatTestCSharp.cpp
@@ -627,6 +627,8 @@ TEST_F(FormatTestCSharp, CSharpSpaces) {
   verifyFormat(R"(taskContext.Factory.Run(async () => doThing(args);)", Style);
   verifyFormat(R"(catch (TestException) when (innerFinallyExecuted))", Style);
   verifyFormat(R"(private float[,] Values;)", Style);
+  verifyFormat(R"(Result this[Index x] => Foo(x);)", Style);
+  verifyFormat(R"(class ItemFactory<T> where T : new() {})", Style);
 
   Style.SpacesInSquareBrackets = true;
   verifyFormat(R"(private float[ , ] Values;)", Style);


        


More information about the cfe-commits mailing list