Index: clang/docs/ClangFormatStyleOptions.rst =================================================================== --- clang/docs/ClangFormatStyleOptions.rst +++ clang/docs/ClangFormatStyleOptions.rst @@ -4746,6 +4746,18 @@ true: false: class Foo : Bar {} vs. class Foo: Bar {} +.. _SpaceBeforeJsonColon: + +**SpaceBeforeJsonColon** (``Boolean``) :versionbadge:`clang-format 17` :ref:`¶ ` + 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:`¶ ` Index: clang/include/clang/Format/Format.h =================================================================== --- clang/include/clang/Format/Format.h +++ clang/include/clang/Format/Format.h @@ -3714,6 +3714,16 @@ /// \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. @@ -4323,6 +4333,7 @@ SpaceBeforeCtorInitializerColon == R.SpaceBeforeCtorInitializerColon && SpaceBeforeInheritanceColon == R.SpaceBeforeInheritanceColon && + SpaceBeforeJsonColon == R.SpaceBeforeJsonColon && SpaceBeforeParens == R.SpaceBeforeParens && SpaceBeforeParensOptions == R.SpaceBeforeParensOptions && SpaceAroundPointerQualifiers == R.SpaceAroundPointerQualifiers && Index: clang/lib/Format/Format.cpp =================================================================== --- clang/lib/Format/Format.cpp +++ clang/lib/Format/Format.cpp @@ -1006,6 +1006,7 @@ 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", @@ -1429,6 +1430,7 @@ LLVMStyle.SpaceBeforeCaseColon = false; LLVMStyle.SpaceBeforeCtorInitializerColon = true; LLVMStyle.SpaceBeforeInheritanceColon = true; + LLVMStyle.SpaceBeforeJsonColon = false; LLVMStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements; LLVMStyle.SpaceBeforeParensOptions = {}; LLVMStyle.SpaceBeforeParensOptions.AfterControlStatements = true; Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -3629,8 +3629,6 @@ 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 && @@ -4154,7 +4152,7 @@ return Right.hasWhitespaceBefore(); } else if (Style.isJson()) { if (Right.is(tok::colon)) - return false; + 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 Index: clang/unittests/Format/FormatTestJson.cpp =================================================================== --- clang/unittests/Format/FormatTestJson.cpp +++ clang/unittests/Format/FormatTestJson.cpp @@ -236,5 +236,20 @@ 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