[clang-format patch] No spaces after : in js object literals in chromium style

Nico Weber thakis at chromium.org
Sun Feb 2 12:47:52 PST 2014


Hi,

the attached patch removes getGoogleJSStyle and getGoogleProtoStyle and
gives getGoogleStyle a LanguageKind parameter. This way, getChromiumStyle
can always call the google function and then do a small amount of tweaks –
this fixes PR18698 (since chromium style now picks up the right bits from
google js style automatically).

Ok?

Nico
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140202/ea75c643/attachment.html>
-------------- next part --------------
Index: include/clang/Format/Format.h
===================================================================
--- include/clang/Format/Format.h	(revision 200645)
+++ include/clang/Format/Format.h	(working copy)
@@ -360,22 +360,15 @@
 /// http://llvm.org/docs/CodingStandards.html.
 FormatStyle getLLVMStyle();
 
-/// \brief Returns a format style complying with Google's C++ style guide:
+/// \brief Returns a format style complying with one of Google's style guides:
 /// http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml.
-FormatStyle getGoogleStyle();
-
-/// \brief Returns a format style complying with Google's JavaScript style
-/// guide:
 /// http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml.
-FormatStyle getGoogleJSStyle();
-
-/// \brief Returns a format style complying with Google's Protocol Buffer style:
 /// https://developers.google.com/protocol-buffers/docs/style.
-FormatStyle getGoogleProtoStyle();
+FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language);
 
 /// \brief Returns a format style complying with Chromium's style guide:
 /// http://www.chromium.org/developers/coding-style.
-FormatStyle getChromiumStyle();
+FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language);
 
 /// \brief Returns a format style complying with Mozilla's style guide:
 /// https://developer.mozilla.org/en-US/docs/Developer_Guide/Coding_Style.
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp	(revision 200645)
+++ lib/Format/Format.cpp	(working copy)
@@ -294,8 +294,10 @@
   return LLVMStyle;
 }
 
-FormatStyle getGoogleStyle() {
+FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
   FormatStyle GoogleStyle = getLLVMStyle();
+  GoogleStyle.Language = Language;
+
   GoogleStyle.AccessModifierOffset = -1;
   GoogleStyle.AlignEscapedNewlinesLeft = true;
   GoogleStyle.AllowShortIfStatementsOnASingleLine = true;
@@ -316,27 +318,19 @@
   GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
   GoogleStyle.PenaltyBreakBeforeFirstCallParameter = 1;
 
+  if (Language == FormatStyle::LK_JavaScript) {
+    GoogleStyle.BreakBeforeTernaryOperators = false;
+    GoogleStyle.MaxEmptyLinesToKeep = 2;
+    GoogleStyle.SpacesInContainerLiterals = false;
+  } else if (Language == FormatStyle::LK_Proto) {
+    GoogleStyle.AllowShortFunctionsOnASingleLine = false;
+  }
+
   return GoogleStyle;
 }
 
-FormatStyle getGoogleJSStyle() {
-  FormatStyle GoogleJSStyle = getGoogleStyle();
-  GoogleJSStyle.Language = FormatStyle::LK_JavaScript;
-  GoogleJSStyle.BreakBeforeTernaryOperators = false;
-  GoogleJSStyle.MaxEmptyLinesToKeep = 2;
-  GoogleJSStyle.SpacesInContainerLiterals = false;
-  return GoogleJSStyle;
-}
-
-FormatStyle getGoogleProtoStyle() {
-  FormatStyle GoogleProtoStyle = getGoogleStyle();
-  GoogleProtoStyle.Language = FormatStyle::LK_Proto;
-  GoogleProtoStyle.AllowShortFunctionsOnASingleLine = false;
-  return GoogleProtoStyle;
-}
-
-FormatStyle getChromiumStyle() {
-  FormatStyle ChromiumStyle = getGoogleStyle();
+FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) {
+  FormatStyle ChromiumStyle = getGoogleStyle(Language);
   ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false;
   ChromiumStyle.AllowShortIfStatementsOnASingleLine = false;
   ChromiumStyle.AllowShortLoopsOnASingleLine = false;
@@ -389,20 +383,11 @@
   if (Name.equals_lower("llvm")) {
     *Style = getLLVMStyle();
   } else if (Name.equals_lower("chromium")) {
-    *Style = getChromiumStyle();
+    *Style = getChromiumStyle(Language);
   } else if (Name.equals_lower("mozilla")) {
     *Style = getMozillaStyle();
   } else if (Name.equals_lower("google")) {
-    switch (Language) {
-    case FormatStyle::LK_JavaScript:
-      *Style = getGoogleJSStyle();
-      break;
-    case FormatStyle::LK_Proto:
-      *Style = getGoogleProtoStyle();
-      break;
-    default:
-      *Style = getGoogleStyle();
-    }
+    *Style = getGoogleStyle(Language);
   } else if (Name.equals_lower("webkit")) {
     *Style = getWebKitStyle();
   } else if (Name.equals_lower("gnu")) {
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp	(revision 200645)
+++ unittests/Format/FormatTest.cpp	(working copy)
@@ -17,6 +17,14 @@
 namespace clang {
 namespace format {
 
+FormatStyle getGoogleStyle() {
+  return getGoogleStyle(FormatStyle::LK_Cpp);
+}
+
+FormatStyle getChromiumStyle() {
+  return getChromiumStyle(FormatStyle::LK_Cpp);
+}
+
 class FormatTest : public ::testing::Test {
 protected:
   std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length,
@@ -7250,7 +7258,7 @@
   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
   EXPECT_ALL_STYLES_EQUAL(Styles);
 
-  Styles[0] = getGoogleJSStyle();
+  Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
   EXPECT_TRUE(
       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
   EXPECT_TRUE(
@@ -7290,7 +7298,7 @@
   EXPECT_ALL_STYLES_EQUAL(Styles);
 
   Styles.resize(5);
-  Styles[0] = getGoogleJSStyle();
+  Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
   Styles[1] = getLLVMStyle();
   Styles[1].Language = FormatStyle::LK_JavaScript;
   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
Index: unittests/Format/FormatTestJS.cpp
===================================================================
--- unittests/Format/FormatTestJS.cpp	(revision 200645)
+++ unittests/Format/FormatTestJS.cpp	(working copy)
@@ -31,19 +31,19 @@
     return Result;
   }
 
-  static std::string format(llvm::StringRef Code,
-                            const FormatStyle &Style = getGoogleJSStyle()) {
+  static std::string format(llvm::StringRef Code, const FormatStyle &Style) {
     return format(Code, 0, Code.size(), Style);
   }
 
   static FormatStyle getGoogleJSStyleWithColumns(unsigned ColumnLimit) {
-    FormatStyle Style = getGoogleJSStyle();
+    FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
     Style.ColumnLimit = ColumnLimit;
     return Style;
   }
 
-  static void verifyFormat(llvm::StringRef Code,
-                           const FormatStyle &Style = getGoogleJSStyle()) {
+  static void verifyFormat(
+      llvm::StringRef Code,
+      const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) {
     EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
   }
 };
@@ -82,6 +82,9 @@
 TEST_F(FormatTestJS, SpacesInContainerLiterals) {
   verifyFormat("var arr = [1, 2, 3];");
   verifyFormat("var obj = {a: 1, b: 2, c: 3};");
+
+  verifyFormat("var obj = {a: 1, b: 2, c: 3};",
+               getChromiumStyle(FormatStyle::LK_JavaScript));
 }
 
 TEST_F(FormatTestJS, SingleQuoteStrings) {
Index: unittests/Format/FormatTestProto.cpp
===================================================================
--- unittests/Format/FormatTestProto.cpp	(revision 200645)
+++ unittests/Format/FormatTestProto.cpp	(working copy)
@@ -32,7 +32,7 @@
   }
 
   static std::string format(llvm::StringRef Code) {
-    FormatStyle Style = getGoogleProtoStyle();
+    FormatStyle Style = getGoogleStyle(FormatStyle::LK_Proto);
     Style.ColumnLimit = 60; // To make writing tests easier.
     return format(Code, 0, Code.size(), Style);
   }


More information about the cfe-commits mailing list