]> granicus.if.org Git - clang/commitdiff
clang-format: Be slightly more aggressive on single-line functions.
authorDaniel Jasper <djasper@google.com>
Wed, 7 May 2014 09:48:30 +0000 (09:48 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 7 May 2014 09:48:30 +0000 (09:48 +0000)
So that JS functions can also be merged into a single line.

Before:
  var func = function() {
    return 1;
  };

After:
  var func = function() { return 1; };

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@208176 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/Format.cpp
unittests/Format/FormatTestJS.cpp

index 21334ad9c80520c019c774055e50a352a3b4cb24..008b4159fa3c2bb94e750ee243d07fb7be9ba1e3 100644 (file)
@@ -654,6 +654,10 @@ private:
       Tok->CanBreakBefore = true;
       return 1;
     } else if (Limit != 0 && Line.First->isNot(tok::kw_namespace)) {
+      // We don't merge short records.
+      if (Line.First->isOneOf(tok::kw_class, tok::kw_union, tok::kw_struct))
+        return 0;
+
       // Check that we still have three lines and they fit into the limit.
       if (I + 2 == E || I[2]->Type == LT_Invalid)
         return 0;
@@ -672,9 +676,9 @@ private:
         Tok = Tok->Next;
       } while (Tok != NULL);
 
-      // Last, check that the third line contains a single closing brace.
+      // Last, check that the third line starts with a closing brace.
       Tok = I[2]->First;
-      if (Tok->getNextNonComment() != NULL || Tok->isNot(tok::r_brace))
+      if (Tok->isNot(tok::r_brace))
         return 0;
 
       return 2;
index ee49912522aece7447e3f88bd0ca9b16db0807e6..a94ed4707db114b67e71f4ac7ab0ab7c31a0120d 100644 (file)
@@ -98,6 +98,11 @@ TEST_F(FormatTestJS, GoogScopes) {
                "});  // goog.scope");
 }
 
+TEST_F(FormatTestJS, Closures) {
+  verifyFormat("doFoo(function() { return 1; });");
+  verifyFormat("var func = function() { return 1; };");
+}
+
 TEST_F(FormatTestJS, ReturnStatements) {
   verifyFormat("function() { return [hello, world]; }");
 }