Skip to content

Commit

Permalink
Put multiline if expression with comment on next line. Fixes fsprojec…
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Apr 4, 2021
1 parent abb3128 commit 24b1dc1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
47 changes: 47 additions & 0 deletions src/Fantomas.Tests/IfThenElseTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2122,3 +2122,50 @@ then
else
return formattedSourceCode
"""

[<Test>]
let ``line comment before multiline if expression, 1588`` () =
formatSourceString
false
"""
if
// Don't support implicit [<ReflectedDefinition>] on generated members, except the implicit members
// for 'let' bound functions in classes.
(not v.IsCompilerGenerated || v.IsIncrClassGeneratedMember) &&
(// Check the attributes on any enclosing module
env.reflect ||
// Check the attributes on the value
HasFSharpAttribute g g.attrib_ReflectedDefinitionAttribute v.Attribs ||
// Also check the enclosing type for members - for historical reasons, in the TAST member values
// are stored in the entity that encloses the type, hence we will not have noticed the ReflectedDefinition
// on the enclosing type at this point.
HasFSharpAttribute g g.attrib_ReflectedDefinitionAttribute v.TopValDeclaringEntity.Attribs) then
()
"""
config
|> prepend newline
|> should
equal
"""
if
// Don't support implicit [<ReflectedDefinition>] on generated members, except the implicit members
// for 'let' bound functions in classes.
(not v.IsCompilerGenerated
|| v.IsIncrClassGeneratedMember)
&&
(env.reflect // Check the attributes on any enclosing module
||
// Check the attributes on the value
HasFSharpAttribute g g.attrib_ReflectedDefinitionAttribute v.Attribs
||
// Also check the enclosing type for members - for historical reasons, in the TAST member values
// are stored in the entity that encloses the type, hence we will not have noticed the ReflectedDefinition
// on the enclosing type at this point.
HasFSharpAttribute g g.attrib_ReflectedDefinitionAttribute v.TopValDeclaringEntity.Attribs)
then
()
"""
18 changes: 17 additions & 1 deletion src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2253,6 +2253,14 @@ and genExpr astContext synExpr ctx =
let short = genExpr astContext e

let long =
let hasCommentBeforeExpr () =
TriviaHelpers.``has content before that matches``
(fun tn -> RangeHelpers.rangeEq tn.Range e.Range)
(function
| Comment (LineCommentOnSingleLine _) -> true
| _ -> false)
(Map.tryFindOrEmptyList (synExprToFsAstType e |> fst) ctx.TriviaMainNodes)

match e with
| App (SynExpr.DotGet _, [ (Paren _) ]) -> atCurrentColumn (genExpr astContext e)
| Paren (lpr, (AppSingleParenArg _ as ate), rpr, pr) ->
Expand Down Expand Up @@ -2316,7 +2324,15 @@ and genExpr astContext synExpr ctx =
+> genExpr astContext e
+> unindent
+> sepNln
| _ -> genExpr astContext e
| _ ->
if hasCommentBeforeExpr () then
indent
+> sepNln
+> genExpr astContext e
+> unindent
+> sepNln
else
genExpr astContext e

expressionFitsOnRestOfLine short long

Expand Down

0 comments on commit 24b1dc1

Please sign in to comment.