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

Nico Weber thakis at chromium.org
Sun Feb 2 13:04:56 PST 2014


r200652, thanks!

Here's a possible followup that lifts the no-space-in-front-of-:-for-js to
getLLVMStyle() as that space is probably not wanted in any style for js.


On Sun, Feb 2, 2014 at 12:53 PM, Daniel Jasper <djasper at google.com> wrote:

> Looks good. (It's exactly what I though when reading your bug report).
>
> Thank you!
>
>
> On Sun, Feb 2, 2014 at 9:47 PM, Nico Weber <thakis at chromium.org> wrote:
>
>> 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
>>
>> _______________________________________________
>> cfe-commits mailing list
>> cfe-commits at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140202/e8120d65/attachment.html>
-------------- next part --------------
Index: include/clang/Format/Format.h
===================================================================
--- include/clang/Format/Format.h	(revision 200652)
+++ include/clang/Format/Format.h	(working copy)
@@ -358,7 +358,7 @@
 
 /// \brief Returns a format style complying with the LLVM coding standards:
 /// http://llvm.org/docs/CodingStandards.html.
-FormatStyle getLLVMStyle();
+FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language);
 
 /// \brief Returns a format style complying with one of Google's style guides:
 /// http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml.
@@ -372,15 +372,15 @@
 
 /// \brief Returns a format style complying with Mozilla's style guide:
 /// https://developer.mozilla.org/en-US/docs/Developer_Guide/Coding_Style.
-FormatStyle getMozillaStyle();
+FormatStyle getMozillaStyle(FormatStyle::LanguageKind Language);
 
 /// \brief Returns a format style complying with Webkit's style guide:
 /// http://www.webkit.org/coding/coding-style.html
-FormatStyle getWebKitStyle();
+FormatStyle getWebKitStyle(FormatStyle::LanguageKind Language);
 
 /// \brief Returns a format style complying with GNU Coding Standards:
 /// http://www.gnu.org/prep/standards/standards.html
-FormatStyle getGNUStyle();
+FormatStyle getGNUStyle(FormatStyle::LanguageKind Language);
 
 /// \brief Gets a predefined style for the specified language by name.
 ///
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp	(revision 200652)
+++ lib/Format/Format.cpp	(working copy)
@@ -239,9 +239,10 @@
 namespace clang {
 namespace format {
 
-FormatStyle getLLVMStyle() {
+FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
   FormatStyle LLVMStyle;
-  LLVMStyle.Language = FormatStyle::LK_Cpp;
+  LLVMStyle.Language = Language;
+
   LLVMStyle.AccessModifierOffset = -2;
   LLVMStyle.AlignEscapedNewlinesLeft = false;
   LLVMStyle.AlignTrailingComments = true;
@@ -291,12 +292,14 @@
   LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
   LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19;
 
+  if (Language == FormatStyle::LK_JavaScript)
+    LLVMStyle.SpacesInContainerLiterals = false;
+
   return LLVMStyle;
 }
 
 FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
-  FormatStyle GoogleStyle = getLLVMStyle();
-  GoogleStyle.Language = Language;
+  FormatStyle GoogleStyle = getLLVMStyle(Language);
 
   GoogleStyle.AccessModifierOffset = -1;
   GoogleStyle.AlignEscapedNewlinesLeft = true;
@@ -321,7 +324,6 @@
   if (Language == FormatStyle::LK_JavaScript) {
     GoogleStyle.BreakBeforeTernaryOperators = false;
     GoogleStyle.MaxEmptyLinesToKeep = 2;
-    GoogleStyle.SpacesInContainerLiterals = false;
   } else if (Language == FormatStyle::LK_Proto) {
     GoogleStyle.AllowShortFunctionsOnASingleLine = false;
   }
@@ -340,8 +342,8 @@
   return ChromiumStyle;
 }
 
-FormatStyle getMozillaStyle() {
-  FormatStyle MozillaStyle = getLLVMStyle();
+FormatStyle getMozillaStyle(FormatStyle::LanguageKind Language) {
+  FormatStyle MozillaStyle = getLLVMStyle(Language);
   MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false;
   MozillaStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
   MozillaStyle.DerivePointerBinding = true;
@@ -353,8 +355,8 @@
   return MozillaStyle;
 }
 
-FormatStyle getWebKitStyle() {
-  FormatStyle Style = getLLVMStyle();
+FormatStyle getWebKitStyle(FormatStyle::LanguageKind Language) {
+  FormatStyle Style = getLLVMStyle(Language);
   Style.AccessModifierOffset = -4;
   Style.AlignTrailingComments = false;
   Style.BreakBeforeBinaryOperators = true;
@@ -368,8 +370,8 @@
   return Style;
 }
 
-FormatStyle getGNUStyle() {
-  FormatStyle Style = getLLVMStyle();
+FormatStyle getGNUStyle(FormatStyle::LanguageKind Language) {
+  FormatStyle Style = getLLVMStyle(Language);
   Style.BreakBeforeBinaryOperators = true;
   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
   Style.BreakBeforeTernaryOperators = true;
@@ -381,22 +383,21 @@
 bool getPredefinedStyle(StringRef Name, FormatStyle::LanguageKind Language,
                         FormatStyle *Style) {
   if (Name.equals_lower("llvm")) {
-    *Style = getLLVMStyle();
+    *Style = getLLVMStyle(Language);
   } else if (Name.equals_lower("chromium")) {
     *Style = getChromiumStyle(Language);
   } else if (Name.equals_lower("mozilla")) {
-    *Style = getMozillaStyle();
+    *Style = getMozillaStyle(Language);
   } else if (Name.equals_lower("google")) {
     *Style = getGoogleStyle(Language);
   } else if (Name.equals_lower("webkit")) {
-    *Style = getWebKitStyle();
+    *Style = getWebKitStyle(Language);
   } else if (Name.equals_lower("gnu")) {
-    *Style = getGNUStyle();
+    *Style = getGNUStyle(Language);
   } else {
     return false;
   }
 
-  Style->Language = Language;
   return true;
 }
 
@@ -1720,7 +1721,7 @@
 
 FormatStyle getStyle(StringRef StyleName, StringRef FileName,
                      StringRef FallbackStyle) {
-  FormatStyle Style = getLLVMStyle();
+  FormatStyle Style;
   Style.Language = getLanguageByFileName(FileName);
   if (!getPredefinedStyle(FallbackStyle, Style.Language, &Style)) {
     llvm::errs() << "Invalid fallback style \"" << FallbackStyle
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp	(revision 200652)
+++ unittests/Format/FormatTest.cpp	(working copy)
@@ -17,10 +17,26 @@
 namespace clang {
 namespace format {
 
+FormatStyle getGNUStyle() {
+  return getGNUStyle(FormatStyle::LK_Cpp);
+}
+
 FormatStyle getGoogleStyle() {
   return getGoogleStyle(FormatStyle::LK_Cpp);
 }
 
+FormatStyle getLLVMStyle() {
+  return getLLVMStyle(FormatStyle::LK_Cpp);
+}
+
+FormatStyle getMozillaStyle() {
+  return getMozillaStyle(FormatStyle::LK_Cpp);
+}
+
+FormatStyle getWebKitStyle() {
+  return getWebKitStyle(FormatStyle::LK_Cpp);
+}
+
 class FormatTest : public ::testing::Test {
 protected:
   std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length,
Index: unittests/Format/FormatTestJS.cpp
===================================================================
--- unittests/Format/FormatTestJS.cpp	(revision 200652)
+++ unittests/Format/FormatTestJS.cpp	(working copy)
@@ -83,6 +83,8 @@
   verifyFormat("var arr = [1, 2, 3];");
   verifyFormat("var obj = {a: 1, b: 2, c: 3};");
 
+  verifyFormat("var obj = { a: 1, b: 2, c: 3 };",
+               getLLVMStyle(FormatStyle::LK_JavaScript));
   verifyFormat("var obj = {a: 1, b: 2, c: 3};",
                getChromiumStyle(FormatStyle::LK_JavaScript));
 }


More information about the cfe-commits mailing list