Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep comment after elif expression before then keyword. #1615

Merged
merged 1 commit into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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