r199317 - clang-format: Fixed formatting of JavaScript container literals
Daniel Jasper
djasper at google.com
Wed Jan 15 07:09:09 PST 2014
Author: djasper
Date: Wed Jan 15 09:09:08 2014
New Revision: 199317
URL: http://llvm.org/viewvc/llvm-project?rev=199317&view=rev
Log:
clang-format: Fixed formatting of JavaScript container literals
Before:
var arr = [ 1, 2, 3 ];
var obj = {a : 1, b : 2, c : 3};
After:
var arr = [1, 2, 3];
var obj = {a: 1, b: 2, c: 3};
Modified:
cfe/trunk/include/clang/Format/Format.h
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/lib/Format/TokenAnnotator.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
cfe/trunk/unittests/Format/FormatTestJS.cpp
Modified: cfe/trunk/include/clang/Format/Format.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Format/Format.h?rev=199317&r1=199316&r2=199317&view=diff
==============================================================================
--- cfe/trunk/include/clang/Format/Format.h (original)
+++ cfe/trunk/include/clang/Format/Format.h Wed Jan 15 09:09:08 2014
@@ -254,10 +254,14 @@ struct FormatStyle {
/// template argument lists
bool SpacesInAngles;
- /// \brief If \c false, spaces may be inserted into '()'.
+ /// \brief If \c true, spaces may be inserted into '()'.
bool SpaceInEmptyParentheses;
- /// \brief If \c false, spaces may be inserted into C style casts.
+ /// \brief If \c true, spaces are inserted inside container literals (e.g.
+ /// ObjC and Javascript array and dict literals).
+ bool SpacesInContainerLiterals;
+
+ /// \brief If \c true, spaces may be inserted into C style casts.
bool SpacesInCStyleCastParentheses;
/// \brief Different ways to put a space before opening parentheses.
@@ -335,6 +339,7 @@ struct FormatStyle {
UseTab == R.UseTab && SpacesInParentheses == R.SpacesInParentheses &&
SpacesInAngles == R.SpacesInAngles &&
SpaceInEmptyParentheses == R.SpaceInEmptyParentheses &&
+ SpacesInContainerLiterals == R.SpacesInContainerLiterals &&
SpacesInCStyleCastParentheses == R.SpacesInCStyleCastParentheses &&
SpaceBeforeParens == R.SpaceBeforeParens &&
SpaceBeforeAssignmentOperators == R.SpaceBeforeAssignmentOperators &&
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=199317&r1=199316&r2=199317&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Wed Jan 15 09:09:08 2014
@@ -190,6 +190,8 @@ template <> struct MappingTraits<FormatS
IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses);
IO.mapOptional("SpacesInCStyleCastParentheses",
Style.SpacesInCStyleCastParentheses);
+ IO.mapOptional("SpacesInContainerLiterals",
+ Style.SpacesInContainerLiterals);
IO.mapOptional("SpaceBeforeAssignmentOperators",
Style.SpaceBeforeAssignmentOperators);
IO.mapOptional("ContinuationIndentWidth", Style.ContinuationIndentWidth);
@@ -271,6 +273,7 @@ FormatStyle getLLVMStyle() {
LLVMStyle.UseTab = FormatStyle::UT_Never;
LLVMStyle.SpacesInParentheses = false;
LLVMStyle.SpaceInEmptyParentheses = false;
+ LLVMStyle.SpacesInContainerLiterals = true;
LLVMStyle.SpacesInCStyleCastParentheses = false;
LLVMStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements;
LLVMStyle.SpaceBeforeAssignmentOperators = true;
@@ -316,9 +319,7 @@ FormatStyle getGoogleJSStyle() {
FormatStyle GoogleJSStyle = getGoogleStyle();
GoogleJSStyle.Language = FormatStyle::LK_JavaScript;
GoogleJSStyle.BreakBeforeTernaryOperators = false;
- // FIXME: Currently unimplemented:
- // var arr = [1, 2, 3]; // No space after [ or before ].
- // var obj = {a: 1, b: 2, c: 3}; // No space after ':'.
+ GoogleJSStyle.SpacesInContainerLiterals = false;
return GoogleJSStyle;
}
Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=199317&r1=199316&r2=199317&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Wed Jan 15 09:09:08 2014
@@ -1321,17 +1321,15 @@ bool TokenAnnotator::spaceRequiredBetwee
return false;
if (Left.is(tok::l_square))
return Left.Type == TT_ArrayInitializerLSquare &&
- Right.isNot(tok::r_square);
+ Style.SpacesInContainerLiterals && Right.isNot(tok::r_square);
if (Right.is(tok::r_square))
- return Right.MatchingParen &&
+ return Right.MatchingParen && Style.SpacesInContainerLiterals &&
Right.MatchingParen->Type == TT_ArrayInitializerLSquare;
if (Right.is(tok::l_square) && Right.Type != TT_ObjCMethodExpr &&
Right.Type != TT_LambdaLSquare && Left.isNot(tok::numeric_constant))
return false;
if (Left.is(tok::colon))
return Left.Type != TT_ObjCMethodExpr;
- if (Right.is(tok::colon))
- return Right.Type != TT_ObjCMethodExpr && !Left.is(tok::question);
if (Right.is(tok::l_paren)) {
if (Left.is(tok::r_paren) && Left.MatchingParen &&
Left.MatchingParen->Previous &&
@@ -1404,7 +1402,8 @@ bool TokenAnnotator::spaceRequiredBefore
if (Tok.is(tok::colon))
return !Line.First->isOneOf(tok::kw_case, tok::kw_default) &&
Tok.getNextNonComment() != NULL && Tok.Type != TT_ObjCMethodExpr &&
- !Tok.Previous->is(tok::question);
+ !Tok.Previous->is(tok::question) &&
+ (Tok.Type != TT_DictLiteral || Style.SpacesInContainerLiterals);
if (Tok.Previous->Type == TT_UnaryOperator ||
Tok.Previous->Type == TT_CastRParen)
return false;
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=199317&r1=199316&r2=199317&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Jan 15 09:09:08 2014
@@ -7321,6 +7321,7 @@ TEST_F(FormatTest, ParsesConfiguration)
CHECK_PARSE_BOOL(SpacesInParentheses);
CHECK_PARSE_BOOL(SpacesInAngles);
CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
+ CHECK_PARSE_BOOL(SpacesInContainerLiterals);
CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=199317&r1=199316&r2=199317&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Wed Jan 15 09:09:08 2014
@@ -79,5 +79,10 @@ TEST_F(FormatTestJS, UnderstandsJavaScri
getGoogleJSStyleWithColumns(20));
}
+TEST_F(FormatTestJS, SpacesInContainerLiterals) {
+ verifyFormat("var arr = [1, 2, 3];");
+ verifyFormat("var obj = {a: 1, b: 2, c: 3};");
+}
+
} // end namespace tooling
} // end namespace clang
More information about the cfe-commits
mailing list