[clang] af98f3b - [clang-format] JSON Add ability to add a space before the colon
via cfe-commits
cfe-commits at lists.llvm.org
Wed Mar 29 13:29:16 PDT 2023
Author: mydeveloperday
Date: 2023-03-29T21:28:40+01:00
New Revision: af98f3b1f4db065ff82b50a9689511dd3223655b
URL: https://github.com/llvm/llvm-project/commit/af98f3b1f4db065ff82b50a9689511dd3223655b
DIFF: https://github.com/llvm/llvm-project/commit/af98f3b1f4db065ff82b50a9689511dd3223655b.diff
LOG: [clang-format] JSON Add ability to add a space before the colon
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
Added:
Modified:
clang/docs/ClangFormatStyleOptions.rst
clang/include/clang/Format/Format.h
clang/lib/Format/Format.cpp
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/ConfigParseTest.cpp
clang/unittests/Format/FormatTestJson.cpp
Removed:
################################################################################
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index 24ae02a2eddb2..d706a0494f0f5 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -4746,6 +4746,18 @@ the configuration (without a prefix: ``Auto``).
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>`
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 412f861f15e1a..cc2b34a20e895 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -3714,6 +3714,16 @@ struct FormatStyle {
/// \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 @@ struct FormatStyle {
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
index d3062905ad7ee..67772467abad4 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1006,6 +1006,7 @@ template <> struct MappingTraits<FormatStyle> {
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 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
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
index 826cf8115a1eb..1fc190e56c288 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3629,8 +3629,6 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
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 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
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
index 1ebd48717fff8..6043bce250738 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -189,6 +189,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
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
index 17fc3b77575c5..8cb8025f096ca 100644
--- a/clang/unittests/Format/FormatTestJson.cpp
+++ b/clang/unittests/Format/FormatTestJson.cpp
@@ -236,5 +236,20 @@ TEST_F(FormatTestJson, DisableJsonFormat) {
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
More information about the cfe-commits
mailing list