-
Notifications
You must be signed in to change notification settings - Fork 440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Collection entry indentation inconsistency in fmt #299
Comments
@huggsboson FYI |
What do you expect it to do? Personally I find collCorrect2 to look the weirdest because there is a } immediately followed by a ] at the same indentation level. For collWeird, I say that if you choose not to start a new line, you're expecting everything to be lined up. The ] is closing the initial [ so is not subject to that environment created within the [ ]. |
Oh and yeah, in the case of someOther() being on the same line as the comma before it... in that case there is no element of the list with a linebreak preceding it, which is why it has chosen to not indent it (same as collCorrect1, but then there was only 1 element of the list so it is more obvious). |
It's kinda hard to codify rules like this... |
I would land on this:
I've largely started eschewing the [{ and }] on the same line as I keep running into edge cases like this and end up landing on.
Which takes up way more space but at least formats consistently. |
I should have been more clear in the behavior I see as surprising. I may be wrong but please bear with me. It looks to me that the second entry's location with respect to the comma affects the indentation of the first entry. That was surprising to me. Please let me know if you think that is expected behavior. Almost the same example in a more directed way and with line numbers :
You may be right. I should not have used "Weird" in the key name. Maybe the coll2 should look like this ?
But then, that implementation may break other patterns that I do not know at this point. I only wanted to point out that changing the line of the second element modifies the indentation of the first element.
Absolutely, thanks a lot for your help in making jsonnet code more beautiful. |
I see, if I am not mistaken, the " element of the list with a linebreak preceding it" is a heuristic the jsonnet fmt uses ? I can see the preceding linebreak's effect in this example, I think.
|
Related: #230 |
Yeah, it's implemented here https://github.com/google/jsonnet/blob/master/core/formatter.cpp#L1365 It may not be a good idea to have rules that complex, but it fixed some weirdnesses I was seeing in the cocktail examples. |
[x, y, z] f(x, y, z) and (x + y + z) should all behave the same in this regard |
So now I told you the heuristic, do you still thing it's surprising, and what behavior should it have? (It's actually not really a heuristic since it's just one signal driving it) |
Having thought about this for a few and watched a fair amount of jsonnet code evolve. I would probably say with at least the [ x, y, z, ] cases I'd love to see multi-line versions of them format as:
Which destroys the nice concise [{ }]. But formats really consistently as you add functions invocations in. The one big counter examples are the env vars lists from kube:
But I think I'm fine with that being transformed to:
|
I'm going to try and generalize this argument. Where C[ ] is some "outer" construct which has sub-expressions that should be indented:
(extended to any number of sub-expressions) Omitting the top-level indentation only works if every element is something that:
Some obscure examples that would be OK:
|
A separate question is whether it should merely accept or actually dictate that form. |
Oh additional rules: a) the "outer" construct has to have no newline before its first sub-expression |
yeah I was largely proposing not using the compact |
This fixes "bracebug" (reported here: google#299) and many variants of it. Of course it's not a complete solution to any newline problems. In particular it never removes or rearranges newlines, it only adds some. But it still should help a lot.
This fixes "bracebug" (reported here: google#299) and many variants of it. Of course it's not a complete solution to any newline problems. In particular it never removes or rearranges newlines, it only adds some. But it still should help a lot.
This fixes "bracebug" (reported here: google#299) and many variants of it. Of course it's not a complete solution to any newline problems. In particular it never removes or rearranges newlines, it only adds some. But it still should help a lot.
This fixes "bracebug" (reported here: google#299) and many variants of it. Of course it's not a complete solution to any newline problems. In particular it never removes or rearranges newlines, it only adds some. But it still should help a lot.
The original reported problem is fixed now. We didn't do everything we discussed here, but the discussion went much deeper than the original issue :) |
Consider the following indentations auto generated by jsonnet fmt
It looks like the someOther() function being on a new line confuses the indentation calculation for the collWeird example.
Would you consider this as a bug @sparkprime ?
The text was updated successfully, but these errors were encountered: