Skip to content

Commit

Permalink
Print trivia before and after with keyword. Fixes #976. (#1007)
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Aug 19, 2020
1 parent 3f0e976 commit 44eaf09
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
73 changes: 73 additions & 0 deletions src/Fantomas.Tests/PatternMatchingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -492,4 +492,77 @@ let f x =
| C -> None
| _ -> None
"""

[<Test>]
let ``very long match clause with many lambdas`` () =
formatSourceString false """
let MethInfoIsUnseen g m ty minfo =
let isUnseenByObsoleteAttrib () =
match BindMethInfoAttributes m minfo
(fun ilAttribs -> Some foo)
(fun fsAttribs -> Some bar)
(fun provAttribs -> Some(CheckProvidedAttributesForUnseen provAttribs m))
(fun _provAttribs -> None)
with
| Some res -> res
| None -> false
()
""" config
|> prepend newline
|> should equal """
let MethInfoIsUnseen g m ty minfo =
let isUnseenByObsoleteAttrib () =
match BindMethInfoAttributes
m
minfo
(fun ilAttribs -> Some foo)
(fun fsAttribs -> Some bar)
(fun provAttribs -> Some(CheckProvidedAttributesForUnseen provAttribs m))
(fun _provAttribs -> None) with
| Some res -> res
| None -> false
()
"""

[<Test>]
let ``very long match clause with many lambdas mixed with defines, 976`` () =
formatSourceString false """
let MethInfoIsUnseen g m ty minfo =
let isUnseenByObsoleteAttrib () =
match BindMethInfoAttributes m minfo
(fun ilAttribs -> Some foo)
(fun fsAttribs -> Some bar)
#if !NO_EXTENSIONTYPING
(fun provAttribs -> Some(CheckProvidedAttributesForUnseen provAttribs m))
#else
(fun _provAttribs -> None)
#endif
with
| Some res -> res
| None -> false
()
""" config
|> prepend newline
|> should equal """
let MethInfoIsUnseen g m ty minfo =
let isUnseenByObsoleteAttrib () =
match BindMethInfoAttributes
m
minfo
(fun ilAttribs -> Some foo)
(fun fsAttribs -> Some bar)
#if !NO_EXTENSIONTYPING
(fun provAttribs -> Some(CheckProvidedAttributesForUnseen provAttribs m))
#else
(fun _provAttribs -> None)
#endif
with
| Some res -> res
| None -> false
()
"""
19 changes: 17 additions & 2 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,15 @@ and genExpr astContext synExpr =
+> autoIndentAndNlnIfExpressionExceedsPageWidth (genExpr astContext e))
| MatchLambda(sp, _) -> !- "function " +> colPre sepNln sepNln sp (genClause astContext true)
| Match(e, cs) ->
atCurrentColumn (!- "match " +> genExpr astContext e -- " with" +> colPre sepNln sepNln cs (genClause astContext true))
atCurrentColumn
(!- "match "
+> genExpr astContext e
+> enterNodeTokenByName synExpr.Range "WITH"
// indent 'with' further if trivia was printed so that is appear after the match keyword.
+> ifElseCtx lastWriteEventIsNewline (rep 5 !- " ") sepNone
-- " with"
+> leaveNodeTokenByName synExpr.Range "WITH"
+> colPre sepNln sepNln cs (genClause astContext true))
| MatchBang(e, cs) ->
atCurrentColumn (!- "match! " +> genExpr astContext e -- " with" +> colPre sepNln sepNln cs (genClause astContext true))
| TraitCall(tps, msg, e) ->
Expand Down Expand Up @@ -1511,14 +1519,21 @@ and genExpr astContext synExpr =
+> genExpr astContext e
+> unindent))))

let hasThreeOrMoreLamdbas =
List.filter (function
| Paren (Lambda (_)) -> true
| _ -> false) es
|> List.length
|> fun l -> l >= 3

if List.exists (function
| Lambda _
| MatchLambda _
| Paren (Lambda (_))
| Paren (MatchLambda (_))
| MultilineString _
| CompExpr _ -> true
| _ -> false) es then
| _ -> false) es && not hasThreeOrMoreLamdbas then
shortExpression
else
expressionFitsOnRestOfLine shortExpression longExpression
Expand Down

0 comments on commit 44eaf09

Please sign in to comment.