diff --git a/CHANGELOG.md b/CHANGELOG.md index ce9f0a218c..28f86947ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ ### Changed * Update FCS to 'Prototype signature files for lex files', commit 5b74995780a0e9d24a5db025e68b31e6d910f71f +### Fixed +* Missing space before arrow. [#2888](https://github.com/fsprojects/fantomas/issues/2888) + ## [6.0.3] - 2023-05-14 ### Fixed diff --git a/src/Fantomas.Core.Tests/PatternMatchingTests.fs b/src/Fantomas.Core.Tests/PatternMatchingTests.fs index 29b35ea90b..d8830d73bd 100644 --- a/src/Fantomas.Core.Tests/PatternMatchingTests.fs +++ b/src/Fantomas.Core.Tests/PatternMatchingTests.fs @@ -2321,3 +2321,26 @@ let f () = tail -> 1 | _ -> None """ + +[] +let ``multiline trivia for arrow, 2888`` () = + formatSourceString + false + """ +match subcategory with +| BuildPhaseSubcategory.Internal +// Getting here means the compiler has ICE-d. Let's not pile on by showing the unknownSubcategory assert below. +// Just treat as an unknown-to-LanguageService error. + -> false +""" + config + |> prepend newline + |> should + equal + """ +match subcategory with +| BuildPhaseSubcategory.Internal +// Getting here means the compiler has ICE-d. Let's not pile on by showing the unknownSubcategory assert below. +// Just treat as an unknown-to-LanguageService error. + -> false +""" diff --git a/src/Fantomas.Core/CodePrinter.fs b/src/Fantomas.Core/CodePrinter.fs index 8b762e8139..252cfd8102 100644 --- a/src/Fantomas.Core/CodePrinter.fs +++ b/src/Fantomas.Core/CodePrinter.fs @@ -2013,7 +2013,16 @@ let genClause (isLastItem: bool) (node: MatchClauseNode) = expressionFitsOnRestOfLine (sepSpace +> genExpr node.BodyExpr) long - (genSingleTextNodeWithSpaceSuffix sepSpace node.Arrow + let sepArrow = + if not node.Arrow.HasContentBefore then + genSingleTextNodeWithSpaceSuffix sepSpace node.Arrow + else + // In the weird case where the arrow has content before it and there is no multiline whenExpr, + // we need to ensure a space was written before the arrow to avoid offset errors. + // See https://github.com/fsprojects/fantomas/issues/2888 + !- $" {node.Arrow.Text} " |> genNode node.Arrow + + (sepArrow +> ifElse (ctx.Config.ExperimentalKeepIndentInBranch && isLastItem) genKeepIndentInBranch