Skip to content

Commit

Permalink
[clang-format] Hanlde leading whitespaces for JSON files
Browse files Browse the repository at this point in the history
Fixes #62228.
Fixes #62229.

Differential Revision: https://reviews.llvm.org/D148777
  • Loading branch information
owenca committed Apr 20, 2023
1 parent cc7bb70 commit 51f6a16
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
10 changes: 5 additions & 5 deletions clang/lib/Format/Format.cpp
Expand Up @@ -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};
}

Expand Down
20 changes: 20 additions & 0 deletions clang/unittests/Format/FormatTestJson.cpp
Expand Up @@ -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

0 comments on commit 51f6a16

Please sign in to comment.