From 79f59e0892106bf03b26045c5043f8bec81567d1 Mon Sep 17 00:00:00 2001 From: nojaf Date: Fri, 16 Aug 2024 09:40:46 +0200 Subject: [PATCH] Add space around dots when negative floats are inside Expr.TripleNumberIndexRange --- CHANGELOG.md | 5 +++++ src/Fantomas.Core.Tests/ListTests.fs | 14 ++++++++++++++ src/Fantomas.Core/CodePrinter.fs | 13 +++++++++++-- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 891db0e0ea..aab8e30599 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 6.3.11 - 2024-08-16 + +### Fixed +* F# Range Operator (\.\.) with negative numbers produces invalid F# code and idempotent. [#3105](https://github.com/fsprojects/fantomas/issues/3105) + ## 6.3.10 - 2024-07-12 ### Changed diff --git a/src/Fantomas.Core.Tests/ListTests.fs b/src/Fantomas.Core.Tests/ListTests.fs index a60a107ecc..9c9704fdeb 100644 --- a/src/Fantomas.Core.Tests/ListTests.fs +++ b/src/Fantomas.Core.Tests/ListTests.fs @@ -2292,3 +2292,17 @@ let fns = Functions[Checked false OnChange(fun _ -> s |> updateSettings)] """ + +[] +let ``negative numbers in range operator, 3105`` () = + formatSourceString + """ +[|-24.0 .. -1.0 .. -30.0|] +""" + config + |> prepend newline + |> should + equal + """ +[| -24.0 .. -1.0 .. -30.0 |] +""" diff --git a/src/Fantomas.Core/CodePrinter.fs b/src/Fantomas.Core/CodePrinter.fs index f1b58cbda4..d78a975fd4 100644 --- a/src/Fantomas.Core/CodePrinter.fs +++ b/src/Fantomas.Core/CodePrinter.fs @@ -1546,10 +1546,19 @@ let genExpr (e: Expr) = |> genNode node | Expr.IndexRangeWildcard node -> genSingleTextNode node | Expr.TripleNumberIndexRange node -> + let isNegativeFloat (text: string) = + text.StartsWith("-", StringComparison.Ordinal) && text.Contains(".") + + let genDots (before: SingleTextNode) (dots: SingleTextNode) (after: SingleTextNode) = + if not (isNegativeFloat before.Text) && not (isNegativeFloat after.Text) then + genSingleTextNode dots + else + sepSpace +> genSingleTextNode dots +> sepSpace + genSingleTextNode node.Start - +> genSingleTextNode node.StartDots + +> genDots node.Start node.StartDots node.Center +> genSingleTextNode node.Center - +> genSingleTextNode node.EndDots + +> genDots node.Center node.EndDots node.EndDots +> genSingleTextNode node.End |> genNode node | Expr.IndexRange node ->