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

Don't add newlines to short partial applications #1701

Merged
merged 2 commits into from
May 7, 2021
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
37 changes: 35 additions & 2 deletions src/Fantomas.Tests/AppTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -494,12 +494,45 @@ let ``parenthesis around composed function expression, 1341`` () =
)
"""

[<Test>]
let ``parenthesis around short composed function expression, tuple, 1700`` () =
formatSourceString false """((=) (ownerName, username))""" config
|> should
equal
"""((=) (ownerName, username))
"""

[<Test>]
let ``parenthesis around short composed function expression, tuple in if, 1700`` () =
formatSourceString false """if ((=) (ownerName, username)) then 6""" config
|> should
equal
"""if ((=) (ownerName, username)) then 6
"""

[<Test>]
let ``parenthesis around short composed function expression, no tuple, 1700`` () =
formatSourceString false """((=) ownerName)""" config
|> should
equal
"""((=) ownerName)
"""

[<Test>]
let ``parenthesis around short composed function expression, no tuple in if, 1700, part 2`` () =
formatSourceString false """if ((=) ownerName) then 6""" config
|> should
equal
"""if ((=) ownerName) then 6
"""


[<Test>]
let ``parenthesis around simple function expression`` () =
formatSourceString
false
"""
(ignore) ("Tuuuuuuuuuuuuurn tooooooooooooooooooooooo stooooooooooooooooooooooooone", 42)
(ignore) ("Tuuuuuuuuuuuuurn Tuuuuuuuuuuuuurn Tuuuuuuuuuuuuurn Tuuuuuuuuuuuuurn tooooooooooooooooooooooo stooooooooooooooooooooooooone", 42)
"""
config
|> prepend newline
Expand All @@ -508,7 +541,7 @@ let ``parenthesis around simple function expression`` () =
"""
(ignore)
(
"Tuuuuuuuuuuuuurn tooooooooooooooooooooooo stooooooooooooooooooooooooone",
"Tuuuuuuuuuuuuurn Tuuuuuuuuuuuuurn Tuuuuuuuuuuuuurn Tuuuuuuuuuuuuurn tooooooooooooooooooooooo stooooooooooooooooooooooooone",
42
)
"""
Expand Down
34 changes: 33 additions & 1 deletion src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1944,7 +1944,14 @@ and genExpr astContext synExpr ctx =
fun ctx -> isShortExpression ctx.Config.MaxDotGetExpressionWidth short long ctx

| AppParenArg (Choice1Of2 (Paren _, _, _, _, _, _) as app)
| AppParenArg (Choice2Of2 (Paren _, _, _, _, _) as app) -> genAlternativeAppWithParenthesis app astContext
| AppParenArg (Choice2Of2 (Paren _, _, _, _, _) as app) ->
let short = genAppWithParenthesis app astContext

let long =
genAlternativeAppWithParenthesis app astContext

expressionFitsOnRestOfLine short long

| AppSingleParenArg (e, px) ->
let sepSpace (ctx: Context) =
match e with
Expand Down Expand Up @@ -3263,6 +3270,19 @@ and genApp astContext e es ctx =
else
expressionFitsOnRestOfLine shortExpression longExpression ctx

and genAppWithTupledArgument (e, lpr, ts, tr, rpr, pr) astContext =
let genTupleTrivia =
match tr with
| Some tr -> genTriviaFor SynExpr_Tuple tr
| None -> id

genExpr astContext e
+> sepSpace
+> tokN lpr LPAREN sepOpenT
+> (col (sepComma) ts (genExpr astContext)
|> genTupleTrivia)
+> tokN (Option.defaultValue pr rpr) RPAREN sepCloseT

and genAlternativeAppWithTupledArgument (e, lpr, ts, tr, rpr, pr) astContext =
let genTupleTrivia =
match tr with
Expand Down Expand Up @@ -3300,11 +3320,23 @@ and genAlternativeAppWithSingleParenthesisArgument (e, lpr, a, rpr, pr) astConte
+> sepNln)
+> tokN (Option.defaultValue pr rpr) RPAREN sepCloseT)

and genAppWithSingleParenthesisArgument (e, lpr, a, rpr, pr) astContext =
genExpr astContext e
+> sepSpace
+> tokN lpr LPAREN sepOpenT
+> (genExpr astContext a)
+> tokN (Option.defaultValue pr rpr) RPAREN sepCloseT

and genAlternativeAppWithParenthesis app astContext =
match app with
| Choice1Of2 t -> genAlternativeAppWithTupledArgument t astContext
| Choice2Of2 s -> genAlternativeAppWithSingleParenthesisArgument s astContext

and genAppWithParenthesis app astContext =
match app with
| Choice1Of2 t -> genAppWithTupledArgument t astContext
| Choice2Of2 s -> genAppWithSingleParenthesisArgument s astContext

and collectMultilineItemForSynExpr (astContext: ASTContext) (e: SynExpr) : ColMultilineItem list =
match e with
| LetOrUses (bs, e) ->
Expand Down