Skip to content

Commit

Permalink
Keep comment after elif expression before then keyword. Fixes #1606. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf authored Apr 8, 2021
1 parent bcd73e8 commit 5d1684b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 23 deletions.
54 changes: 54 additions & 0 deletions src/Fantomas.Tests/IfThenElseTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2169,3 +2169,57 @@ then
()
"""

[<Test>]
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 ""
"""
26 changes: 3 additions & 23 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5d1684b

Please sign in to comment.