I've seen a couple of request for extra Json formatting to match prettier capability.
Reviewed By: owenpan
Differential Revision: https://reviews.llvm.org/D147003
true: false:
class Foo : Bar {} vs. class Foo: Bar {}
+.. _SpaceBeforeJsonColon:
+
+**SpaceBeforeJsonColon** (``Boolean``) :versionbadge:`clang-format 17` :ref:`¶ <SpaceBeforeJsonColon>`
+ If ``true``, a space will be add before a JSON colon.
+
+ .. code-block:: c++
+
+ true: false:
+ { {
+ "key" : "value" vs. "key": "value"
+ } }
+
.. _SpaceBeforeParens:
**SpaceBeforeParens** (``SpaceBeforeParensStyle``) :versionbadge:`clang-format 3.5` :ref:`¶ <SpaceBeforeParens>`
/// \version 7
bool SpaceBeforeInheritanceColon;
+ /// If ``true``, a space will be add before a JSON colon.
+ /// \code
+ /// true: false:
+ /// { {
+ /// "key" : "value" vs. "key": "value"
+ /// } }
+ /// \endcode
+ /// \version 17
+ bool SpaceBeforeJsonColon;
+
/// Different ways to put a space before opening parentheses.
enum SpaceBeforeParensStyle : int8_t {
/// Never put a space before opening parentheses.
SpaceBeforeCtorInitializerColon ==
R.SpaceBeforeCtorInitializerColon &&
SpaceBeforeInheritanceColon == R.SpaceBeforeInheritanceColon &&
+ SpaceBeforeJsonColon == R.SpaceBeforeJsonColon &&
SpaceBeforeParens == R.SpaceBeforeParens &&
SpaceBeforeParensOptions == R.SpaceBeforeParensOptions &&
SpaceAroundPointerQualifiers == R.SpaceAroundPointerQualifiers &&
Style.SpaceBeforeCtorInitializerColon);
IO.mapOptional("SpaceBeforeInheritanceColon",
Style.SpaceBeforeInheritanceColon);
+ IO.mapOptional("SpaceBeforeJsonColon", Style.SpaceBeforeJsonColon);
IO.mapOptional("SpaceBeforeParens", Style.SpaceBeforeParens);
IO.mapOptional("SpaceBeforeParensOptions", Style.SpaceBeforeParensOptions);
IO.mapOptional("SpaceBeforeRangeBasedForLoopColon",
LLVMStyle.SpaceBeforeCaseColon = false;
LLVMStyle.SpaceBeforeCtorInitializerColon = true;
LLVMStyle.SpaceBeforeInheritanceColon = true;
+ LLVMStyle.SpaceBeforeJsonColon = false;
LLVMStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements;
LLVMStyle.SpaceBeforeParensOptions = {};
LLVMStyle.SpaceBeforeParensOptions.AfterControlStatements = true;
Right.MatchingParen->is(TT_CastRParen)) {
return true;
}
- if (Style.isJson() && Left.is(tok::string_literal) && Right.is(tok::colon))
- return false;
if (Left.is(Keywords.kw_assert) && Style.Language == FormatStyle::LK_Java)
return true;
if (Style.ObjCSpaceAfterProperty && Line.Type == LT_ObjCProperty &&
if (Left.is(tok::numeric_constant) && Right.is(tok::percent))
return Right.hasWhitespaceBefore();
} else if (Style.isJson()) {
- if (Right.is(tok::colon))
- return false;
+ if (Right.is(tok::colon) && Left.is(tok::string_literal))
+ return Style.SpaceBeforeJsonColon;
} else if (Style.isCSharp()) {
// Require spaces around '{' and before '}' unless they appear in
// interpolated strings. Interpolated strings are merged into a single token
CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList);
CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
+ CHECK_PARSE_BOOL(SpaceBeforeJsonColon);
CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets);
Style);
}
+TEST_F(FormatTestJson, SpaceBeforeJsonColon) {
+ FormatStyle Style = getLLVMStyle(FormatStyle::LK_Json);
+ verifyFormatStable("{\n"
+ " \"name\": 1\n"
+ "}",
+ Style);
+
+ Style.SpaceBeforeJsonColon = true;
+ verifyFormatStable("{}", Style);
+ verifyFormatStable("{\n"
+ " \"name\" : 1\n"
+ "}",
+ Style);
+}
+
} // namespace format
} // end namespace clang