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

Print trivia before and after with keyword #1007

Merged
merged 2 commits into from
Aug 19, 2020
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
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