Skip to content

Commit

Permalink
Print trivia before named argument application. Fixes #2068. (#2083)
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Feb 11, 2022
1 parent c78dbda commit b742103
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 13 deletions.
98 changes: 98 additions & 0 deletions src/Fantomas.Tests/AppTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -779,3 +779,101 @@ let makeStreamReader x y = new StreamReader(arg1=x, arg2=y)"""
"""
let makeStreamReader x y = new StreamReader(arg1 = x, arg2 = y)
"""

[<Test>]
let ``print comments before named argument application`` () =
formatSourceString
false
"""
let Ok (content: string) =
// #if API_GATEWAY || MADAPI
APIGatewayHttpApiV2ProxyResponse(
StatusCode = int HttpStatusCode.OK,
Body = content,
// #if API_GATEWAY
//Headers = Map.empty.Add("Content-Type", "text/plain")
// #else
Headers = Map.empty.Add("Content-Type", "application/json")
// #endif
)
// #else
ApplicationLoadBalancerResponse(
StatusCode = int HttpStatusCode.OK,
Body = content,
Headers = Map.empty.Add("Content-Type", "text/plain")
)
// #endif
"""
config
|> prepend newline
|> should
equal
"""
let Ok (content: string) =
// #if API_GATEWAY || MADAPI
APIGatewayHttpApiV2ProxyResponse(
StatusCode = int HttpStatusCode.OK,
Body = content,
// #if API_GATEWAY
//Headers = Map.empty.Add("Content-Type", "text/plain")
// #else
Headers = Map.empty.Add("Content-Type", "application/json")
// #endif
)
// #else
ApplicationLoadBalancerResponse(
StatusCode = int HttpStatusCode.OK,
Body = content,
Headers = Map.empty.Add("Content-Type", "text/plain")
)
// #endif
"""

[<Test>]
let ``print trivia before named argument application, 2068`` () =
formatSourceString
false
"""
let Ok (content: string) =
#if API_GATEWAY || MADAPI
APIGatewayHttpApiV2ProxyResponse(
StatusCode = int HttpStatusCode.OK,
Body = content,
#if API_GATEWAY
Headers = Map.empty.Add("Content-Type", "text/plain")
#else
Headers = Map.empty.Add("Content-Type", "application/json")
#endif
)
#else
ApplicationLoadBalancerResponse(
StatusCode = int HttpStatusCode.OK,
Body = content,
Headers = Map.empty.Add("Content-Type", "text/plain")
)
#endif
"""
config
|> prepend newline
|> should
equal
"""
let Ok (content: string) =
#if API_GATEWAY || MADAPI
APIGatewayHttpApiV2ProxyResponse(
StatusCode = int HttpStatusCode.OK,
Body = content,
#if API_GATEWAY
Headers = Map.empty.Add("Content-Type", "text/plain")
#else
Headers = Map.empty.Add("Content-Type", "application/json")
#endif
)
#else
ApplicationLoadBalancerResponse(
StatusCode = int HttpStatusCode.OK,
Body = content,
Headers = Map.empty.Add("Content-Type", "text/plain")
)
#endif
"""
17 changes: 9 additions & 8 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ and genTuple astContext es =
|> List.exists (function
| SynExpr.Match _, _
| SynExpr.Lambda _, _
| InfixApp (_, _, _, SynExpr.Lambda _), _ -> true
| InfixApp (_, _, _, SynExpr.Lambda _, _), _ -> true
| _ -> false)

let sep =
Expand All @@ -1004,7 +1004,7 @@ and genTuple astContext es =

atCurrentColumn (expressionFitsOnRestOfLine shortExpression longExpression)

and genNamedArgumentExpr (astContext: ASTContext) operatorExpr e1 e2 =
and genNamedArgumentExpr (astContext: ASTContext) operatorExpr e1 e2 appRange =
let short =
genExpr astContext e1
+> sepSpace
Expand All @@ -1022,6 +1022,7 @@ and genNamedArgumentExpr (astContext: ASTContext) operatorExpr e1 e2 =
+> unindent

expressionFitsOnRestOfLine short long
|> genTriviaFor SynExpr_App appRange

and genExpr astContext synExpr ctx =
let kw tokenName f = tokN synExpr.Range tokenName f
Expand Down Expand Up @@ -1719,7 +1720,7 @@ and genExpr astContext synExpr ctx =
fun ctx ->
atCurrentColumn (isShortExpression ctx.Config.MaxInfixOperatorExpression shortExpr multilineExpr) ctx

| InfixApp (operatorText, operatorExpr, e1, e2) ->
| InfixApp (operatorText, operatorExpr, e1, e2, _) ->
fun ctx ->
isShortExpression
ctx.Config.MaxInfixOperatorExpression
Expand Down Expand Up @@ -2819,7 +2820,7 @@ and genExprInMultilineInfixExpr astContext e =
+> sepCloseTFor rpr
|> genTriviaFor SynExpr_Paren pr)
ctx
| Paren (_, InfixApp (_, _, DotGet _, _), _, _)
| Paren (_, InfixApp (_, _, DotGet _, _, _), _, _)
| Paren (_, DotGetApp _, _, _) -> atCurrentColumnIndent (genExpr astContext e)
| MatchLambda (keywordRange, cs) ->
(!- "function "
Expand Down Expand Up @@ -2883,8 +2884,8 @@ and genMultilineFunctionApplicationArguments astContext argExpr =

let genExpr astContext e =
match e with
| InfixApp (equal, operatorExpr, e1, e2) when (equal = "=") ->
genNamedArgumentExpr astContext operatorExpr e1 e2
| InfixApp (equal, operatorExpr, e1, e2, range) when (equal = "=") ->
genNamedArgumentExpr astContext operatorExpr e1 e2 range
| _ -> genExpr astContext e

match argExpr with
Expand Down Expand Up @@ -3391,14 +3392,14 @@ and genExprInIfOrMatch astContext (e: SynExpr) (ctx: Context) : Context =
| AppParenArg app ->
genAlternativeAppWithParenthesis app astContext
|> indentNlnUnindentNln
| InfixApp (s, e, (AppParenArg app as e1), e2) ->
| InfixApp (s, e, (AppParenArg app as e1), e2, _) ->
(expressionFitsOnRestOfLine (genExpr astContext e1) (genAlternativeAppWithParenthesis app astContext)
+> ifElse (noBreakInfixOps.Contains(s)) sepSpace sepNln
+> genInfixOperator s e
+> sepSpace
+> genExpr astContext e2)
|> indentNlnUnindentNln
| InfixApp (s, e, e1, (AppParenArg app as e2)) ->
| InfixApp (s, e, e1, (AppParenArg app as e2), _) ->
(genExpr astContext e1
+> sepNln
+> genInfixOperator s e
Expand Down
10 changes: 5 additions & 5 deletions src/Fantomas/SourceParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -824,13 +824,13 @@ let (|PrefixApp|_|) =

let (|InfixApp|_|) synExpr =
match synExpr with
| SynExpr.App (_, true, (Var "::" as e), Tuple ([ e1; e2 ], _), _) -> Some("::", e, e1, e2)
| SynExpr.App (_, _, SynExpr.App (_, true, (Var s as e), e1, _), e2, _) -> Some(s, e, e1, e2)
| SynExpr.App (_, true, (Var "::" as e), Tuple ([ e1; e2 ], _), range) -> Some("::", e, e1, e2, range)
| SynExpr.App (_, _, SynExpr.App (_, true, (Var s as e), e1, _), e2, range) -> Some(s, e, e1, e2, range)
| _ -> None

let (|NewlineInfixApp|_|) =
function
| InfixApp (text, operatorExpr, e1, e2) when (newLineInfixOps.Contains(text)) -> Some(text, operatorExpr, e1, e2)
| InfixApp (text, operatorExpr, e1, e2, _) when (newLineInfixOps.Contains(text)) -> Some(text, operatorExpr, e1, e2)
| _ -> None

let (|NewlineInfixApps|_|) e =
Expand All @@ -848,13 +848,13 @@ let (|NewlineInfixApps|_|) e =
let (|SameInfixApps|_|) e =
let rec loop operator synExpr =
match synExpr with
| InfixApp (s, opE, e, e2) when (s = operator) ->
| InfixApp (s, opE, e, e2, _) when (s = operator) ->
let e1, es = loop operator e
(e1, (s, opE, e2) :: es)
| e -> (e, [])

match e with
| InfixApp (operatorText, _, _, _) ->
| InfixApp (operatorText, _, _, _, _) ->
match loop operatorText e with
| e, es when (List.length es > 1) -> Some(e, List.rev es)
| _ -> None
Expand Down

0 comments on commit b742103

Please sign in to comment.