Add fsharp_keep_single_case_union_multiline setting#3377
Open
philipcraig wants to merge 2 commits into
Open
Conversation
Add a new boolean configuration option that, when enabled, keeps a single-case discriminated union declaration on its own line instead of collapsing it onto the same line as the type name when it is short enough to fit. The option defaults to false, preserving the existing behaviour. It is wired through the FormatConfig record (and is therefore picked up automatically by the .editorconfig reflection-based parser/serializer) and applied in the single-case union branch of genTypeDefn in CodePrinter. Signature files share the same code path. Includes documentation and regression/idempotency tests. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
philipcraig
marked this pull request as ready for review
July 11, 2026 16:28
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds an opt-in setting
fsharp_keep_single_case_union_multiline(defaultfalse, so existing output is unchanged). When enabled, a single-case discriminated union declaration is kept on its own line instead of being collapsed onto the type name when it fits:Why
The goal is to let users uphold the G-Research F# formatting conventions, which list the multiline layout as the Preferred form for a single-case DU:
Today Fantomas collapses a short single-case DU onto one line — even with
fsharp_bar_before_discriminated_union_declaration = trueit producestype Foo = | Foo of int— so that Preferred multiline form only appears once the declaration is long enough to wrap. This setting lets people following those conventions get the Preferred shape consistently, and (independently) gives teams the uniform "one case per line regardless of case count" style, which also keeps diffs smaller when a second case is later added.It is opt-in and off by default, so nothing changes for anyone who doesn't set it. Related prior discussion: fsharp/fslang-design#762.
How
FormatConfig: new fieldKeepSingleCaseUnionMultiline(+ default). The editorconfig key is derived by reflection, so parsing/serialization are automatic.CodePrinter: in the single-case branch ofgenTypeDefn(TypeDefn.Union), use the existing multiline layout when the flag is set instead ofexpressionFitsOnRestOfLine. Signature files share this path.Tested
KeepSingleCaseUnionMultilineTests.fs(8 tests: fields / no-field / access modifier / bar-combination / multi-case unaffected / member case / signature file / idempotency).Fantomas.Core.Testsgreen (2849 passed); changed files passfantomas --check.Happy to bikeshed the setting name.