clang-format: Let chromium style inherit google style's javascript tweaks.

llvm-svn: 200652
This commit is contained in:
Nico Weber 2014-02-02 20:50:45 +00:00
parent cf466800b7
commit 514ecc8ce8
5 changed files with 35 additions and 50 deletions

View File

@ -360,22 +360,15 @@ struct FormatStyle {
/// 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.

View File

@ -294,8 +294,10 @@ FormatStyle getLLVMStyle() {
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 @@ FormatStyle getGoogleStyle() {
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 @@ bool getPredefinedStyle(StringRef Name, FormatStyle::LanguageKind Language,
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")) {

View File

@ -17,6 +17,10 @@
namespace clang {
namespace format {
FormatStyle getGoogleStyle() {
return getGoogleStyle(FormatStyle::LK_Cpp);
}
class FormatTest : public ::testing::Test {
protected:
std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length,
@ -5783,7 +5787,7 @@ TEST_F(FormatTest, FormatObjCMethodExpr) {
" backing:NSBackingStoreBuffered\n"
" defer:NO]);\n"
"}",
getChromiumStyle());
getChromiumStyle(FormatStyle::LK_Cpp));
verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
" with:contentsNativeView];");
@ -7250,14 +7254,14 @@ TEST_F(FormatTest, GetsPredefinedStyleByName) {
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(
getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
EXPECT_ALL_STYLES_EQUAL(Styles);
Styles[0] = getChromiumStyle();
Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
EXPECT_ALL_STYLES_EQUAL(Styles);
@ -7290,7 +7294,7 @@ TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
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());

View File

@ -31,19 +31,19 @@ protected:
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, UnderstandsJavaScriptOperators) {
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) {

View File

@ -32,7 +32,7 @@ protected:
}
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);
}