diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/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:`¶ ` diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h --- a/clang/include/clang/Format/Format.h +++ b/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 && diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp --- a/clang/lib/Format/Format.cpp +++ b/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; diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/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 && @@ -4153,8 +4151,8 @@ 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 diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp --- a/clang/unittests/Format/ConfigParseTest.cpp +++ b/clang/unittests/Format/ConfigParseTest.cpp @@ -189,6 +189,7 @@ CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList); CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); + CHECK_PARSE_BOOL(SpaceBeforeJsonColon); CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets); diff --git a/clang/unittests/Format/FormatTestJson.cpp b/clang/unittests/Format/FormatTestJson.cpp --- a/clang/unittests/Format/FormatTestJson.cpp +++ b/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