The option allows users to specify how many columns to use to indent
the contents of initializer lists.
This addresses the corresponding github issue, albeit in a slightly different and broader way to what was proposed there.
Differential D146101
[clang-format] Add BracedInitializerIndentWidth option. jp4a50 on Mar 14 2023, 3:29 PM. Authored by
Details The option allows users to specify how many columns to use to indent This addresses the corresponding github issue, albeit in a slightly different and broader way to what was proposed there.
Diff Detail
Event Timeline
Comment Actions Please see https://clang.llvm.org/docs/ClangFormatStyleOptions.html#adding-additional-style-options. Is there a way to fix the issue without adding a new option?
Comment Actions Change DesignatedInitializerIndentWidth to a signed integer which defaults to ContinuationIndentWidth when set to -1.
Comment Actions @owenpan We could simply change the indentation level of designated initializers for everyone to match IndentWidth instead of ContinuationIndentWidth but I suspect that other users might be unhappy if we made that breaking change? I guess we could also consider making this behaviour only kick in when a specific set of *other* options are specified (e.g. AlwaysBreak) so that it applies to our clang-format style and not *all* others but that just feels like a hack. I understand the added complexity and maintenance burden of a new option but we do meet the 3 criteria listed in your link.
I think it's also worth noting that the google style guide gives an example of designated initializers indented at 2 spaces (whereas their "continuation indent" for wrapped function parameters is 4). Comment Actions The style guide doesn't mention indenting designated initializers with 2 spaces?
It's likely an error that the designated initializer example there shows 2-space indents as clang-format uses the 4-space continuation indent width: clang-format -style=Google Point p = { .x = 1.0, .y = 2.0, // z will be 0.0 }; Point p = { .x = 1.0, .y = 2.0, // z will be 0.0 }; Comment Actions
That is a fair point. I will get it updated because the author and maintainer of that style guide is the one requesting this change since, in practice, codebases following this style guide indent designated initializers with 2 spaces.
This is possible, but isn't it also possible that they would prefer designated initializers to be indented at 2 spaces but don't have the option with clang-format currently? The only general information supplied in the google style guide about indentation is as follows:
If we are taking a strict definition of parameters, the above suggests to me that designated initializers would fall under the default indentation of 2 spaces. As mentioned on my other diff, I'm away until next Monday now so won't be able to get back to further comments til then. Comment Actions What I'd really like to see, is... in the event that ContinuationIndentWidth is set and I do NOTset DesignatedInitializerIndentWidth , then DesignatedInitializerIndentWidth would inherit from that (not the default as you have here), if I set ContinuationIndentWidthto 3 DesignatedInitializerIndentWidth should be 3 Otherwise if I set DesignatedInitializerIndentWidth explicitly then it can be what is set, i.e. (for base LLVM style) ContinuationIndentWidth is not set = (default) 4 DesignatedInitializerIndentWidth = not set (implicitly 4) ContinuationIndentWidth is set = 3 DesignatedInitializerIndentWidth = not set (implicitly 3) ContinuationIndentWidth is not set = (default) 4 DesignatedInitializerIndentWidth = is set to 2 (explicitly 2) ContinuationIndentWidth is set = 3 DesignatedInitializerIndentWidth = is set to 4 (explicitly 4)
Comment Actions Agree that this makes the most sense and, luckily, that is exactly the behaviour I implemented in my most recent diff! Comment Actions I really doubt it as the creators of clang-format and a number of reviewers/contributors/maintainers from the recent past were Google engineers, I believe.
IMO it's clear that in Google style IndentWidth = 2 and ContinuationIndentWidth = 4. I think in general IndentWidth is for block indent and ContinuationIndentWidth is for everything else.
Comment Actions All actionable comments have been addressed.
Comment Actions So at the risk of adding to the number of decisions we need to come to a consensus on, I was about to update the KJ style guide to explicitly call out the difference in indentation for designated initializers when I realized that we (both KJ code authors and clang-format contributors) should consider whether users should have the option to configure other similar types of indentation following opening braces. I chatted to the owner of the KJ style guide and, whilst he did not have extremely strong opinions one way or another, he and I agreed that it probably makes more sense for such a config option to apply to other types of braced init lists. Broadly speaking, these include aggregate initialization and list initialization (possibly direct initialization with braces too). See the initialization cppref article for links to all these. As such, I would propose to actually rename DesignatedInitializerIndentWidth to BracedInitializerIndentWidth (but open to suggestiosn on exact naming) and have it apply to all the above types of initialization. What does everyone think? Comment Actions Would be okay for me. But then I want the documentation and tests show nested initialization (array of struct for example).
Comment Actions Replace DesignatedInitializerIndentWidth with BracedInitializerIndentWidth which applies to a broader range of initializers. Comment Actions I've implemented BracedInitializerIndentWidth now with significantly extended test coverage (including checking that it is not applied *too* broadly). The actual implementation is just as simple as that for DesignatedInitializerIndentWidth.
Pretty sure I would have to modify the YAML code in order to get it to output something when std::optional<unsigned> is set to std::nullopt so I have left it as-is. PTAL.
Comment Actions Following on from our discussion about new options needing motivation from public style guides, I've updated the KJ style guide to clarify its stance on braced init lists: https://github.com/capnproto/capnproto/blob/master/style-guide.md#spacing-and-bracing
Comment Actions If all comments and concerns are done, then I'm inclined to accept, but I'd like @owenpan and @HazardyKnusperkeks to give their opinion before we land this. Comment Actions Sure. Thanks!
Comment Actions Commit details as follows as per other diffs: Name: Jon Phillips Would appreciate someone committing for me. Comment Actions @jp4a50 There's a mismatch between Format.h and and the resulting rst file. I'll rerun dump_format_style.py to fix it before merging. |
Perhaps it would be better to make this a signed integer and default to -1 which would cause ContinuationIndentWidth to be used (i.e. current behaviour)?