Skip to content

Commit

Permalink
Fix line comment after then keyword. Fixes #730 (#768)
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf authored Apr 19, 2020
1 parent 47645b3 commit 24cd3f1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
11 changes: 11 additions & 0 deletions src/Fantomas.Tests/IfThenElseTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -904,4 +904,15 @@ module String =
else if String.length a' < String.length b' then a'
else if String.length a' > String.length b' then b'
else b'
"""

[<Test>]
let ``comment after then in if/then, 730`` () =
formatSourceString false """if true then // comment
()
""" config
|> prepend newline
|> should equal """
if true then // comment
()
"""
25 changes: 18 additions & 7 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1181,26 +1181,37 @@ and genExpr astContext synExpr =
// * https://github.com/fsprojects/fantomas/issues/513
firstNewlineOrComment es +> atCurrentColumn (colEx (fun (e:SynExpr) -> sepConsideringTriviaContentBefore sepSemiNln e.Range) es (genExpr astContext))

| IfThenElse(e1, e2, None) ->
| IfThenElse(e1, e2, None, mIfToThen) ->
fun (ctx:Context) ->
let maxWidth = ctx.Config.MaxIfThenElseShortWidth

let thenKeywordHasLineComment =
TriviaHelpers.``has content after after that matches``
(fun tn ->
match tn.Type with
| Token({ TokenInfo = ti }) when (ti.TokenName = "THEN") -> true
| _ -> false)
(function | Comment(LineCommentAfterSourceCode(_)) -> true | _ -> false)
ctx.Trivia

let thenExpr = tokN mIfToThen "THEN"

(leadingExpressionResult
(!- "if "+> genExpr astContext e1)
(!- "if " +> genExpr astContext e1)
(fun ((lb,cb),(la,ca)) ->
let thenExpressionIsMultiline = futureNlnCheck (genExpr astContext e2) ctx
let thenExpressionIsMultiline = thenKeywordHasLineComment || futureNlnCheck (genExpr astContext e2) ctx

if lb < la || thenExpressionIsMultiline then // if or then expression was multiline
!- " then" +> indent +> sepNln +> genExpr astContext e2 +> unindent
thenExpr (!- " then") +> indent +> sepNln +> genExpr astContext e2 +> unindent
elif (lb = la && (ca - cb) > maxWidth)
&& not thenExpressionIsMultiline then // if expression is longer than maxWidth but not multiline
sepNln +> !- "then " +> genExpr astContext e2
sepNln +> thenExpr (!- "then ") +> genExpr astContext e2
elif (exceedsWidth maxWidth (genExpr astContext e2) ctx)
&& not thenExpressionIsMultiline then // then is longer than maxWidth but not multiline
sepNln +> !- "then " +> genExpr astContext e2
sepNln +> thenExpr (!- "then ") +> genExpr astContext e2
else
// write out as short expression
!- " then " +> genExpr astContext e2)
thenExpr (!- " then ") +> genExpr astContext e2)
|> atCurrentColumn) ctx

// A generalization of IfThenElse
Expand Down
4 changes: 2 additions & 2 deletions src/Fantomas/SourceParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -834,8 +834,8 @@ let (|DotSet|_|) = function
| _ -> None

let (|IfThenElse|_|) = function
| SynExpr.IfThenElse(e1, e2, e3, _, _, _, _) ->
Some(e1, e2, e3)
| SynExpr.IfThenElse(e1, e2, e3, _, _, mIfToThen, _) ->
Some(e1, e2, e3, mIfToThen)
| _ -> None

let rec (|ElIf|_|) = function
Expand Down

0 comments on commit 24cd3f1

Please sign in to comment.