[clang] 51f6a16 - [clang-format] Hanlde leading whitespaces for JSON files
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 20 04:32:04 PDT 2023
Author: Owen Pan
Date: 2023-04-20T04:31:55-07:00
New Revision: 51f6a16646b76152d1b91ed68d018d306f7ab2fc
URL: https://github.com/llvm/llvm-project/commit/51f6a16646b76152d1b91ed68d018d306f7ab2fc
DIFF: https://github.com/llvm/llvm-project/commit/51f6a16646b76152d1b91ed68d018d306f7ab2fc.diff
LOG: [clang-format] Hanlde leading whitespaces for JSON files
Fixes #62228.
Fixes #62229.
Differential Revision: https://reviews.llvm.org/D148777
Added:
Modified:
clang/lib/Format/Format.cpp
clang/unittests/Format/FormatTestJson.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 43054ec5f5d38..087ac8f627331 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3447,11 +3447,11 @@ reformat(const FormatStyle &Style, StringRef Code,
tooling::Replacements Replaces =
Formatter(*Env, Style, Status).process().first;
// add a replacement to remove the "x = " from the result.
- if (!Replaces.add(tooling::Replacement(FileName, 0, 4, ""))) {
- // apply the reformatting changes and the removal of "x = ".
- if (applyAllReplacements(Code, Replaces))
- return {Replaces, 0};
- }
+ Replaces = Replaces.merge(
+ tooling::Replacements(tooling::Replacement(FileName, 0, 4, "")));
+ // apply the reformatting changes and the removal of "x = ".
+ if (applyAllReplacements(Code, Replaces))
+ return {Replaces, 0};
return {tooling::Replacements(), 0};
}
diff --git a/clang/unittests/Format/FormatTestJson.cpp b/clang/unittests/Format/FormatTestJson.cpp
index 8cb8025f096ca..3254802dc0d60 100644
--- a/clang/unittests/Format/FormatTestJson.cpp
+++ b/clang/unittests/Format/FormatTestJson.cpp
@@ -251,5 +251,25 @@ TEST_F(FormatTestJson, SpaceBeforeJsonColon) {
Style);
}
+TEST_F(FormatTestJson, StartsWithWhitespaces) {
+ FormatStyle Style = getLLVMStyle(FormatStyle::LK_Json);
+ EXPECT_EQ("{\n"
+ " \"name\": 1\n"
+ "}",
+ format(" {\n"
+ " \"name\": 1\n"
+ "}",
+ Style));
+
+ // FIXME: The block below is over-indented.
+ EXPECT_EQ(" {\n"
+ " \"name\": 1\n"
+ " }",
+ format("\n{\n"
+ " \"name\": 1\n"
+ "}",
+ Style));
+}
+
} // namespace format
} // end namespace clang
More information about the cfe-commits
mailing list