Repository navigation
Conversation
When fsharp_multiline_bracket_style = stroustrup, keep a single
anonymous record type argument (struct allowed) attached to < and >:
|> convertTo<{|
kind: string
|}>
Only attach when the head and opening fit on the current line and
trivia permits it; otherwise use the unchanged genPrefixApp.
Fixes fsprojects#3069
Fixes fsprojects#2860
|
Targeting v9.0 for now, assuming this formatting change is considered breaking and belongs in the next major release; please let me know if |
nojaf
left a comment
There was a problem hiding this comment.
Thanks for coming back with a much smaller PR. This is the scope I asked for, and the tests are exactly the kind I hoped for. I'm happy with the layout itself: <{| ... |}> under Stroustrup is a reasonable thing to restore.
What I'd like to change is how it's wired into CodePrinter. genApplicationWithTypeArgument is a new top-level function that sits in front of genPrefixApp and decides between two layouts. To do that it takes the type arguments twice, once as Type list and once already printed, so from its signature it's hard to tell what it prints. It also writes its own Stroustrup check (IsStroustrupStyle, a match on Type.AnonRecord, canSafelyUseStroustrup). Context.fs already expresses that rule as isStroustrupStyleType plus canSafelyUseStroustrup, and the other places that attach an anonymous record type use those (see autoIndentAndNlnTypeUnlessStroustrup).
Could you try putting the decision where the argument layout is already chosen? colGenericTypeParameters picks between the short and long forms. A single argument for which isStroustrupStyleType and canSafelyUseStroustrup hold could print there with genType directly, without the indent and trailing newline. genPrefixApp would then need to leave out the padding before > in that case. Ideally that removes the new helper altogether, and the Stroustrup rule for type arguments matches the one used everywhere else.
I haven't tried this myself, so if it turns out awkward (the > handling especially), tell me what you ran into before forcing it. I'd rather talk about it than have you work around it. Your tests can stay as they are: if the goldens still pass, the behaviour is the same.
Some smaller things I noticed in the current version. A few of them disappear with the rework, but I'd like to explain them anyway, because they're the kind of thing that makes a change in CodePrinter hard for me to accept:
- The name.
genApplicationWithTypeArgumentdoesn't tell me it printsExpr.TypeAppandType.AppPrefix, or that it's about where the record goes.CodePrinteris big, and people mostly find their way around it by function names, so a name has to say what gets printed. let standard. This binding exists only because two branches share the same fallback. When a fallback needs its own name so it can be reached from more than one place, that usually means the conditions belong in one place instead of spread over awhenguard and anif. And "standard" doesn't say what the layout is.- The prefix is written twice.
identifier +> genSingleTextNode lessThanappears once inopening(only to measure it) and again in the real output. Two copies of the same sequence will drift the first time someone changes one of them. - It skips the offside padding.
genPrefixApppads withaddFixedSpaces (startColumn + 1)so that>stays right of the identifier, which is what keeps it from being read as the comparison operator. The attached layout skips that, and|}>can end up left of the identifier. Your goldens show that this compiles, but it's the least obvious part of the change and it has no comment. That's where a comment should go: why it's safe when>follows|}. - The doc comment spends five lines restating the guard conditions, which the code already shows. A comment here is most useful when it explains a reason the code can't show, like the offside point above.
None of this is a criticism of the effort. You did what I asked, and the reason I'm careful is that every special case in CodePrinter gets read by everyone who later touches that node. Thanks again for your patience with this one!
029f9a5 to
9f7699a
Compare
|
I tried putting the decision in
Keeping the two in step would need a shared helper and How about making the decision once in Proposed
|
|
Hi @mmabdpr, thanks for the proposal, I will circle back to this next week. |
With Stroustrup, a type application whose only type argument is an
anonymous record printed it as an indented list item, with the closing
`>` on a line of its own and a space before it. It now stays attached,
`convertTo<{|`, fields, `|}>`, for expressions and types alike, as it
was before 6.3.0.
genPrefixApp makes the choice, since it already holds the padding that
keeps a `>` starting a line right of the identifier, where it cannot be
read as the comparison operator. It takes the type arguments as a list
and uses the same rule as every other place that attaches an anonymous
record type: isStroustrupStyleType and canSafelyUseStroustrup, now in
Context.fsi, decided once `<` is written so a comment after it counts.
With trivia before `>`, the list layout stays, so the `>` always
follows `|}` and needs no padding.
This is the design @mmabdpr proposed in fsprojects#3525, after the first version
there was reviewed.
Closes fsprojects#3069
Closes fsprojects#2860
Co-authored-by: Mohammad Mahdi Abdollahpour <[email protected]>
643ede4 to
19e77bd
Compare
Fixes #3069
Fixes #2860
Replaces #3523, narrowed as requested there. With Stroustrup, a type application whose only type argument is an anonymous record (
structallowed) now keeps<{|and|}>attached:The fix
A new helper,
genApplicationWithTypeArgument, replaces the directgenPrefixAppcalls inExpr.TypeAppand theType.AppPrefixbranch ofgenType.It uses the attached layout only when all of these hold:
canSafelyUseStroustrupallows it<identifier<[struct ]{|fits on the current lineIt then prints the identifier,
<, the record and>in sequence. The existing anonymous-record printer lays out the fields and|}. In every other case it calls the unchangedgenPrefixApp.Behaviour changes
<{| ... |}>) instead of being an indented generic-list item with a separate closing>. This restores the layout requested in Regression: Stroustrup bracket style in anonymous record type parameter is not respected #3069.>: the comment now follows the attached closing tokens (|}> // close). A line comment cannot sit between|}and an attached>without swallowing it. The comment is preserved on the record’s closing line.Other argument shapes and styles retain their existing layout rules. One-line output, both fallbacks—a comment after
<or an opening that does not fit—and the #2706 goldens are unchanged.Tests
scenarios/TypeAppTests/3069-single-record-type-argument: the reported Regression: Stroustrup bracket style in anonymous record type parameter is not respected #3069 example, pinning the requested pipeline layout.stroustrup/Expr/TypeApp/single-record: covers the expression-side call site in a binding, keeping the record attached rather than printing it as an indented generic-list item.stroustrup/Type/AppPrefix/single-struct-record: covers the separate type-side call site (Box<struct {| ... |}>) and verifies thatstructis supported.stroustrup/Expr/TypeApp/trivia/comment-before-greater-than: verifies that the line comment is preserved and follows the attached>.The two fallbacks were checked against the baseline but have no new snapshot cases because their output is unchanged.
Not covered
#3523 also changed the cases below. They are left out to keep this PR narrow:
f<Box<{| ... |}>>; the innerBox<Record>can still change because it independently qualifies>solely to satisfy the line-width limitAn attached layout for several anonymous-record arguments needs its own discussion, as requested in the review of #3523. This PR leaves that decision alone.