This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] JSON formatting add new option for controlling newlines in json arrays
ClosedPublic

Authored by MyDeveloperDay on Sep 9 2022, 10:17 AM.

Details

Summary

Working in a mixed environment of both vscode/vim with a team configured prettier configuration, this can leave clang-format and prettier fighting each other over the formatting of arrays, both simple arrays of elements.

This review aims to add some "control knobs" to the Json formatting in clang-format to help align the two tools so they can be used interchangeably.

This will allow simply arrays [1, 2, 3] to remain on a single line but will break those arrays based on context within that array.

Happy to change the name of the option (this is the third name I tried)

Diff Detail

Event Timeline

MyDeveloperDay created this revision.Sep 9 2022, 10:17 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 9 2022, 10:17 AM
MyDeveloperDay requested review of this revision.Sep 9 2022, 10:17 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 9 2022, 10:17 AM
curdeius added inline comments.Sep 9 2022, 1:37 PM
clang/docs/ClangFormatStyleOptions.rst
3132

Why limiting to JSON only?
Could we name it in a general fashion (we comment that it's JSON only for the time being). I believe it may be an interesting option for various languages.

How about BreakMultilineArrays, or just BreakArrays to follow the naming of existing options a bit?

clang/lib/Format/TokenAnnotator.cpp
4410

Please elide braces in the ifs.

4420

You can elide braces here.

4427

You might elide braces in both ifs.

owenpan added inline comments.Sep 9 2022, 11:26 PM
clang/lib/Format/TokenAnnotator.cpp
4403–4419

Merge the check for comma below to avoid repeated code. Also, the check for l_brace is redundant.

4408–4417

Use a for loop instead.

4419–4420

This can be deleted now.

MyDeveloperDay marked 7 inline comments as done.

Addressed review comments, renamed the options

clang/docs/ClangFormatStyleOptions.rst
3132

I'm going to change the name to be BreakArrays but I'm not 100% sure who it might help other languages, but maybe we can look at this afterwards so having a good name now will help us later on.

clang/lib/Format/TokenAnnotator.cpp
4403–4419

@owenpan you have a very fine eye.. I didn't see that and had to reread your comments a couple of times! Thank you its much cleaner now

4420

This was funny I had RemoveBracesLLVM on, but it didn't get rid of them automatically, I wonder if this was my fault or if we are missing a case for the RemoveBraces option, but thanks you be done now

MyDeveloperDay added inline comments.Sep 10 2022, 6:04 AM
clang/docs/ClangFormatStyleOptions.rst
3132

Why limiting to JSON only?

I'm slightly wondering if it would get over rrules by code later on, but we could try I guess, I think we'd need to handle the [] / {} separately I think.

clang/.clang-format
2 ↗(On Diff #459283)

Unrelated.

clang/include/clang/Format/Format.h
2080
2082
2093

Please sort.

2093

What about Leave?

3918

Please sort.

clang/lib/Format/TokenAnnotator.cpp
4403–4404
  • address review comments
MyDeveloperDay marked 6 inline comments as done.Sep 12 2022, 12:13 AM
MyDeveloperDay added inline comments.
clang/include/clang/Format/Format.h
2093

I'm going to say that at present the boolean is probably enough as JSON formatting is really to do one or the other, but of course this can progress easily into an enum when there is a use case

owenpan accepted this revision.Sep 12 2022, 12:27 AM
owenpan added inline comments.
clang/lib/Format/TokenAnnotator.cpp
4403–4418

The compiler will warn without the extra pair of parentheses?

4420

@MyDeveloperDay I'm very curious about this. Can you open an issue if it can be reproduced?

This revision is now accepted and ready to land.Sep 12 2022, 12:27 AM
clang/lib/Format/Format.cpp
1968

These braces should stay, shouldn't they?

2196

It seems like there are a ton of unrelated brace removals.

MyDeveloperDay marked an inline comment as done.Sep 12 2022, 11:53 PM
MyDeveloperDay added inline comments.
clang/lib/Format/Format.cpp
2196

Oh rubbish I thought we were RemoveBraceLLVM clean, let me reproduce the patch

MyDeveloperDay marked 4 inline comments as done.

put back removed braces
address other review comments