diff --git a/src/Fantomas.Tests/AppTests.fs b/src/Fantomas.Tests/AppTests.fs index 6adb60fad5..113fd23882 100644 --- a/src/Fantomas.Tests/AppTests.fs +++ b/src/Fantomas.Tests/AppTests.fs @@ -779,3 +779,101 @@ let makeStreamReader x y = new StreamReader(arg1=x, arg2=y)""" """ let makeStreamReader x y = new StreamReader(arg1 = x, arg2 = y) """ + +[] +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 +""" + +[] +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 +""" diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index d29b8a8197..4126720f48 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -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 = @@ -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 @@ -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 @@ -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 @@ -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 " @@ -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 @@ -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 diff --git a/src/Fantomas/SourceParser.fs b/src/Fantomas/SourceParser.fs index a665e68bf8..526f2bc3b9 100644 --- a/src/Fantomas/SourceParser.fs +++ b/src/Fantomas/SourceParser.fs @@ -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 = @@ -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