Skip to content

Commit

Permalink
Proposal to split parameters of function application over multiple li…
Browse files Browse the repository at this point in the history
…nes.
  • Loading branch information
nojaf committed Jun 26, 2020
1 parent b4444b8 commit 6a28f6f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 50 deletions.
8 changes: 2 additions & 6 deletions src/Fantomas.Tests/AppTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,15 @@ let a s =
|> should equal @"
let a s =
if s <> """" then
printfn
""""""fooo
printfn """"""fooo
%s
%s
%s
%s""""""
(llloooooooooooooooooooooooooo s)
s
%s"""""" (llloooooooooooooooooooooooooo s) s
(llloooooooooooooooooooooooooo s)
(llloooooooooooooooooooooooooo s)
"


[<Test>]
let ``should split parameters over multiple lines when they exceed page width``() =
formatSourceString false """module Caching =
Expand Down
8 changes: 5 additions & 3 deletions src/Fantomas.Tests/ElmishTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,11 @@ table [ ClassName "table table-striped table-hover mb-0" ] [
tokenDetailRow "ColorClass" (str colorClass)
tokenDetailRow "CharClass" (str charClass)
tokenDetailRow "Tag" (ofInt tag)
tokenDetailRow "FullMatchedLength" (span [ ClassName "has-text-weight-semibold" ] [
ofInt fullMatchedLength
])
tokenDetailRow
"FullMatchedLength"
(span [ ClassName "has-text-weight-semibold" ] [
ofInt fullMatchedLength
])
]
]
"""
Expand Down
61 changes: 20 additions & 41 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1322,48 +1322,17 @@ and genExpr astContext synExpr =
else
sepSpace ctx

// This checks if there is an application with multiple parameters which would fit in one line
// but at least one of the parameters which is not the last parameter has a line comment
// In that case we have to split the arguments over multiple lines to preserve the line comments
// Example:
//
// someFunc a b c // comment
//
// This can stay as is because the last argument has the line comment
// However:
//
// someFunc
// a
// b // comment
// c
//
// This has to be split over multiple lines to preserve the line comment
let parametersMustBeNewlinedBecauseOfTriviaContent es ctx =
es
|> List.map (fun (e:SynExpr) -> TriviaHelpers.findByRange ctx.Trivia e.Range)
|> List.mapi (fun i node -> if node.IsSome && not (i = List.length es - 1) then TriviaHelpers.``has line comment after`` node.Value else false)
|> List.contains true

fun ctx ->
let shortExpression =
(atCurrentColumn
(genExpr astContext e
+> colPre sepSpace sepSpace es (fun e ->
ifElse (isCompExpr e)
(sepSpace
+> sepOpenSFixed
+> sepSpace
let shortExpression =
atCurrentColumn
(genExpr astContext e
+> colPre sepSpace sepSpace es (fun e ->
onlyIf (isCompExpr e) (sepSpace +> sepOpenSFixed +> sepSpace)
+> indent
+> appNlnFun e (indentIfNeeded +> genExpr astContext e)
+> unindent)
(indent
+> indentIfNeeded
+> sepNln
+> genExpr astContext e
+> unindent))))
+> unindent))

let longExpression =
(atCurrentColumn
let longExpression =
(atCurrentColumn
(genExpr astContext e
+> colPre sepSpace sepSpace es (fun e ->
ifElse (isCompExpr e)
Expand All @@ -1375,11 +1344,21 @@ and genExpr astContext synExpr =
+> unindent)
(indent
+> indentIfNeeded
+> sepNln
+> genExpr astContext e
+> unindent))))

//ifElseCtx (fun ctx -> exceedsWidth ctx.Config.PageWidth longExpression ctx || parametersMustBeNewlinedBecauseOfTriviaContent es ctx) shortExpression longExpression ctx
ifElseCtx (fun ctx -> parametersMustBeNewlinedBecauseOfTriviaContent es ctx) shortExpression (expressionFitsOnRestOfLine longExpression shortExpression) ctx
if List.exists (function
| Lambda _
| MatchLambda _
| Paren (Lambda (_))
| Paren (MatchLambda (_))
| MultilineString _
| CompExpr _ -> true
| _ -> false) es then
shortExpression
else
expressionFitsOnRestOfLine shortExpression longExpression

| TypeApp(e, ts) -> genExpr astContext e -- "<" +> col sepComma ts (genType astContext false) -- ">"
| LetOrUses(bs, e) ->
Expand Down

0 comments on commit 6a28f6f

Please sign in to comment.