Skip to content

Commit

Permalink
Merge pull request #244 from jindraivanek/issue-241-from-master
Browse files Browse the repository at this point in the history
Fix 241
  • Loading branch information
dungpa authored May 12, 2018
2 parents 32a7494 + 7024a0a commit 1485c19
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Fantomas.Tests/CommentTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ type IlxGenOptions =
alwaysCallVirt : bool }
"""

[<Test; Ignore "reason">]
[<Test>]
let ``should keep comments on else if``() =
formatSourceString false """
if true then ()
Expand Down
10 changes: 7 additions & 3 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -599,11 +599,15 @@ and genExpr astContext = function
// It seems too annoying to use sepSemiNln
| Sequentials es -> atCurrentColumn (col sepNln es (genExpr astContext))
// A generalization of IfThenElse
| ElIf((e1,e2, _)::es, enOpt) ->
| ElIf((e1,e2, _, _)::es, enOpt) ->
atCurrentColumn (!- "if " +> ifElse (checkBreakForExpr e1) (genExpr astContext e1 ++ "then") (genExpr astContext e1 +- "then") -- " "
+> preserveBreakNln astContext e2
+> fun ctx -> col sepNone es (fun (e1, e2, r) ->
ifElse (startWith "elif" r ctx) (!+ "elif ") (!+ "else if ")
+> fun ctx -> col sepNone es (fun (e1, e2, r, fullRange) ->
let elsePart =
ifElse (ctx.Comments.ContainsKey fullRange.Start)
(!+ "else" +> indent +> sepNln -- "if ")
(!+ "else if ")
ifElse (startWith "elif" r ctx) (!+ "elif ") elsePart
+> ifElse (checkBreakForExpr e1) (genExpr astContext e1 ++ "then") (genExpr astContext e1 +- "then")
-- " " +> preserveBreakNln astContext e2) ctx
+> opt sepNone enOpt (fun en -> !+ "else " +> preserveBreakNln astContext en))
Expand Down
8 changes: 4 additions & 4 deletions src/Fantomas/SourceParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -784,10 +784,10 @@ let (|IfThenElse|_|) = function
| _ -> None

let rec (|ElIf|_|) = function
| SynExpr.IfThenElse(e1, e2, Some(ElIf(es, e3)), _, _, r, _) ->
Some((e1, e2, r)::es, e3)
| SynExpr.IfThenElse(e1, e2, e3, _, _, r, _) ->
Some([(e1, e2, r)], e3)
| SynExpr.IfThenElse(e1, e2, Some(ElIf(es, e3)), _, _, r, fullRange) ->
Some((e1, e2, r, fullRange)::es, e3)
| SynExpr.IfThenElse(e1, e2, e3, _, _, r, fullRange) ->
Some([(e1, e2, r, fullRange)], e3)
| _ -> None

let (|Record|_|) = function
Expand Down

0 comments on commit 1485c19

Please sign in to comment.