From b4acb4af3df9982370a25af25c486d4a6fff70f1 Mon Sep 17 00:00:00 2001 From: nojaf Date: Thu, 8 Apr 2021 08:52:50 +0200 Subject: [PATCH] Keep comment after elif expression before then keyword. Fixes #1606. --- src/Fantomas.Tests/IfThenElseTests.fs | 54 +++++++++++++++++++++++++++ src/Fantomas/CodePrinter.fs | 26 ++----------- 2 files changed, 57 insertions(+), 23 deletions(-) diff --git a/src/Fantomas.Tests/IfThenElseTests.fs b/src/Fantomas.Tests/IfThenElseTests.fs index f8759393a9..84a25d0405 100644 --- a/src/Fantomas.Tests/IfThenElseTests.fs +++ b/src/Fantomas.Tests/IfThenElseTests.fs @@ -2169,3 +2169,57 @@ then () """ + +[] +let ``comment after then of else if, 1606`` () = + formatSourceString + false + """ +type internal Foo private () = + static member Bar : int option = + if thing = 1 then + printfn "hi" + else if + veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLong |> Seq.forall (fun (u : VeryVeryVeryVeryVeryVeryVeryLong) -> u.Length = 0) // + then + printfn "hi" + else failwith "" + +type internal Foo2 private () = + static member Bar : int option = + if thing = 1 then + printfn "hi" + else if veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLong + |> Seq.forall (fun (u: VeryVeryVeryVeryVeryVeryVeryLong) -> u.Length = 0) // + then + printfn "hi" + else + failwith "" +""" + config + |> prepend newline + |> should + equal + """ +type internal Foo private () = + static member Bar : int option = + if thing = 1 then + printfn "hi" + else if veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLong + |> Seq.forall (fun (u: VeryVeryVeryVeryVeryVeryVeryLong) -> u.Length = 0) // + then + printfn "hi" + else + failwith "" + +type internal Foo2 private () = + static member Bar : int option = + if thing = 1 then + printfn "hi" + else if veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLong + |> Seq.forall (fun (u: VeryVeryVeryVeryVeryVeryVeryLong) -> u.Length = 0) // + then + printfn "hi" + else + failwith "" +""" diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index f75f340612..16ab3d016e 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -2239,23 +2239,11 @@ and genExpr astContext synExpr ctx = let genElse ifElseRange = tokN ifElseRange ELSE (!- "else ") let genElifOneliner ((elf1: SynExpr), (elf2: SynExpr), fullRange) = - let hasCommentAfterBoolExpr = - TriviaHelpers.``has content after after that matches`` - (fun tn -> RangeHelpers.rangeEq tn.Range elf1.Range) - (function - | Comment (LineCommentAfterSourceCode _) -> true - | _ -> false) - (Map.tryFindOrEmptyList SynExpr_Ident ctx.TriviaMainNodes) - - let hasCommentAfterThenKeyword = - commentAfterKeyword THEN (RangeHelpers.``range contains`` fullRange) ctx - TriviaContext.``else if / elif`` fullRange +> genExpr astContext elf1 - +> sepSpace - +> ifElse hasCommentAfterBoolExpr sepNln sepNone + +> sepNlnWhenWriteBeforeNewlineNotEmpty sepSpace +> genThen fullRange - +> ifElse hasCommentAfterThenKeyword sepNln sepNone + +> sepNlnWhenWriteBeforeNewlineNotEmpty sepSpace +> genExpr astContext elf2 |> genTriviaFor SynExpr_IfThenElse fullRange @@ -2361,18 +2349,10 @@ and genExpr astContext synExpr ctx = indent) |> Option.defaultValue indent - let hasCommentAfterBoolExpr = - TriviaHelpers.``has content after after that matches`` - (fun tn -> RangeHelpers.rangeEq tn.Range elf1.Range) - (function - | Comment (LineCommentAfterSourceCode _) -> true - | _ -> false) - (Map.tryFindOrEmptyList SynExpr_Ident ctx.TriviaMainNodes) - let elifExpr = TriviaContext.``else if / elif`` fullRange +> genIfExpr elf1 astContext - +> ifElse hasCommentAfterBoolExpr sepNln sepSpace + +> sepNlnWhenWriteBeforeNewlineNotEmpty sepSpace +> genThen fullRange +> indentAfterThenKeyword +> sepNln