Skip to content

Commit

Permalink
multiline non lambda argument, match lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Aug 10, 2021
1 parent 30b2a17 commit c6186e1
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 18 deletions.
27 changes: 26 additions & 1 deletion src/Fantomas.Tests/LambdaTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1043,4 +1043,29 @@ let argExpr =
|> genTriviaFor SynExpr_Paren pr
())
"""
"""

[<Test>]
let ``multiline non lambda argument, match lambda`` () =
formatSourceString
false
"""
leadingExpressionIsMultiline (sepOpenTFor lpr -- "fun "
+> pats
+> genArrowWithTrivia
(genExprKeepIndentInBranch astContext bodyExpr)
arrowRange) (function | Ok _ -> true | Error _ -> false)
"""
config
|> prepend newline
|> should
equal
"""
leadingExpressionIsMultiline
(sepOpenTFor lpr -- "fun "
+> pats
+> genArrowWithTrivia (genExprKeepIndentInBranch astContext bodyExpr) arrowRange)
(function
| Ok _ -> true
| Error _ -> false)
"""
67 changes: 50 additions & 17 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2150,23 +2150,56 @@ and genExpr astContext synExpr ctx =
singleLine ctx

| Choice2Of2 (keywordRange, cs, matchLambdaRange) ->
(genExpr astContext e
+> sepSpaceAfterFunctionName
+> col sepSpace es (genExpr astContext)
+> sepSpace
+> (sepOpenTFor lpr
+> ((!- "function "
|> genTriviaFor SynExpr_MatchLambda_Function keywordRange)
+> indent
+> sepNln
+> genClauses astContext cs
+> unindent
|> genTriviaFor SynExpr_MatchLambda matchLambdaRange)
+> sepNlnWhenWriteBeforeNewlineNotEmpty id
+> unindent
+> sepCloseTFor rpr pr)
|> genTriviaFor SynExpr_Paren pr)
ctx
let singleLineTestExpr =
genExpr astContext e
+> sepSpaceAfterFunctionName
+> col sepSpace es (genExpr astContext)
+> enterNodeFor SynExpr_Paren pr
+> sepOpenTFor lpr
+> enterNodeFor SynExpr_MatchLambda matchLambdaRange
+> enterNodeFor SynExpr_MatchLambda_Function keywordRange
+> !- "function "

let singleLine =
genExpr astContext e
+> sepSpaceAfterFunctionName
+> col sepSpace es (genExpr astContext)
+> sepSpace
+> (sepOpenTFor lpr
+> ((!- "function "
|> genTriviaFor SynExpr_MatchLambda_Function keywordRange)
+> indent
+> sepNln
+> genClauses astContext cs
+> unindent
|> genTriviaFor SynExpr_MatchLambda matchLambdaRange)
+> sepNlnWhenWriteBeforeNewlineNotEmpty id
+> unindent
+> sepCloseTFor rpr pr)
|> genTriviaFor SynExpr_Paren pr

let multiLine =
genExpr astContext e
+> indent
+> sepNln
+> col sepNln es (genExpr astContext)
+> sepNln
+> (sepOpenTFor lpr
+> atCurrentColumn (
(!- "function "
|> genTriviaFor SynExpr_MatchLambda_Function keywordRange)
+> sepNln
+> genClauses astContext cs
|> genTriviaFor SynExpr_MatchLambda matchLambdaRange
)
+> sepCloseTFor rpr pr
|> genTriviaFor SynExpr_Paren pr)
+> unindent

if futureNlnCheck singleLineTestExpr ctx then
multiLine ctx
else
singleLine ctx

expressionFitsOnRestOfLine short long

Expand Down

0 comments on commit c6186e1

Please sign in to comment.