Skip to content

Respect Stroustrup style for anonymous-record type arguments - #3523

Closed
mmabdpr wants to merge 1 commit into
fsprojects:v9.0from
mmabdpr:fix-2860
Closed

mmabdpr wants to merge 1 commit into
fsprojects:v9.0from
mmabdpr:fix-2860

Conversation

@mmabdpr

@mmabdpr mmabdpr commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Fixes #2860
Fixes #3069

Summary

  • Keep anonymous-record type arguments attached to the surrounding generic brackets when using Stroustrup style and the layout allows it.
  • Fix the extra space before > reported in Space inserted before final > in type parameter #2860 and the regression reported in Regression: Stroustrup bracket style in anonymous record type parameter is not respected #3069, where the record fell back to the aligned layout for a multiline anonymous record even though Stroustrup was configured (a regression from Fantomas 6.3.0).
  • Apply the same layout rules to generic function calls and generic types, including type abbreviations and signatures.
  • Handle single, multiple and mixed type arguments, as well as struct records, parentheses, arrays and nested generic types.

Before and after

With fsharp_multiline_bracket_style = stroustrup, the example from #3069 currently becomes:

let private asJson (arm: IArmResource) =
    arm.JsonModel
    |> convertTo<
        {|
            kind: string
            properties: {| statisticsEnabled: bool |}
        |}
        >

This PR gives the requested layout:

let private asJson (arm: IArmResource) =
    arm.JsonModel
    |> convertTo<{|
        kind: string
        properties: {| statisticsEnabled: bool |}
    |}>
  • The opening stays together as <{|, and the closing stays together as |}>.
  • This also fixes Space inserted before final > in type parameter #2860's original |} > ending. On the current base, that example instead has a detached >; the fix restores the full requested layout, rather than only removing a space.

Why this happened

  • The generic-list printer tried to fit the whole argument list on one line.
  • A multiline anonymous record made that trial fail, even when its opening bracket would fit after <.
  • The fallback then moved all arguments below < and put > on another line, without a Stroustrup-aware choice at each argument boundary.
  • Simply removing the closing padding would not fix the opening layout. It could also remove indentation needed to keep a standalone > valid F#.

How the fix works

  • Share the generic application layout. Both Expr.TypeApp and Type.AppPrefix use genGenericApplication, so expression and type positions follow the same rules.
  • Measure the opening, not the whole record. For supported record arguments and wrappers, check whether the opening fits on the current line. Then print the complete argument with the existing genType printer. Record fields still follow the existing width and record-size settings.
  • Choose breaks one argument at a time. Keep the next opening beside the previous argument when it fits; otherwise, use an indented continuation line. One narrow boundary does not force every argument onto a separate line. Comma spacing still follows SpaceAfterComma.
  • Place > from the actual output position. Keep it attached when possible, and reserve room for it when measuring a final ordinary type argument. If width or comments require a separate line, retain the indentation needed by F#'s offside rule.
  • Handle token comments before positioning. Print leading comments before deciding the closing angle's final position, then capture its cursor position. Shared opening-token printing also keeps record-opening comments and cursors on their actual token nodes.
  • Keep a conservative fallback. If an ordinary argument needs several lines and has no supported opening, discard the attached attempt and use the existing generic-list layout. Restore both the writer events and the original cursor context.
  • Do not bypass outer layout trials. Newlines still reject an enclosing one-line trial; the new printer does not hide them or change the trial's mode.

Scope and compatibility

  • Target v9.0 because this changes Stroustrup output. Aligned and Cramped keep the existing generic-list layout; no new setting or public API change.
  • Multiple and mixed record arguments use the attached layout too. Update two existing Aligned bracket style in anonymous record is not respected #2706 snapshots to show that change.
  • Add regression tests for both issues, wrappers and nested types, signatures, width limits, spacing, comments, directives, cursor positions and fallback layouts.
  • A before/after soak check across three Stroustrup codebases (FSharp.Collections.Immutable, FsToolkit.ErrorHandling, Lanayx/Oxpecker) found no unexpected changes: zero output differences in the first two, and in Oxpecker — which uses anonymous-record type arguments in real code (serializer.Deserialize<{| ... |}>(...) in tests/Oxpecker.Tests/Json.Tests.fs) — exactly one hunk, converting the broken layout to the attached one. The fix output was idempotent and dotnet build Oxpecker.slnx passed from the formatted tree.
  • The solution build, formatting checks and full analyzers passed. All five test projects passed too: Fantomas.Core.Tests, Fantomas.Core.SnapshotTests, Fantomas.Tests, Fantomas.Client.Tests and Fantomas.Analyzers.Tests.

- Share the layout across function calls and type annotations,
  including multiple, mixed, and nested type arguments.
- Preserve comments, directives, required spaces, and cursor positions.

How the fix works:
- Measure argument openings instead of requiring the whole argument
  to fit on one line. Choose line breaks at each argument boundary.
- Keep the closing angle attached when possible. Apply indentation
  only when it needs its own line, after printing leading comments
  and before recording its cursor position.
- If an unsupported type needs multiple lines, discard the attempted
  layout and use the existing generic layout.

Add regression tests for signatures, wrapped records, width limits,
comments, directives, cursor positions, and fallback layouts.

Fixes fsprojects#2860
Fixes fsprojects#3069
@nojaf

nojaf commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Thanks for digging into this, but I'm closing this PR: the scope is far too large for me to review.

#3069 reports one thing: with Stroustrup, a single anonymous record as the only type argument should keep <{| and |}> attached. (#2860 is the same report from before; on v9.0 its example already gives the #3069 layout.) This PR also changes several records, mixed arguments, parens, arrays, nested generics, SRTP and verbatim-string padding, closing-angle width handling, cursor capture in genNode, and the genType printers for anonymous records and parens. On top of that come 44 snapshot cases and 8 cursor tests. I didn't ask for most of this, and I'd have to maintain all of it.

Some of it changes output no issue asks for. For example, the #2706 goldens with two records. How several anonymous records lay out under Stroustrup has never been discussed or decided. If you think that should change, open an issue for it. A fix PR is not the place to decide it.

If you want to try again, please open a new PR with the smallest change that fixes #3069:

  • Stroustrup only, when there is exactly one type argument and it is an anonymous record (struct allowed). In every other case the existing layout stays as it is.
  • Don't touch genNode, cursor capture, or the Type.AnonRecord / Type.Paren printers in genType.
  • The Aligned bracket style in anonymous record is not respected #2706 goldens must not change, and neither may any one-line output.
  • Tests: one scenarios/ case with the Regression: Stroustrup bracket style in anonymous record type parameter is not respected #3069 example. Beyond that, only targeted cases under settings/fsharp_multiline_bracket_style/stroustrup/<Node>/ or oak/<Node>/, one per behaviour you actually change: Expr/TypeApp, Type/AppPrefix, the fallback when it doesn't fit, and one trivia/ case for a comment before >. Leave out any case that gives the same output before and after your change, and add no cursor tests.
  • Changelog: one entry per issue, using the issue title.

An occasional small PR that touches as little as possible is very welcome and easy for me to review. A large one isn't, however well it's tested. When in doubt, open a draft early and ask first, as Pull request ground rules asks.

@nojaf nojaf closed this Oct 7, 2026
@mmabdpr

mmabdpr commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

That's totally fair. I'll open a new PR with the minimal fix you described.

Separately, I think these may be worth discussing:

  • Stroustrup layout for multiple or mixed anonymous-record type arguments.
  • Attachment when the record argument is wrapped in parentheses or an array type.
  • Attachment across nested generic applications.
  • What to do when the final > alone exceeds the line-width limit.
  • The existing cursor-position loss inside a multiline anonymous-record opening delimiter.

I checked the open issues and didn't find exact duplicates. Any new issues would reference the related reports and distinguish themselves from the singleton fix in #3069 and #2860.

These would stay out of the replacement PR. If you think any are worth discussing, let me know and I'll open issues for them.

For the Stroustrup layout questions, would you prefer one umbrella issue or separate focused issues for each behaviour? I'd keep the cursor problem separate.

@nojaf

nojaf commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Thanks for understanding and making a smaller PR for this 🚀

The four layout questions are style questions, and I have very little appetite for deciding style in this repository. As the [Style guide](https://fsprojects.github.io/fantomas/docs/end-users/StyleGuide.html) page explains, those conversations belong at [fsharp/fslang-design](https://github.com/fsharp/fslang-design#style-guide). If one of them gets settled there and lands in the style guide, Fantomas can follow it. Please don't open issues for them here.

The cursor position is a real bug, I agree. But I haven't seen anyone run into it, and the worst it does is put the cursor a line or so from where it should be. You're free to open an issue for it, but I won't prioritize if there is no real user impact attached to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants