Allow predefined styles to define different options for different languages.
Summary:
Allow predefined styles to define different options for different
languages so that one can run:
clang-format -style=google file1.cpp file2.js
or use a single .clang-format file with "BasedOnStyle: Google" for both c++ and
JS files.
Added Google style for JavaScript with "BreakBeforeTernaryOperators" set to
false.
Reviewers: djasper
Reviewed By: djasper
CC: cfe-commits, klimek
Differential Revision: http://llvm-reviews.chandlerc.com/D2364
llvm-svn: 196909
diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp
index 1f8f139..0d242a7 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/unittests/Format/FormatTestJS.cpp
@@ -33,24 +33,18 @@
}
static std::string format(llvm::StringRef Code,
- const FormatStyle &Style = getJSStyle()) {
+ const FormatStyle &Style = getGoogleJSStyle()) {
return format(Code, 0, Code.size(), Style);
}
- static FormatStyle getJSStyle() {
- FormatStyle Style = getLLVMStyle();
- Style.Language = FormatStyle::LK_JavaScript;
- return Style;
- }
-
- static FormatStyle getJSStyleWithColumns(unsigned ColumnLimit) {
- FormatStyle Style = getJSStyle();
+ static FormatStyle getGoogleJSStyleWithColumns(unsigned ColumnLimit) {
+ FormatStyle Style = getGoogleJSStyle();
Style.ColumnLimit = ColumnLimit;
return Style;
}
static void verifyFormat(llvm::StringRef Code,
- const FormatStyle &Style = getJSStyle()) {
+ const FormatStyle &Style = getGoogleJSStyle()) {
EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
}
};
@@ -60,26 +54,30 @@
verifyFormat("a != = b;");
verifyFormat("a === b;");
- verifyFormat("aaaaaaa ===\n b;", getJSStyleWithColumns(10));
+ verifyFormat("aaaaaaa ===\n b;", getGoogleJSStyleWithColumns(10));
verifyFormat("a !== b;");
- verifyFormat("aaaaaaa !==\n b;", getJSStyleWithColumns(10));
+ verifyFormat("aaaaaaa !==\n b;", getGoogleJSStyleWithColumns(10));
verifyFormat("if (a + b + c +\n"
" d !==\n"
" e + f + g)\n"
" q();",
- getJSStyleWithColumns(20));
+ getGoogleJSStyleWithColumns(20));
verifyFormat("a >> >= b;");
verifyFormat("a >>> b;");
- verifyFormat("aaaaaaa >>>\n b;", getJSStyleWithColumns(10));
+ verifyFormat("aaaaaaa >>>\n b;", getGoogleJSStyleWithColumns(10));
verifyFormat("a >>>= b;");
- verifyFormat("aaaaaaa >>>=\n b;", getJSStyleWithColumns(10));
+ verifyFormat("aaaaaaa >>>=\n b;", getGoogleJSStyleWithColumns(10));
verifyFormat("if (a + b + c +\n"
" d >>>\n"
" e + f + g)\n"
" q();",
- getJSStyleWithColumns(20));
+ getGoogleJSStyleWithColumns(20));
+ verifyFormat("var x = aaaaaaaaaa ?\n"
+ " bbbbbb :\n"
+ " ccc;",
+ getGoogleJSStyleWithColumns(20));
}
} // end namespace tooling