diff --git a/src/Fantomas.Core/ASTTransformer.fs b/src/Fantomas.Core/ASTTransformer.fs index c2cef3c8dc..ac76a17d6a 100644 --- a/src/Fantomas.Core/ASTTransformer.fs +++ b/src/Fantomas.Core/ASTTransformer.fs @@ -871,17 +871,6 @@ let (|ChainExpr|_|) (e: SynExpr) : LinkExpr list option = Some(visit e id) | _ -> None -let (|AppSingleParenArg|_|) = - function - | App(SynExpr.DotGet _, [ (SynExpr.Paren(expr = SynExpr.Tuple _)) ]) -> None - | App(e, [ UnitExpr _ as px ]) -> Some(e, px) - | App(e, [ SynExpr.Paren(expr = singleExpr) as px ]) -> - match singleExpr with - | SynExpr.Lambda _ - | SynExpr.MatchLambda _ -> None - | _ -> Some(e, px) - | _ -> None - let mkParenExpr creationAide lpr e rpr m = ExprParenNode(stn "(" lpr, mkExpr creationAide e, stn ")" rpr, m) @@ -1234,13 +1223,6 @@ let mkExpr (creationAide: CreationAide) (e: SynExpr) : Expr = ExprChain(chainLinks, exprRange) |> Expr.Chain - | AppSingleParenArg(SynExpr.LongIdent(longDotId = longDotId), px) -> - ExprAppLongIdentAndSingleParenArgNode(mkSynLongIdent longDotId, mkExpr creationAide px, exprRange) - |> Expr.AppLongIdentAndSingleParenArg - | AppSingleParenArg(e, px) -> - ExprAppSingleParenArgNode(mkExpr creationAide e, mkExpr creationAide px, exprRange) - |> Expr.AppSingleParenArg - | SynExpr.App(funcExpr = App(fe, args); argExpr = ParenLambda(lpr, pats, mArrow, body, mLambda, rpr)) -> let lambdaNode = mkLambda creationAide pats mArrow body mLambda diff --git a/src/Fantomas.Core/CodePrinter.fs b/src/Fantomas.Core/CodePrinter.fs index 2f824c37fe..c88c66a910 100644 --- a/src/Fantomas.Core/CodePrinter.fs +++ b/src/Fantomas.Core/CodePrinter.fs @@ -752,26 +752,22 @@ let genExpr (e: Expr) = | Expr.Constant _ | Expr.InterpolatedStringExpr _ when not (String.startsWithOrdinal "%" node.Operator.Text) -> genSingleTextNode node.Operator +> sepSpace +> genExpr node.Expr - | Expr.AppSingleParenArg appNode -> - genSingleTextNode node.Operator - +> sepSpace - +> genExpr appNode.FunctionExpr - +> genExpr appNode.ArgExpr - | Expr.AppLongIdentAndSingleParenArg appNode -> - let mOptVarNode = appNode.FunctionName.Range - - genSingleTextNode node.Operator - +> sepSpace - +> genExpr (Expr.OptVar(ExprOptVarNode(false, appNode.FunctionName, mOptVarNode))) - +> genExpr appNode.ArgExpr | Expr.App appNode -> - match appNode.Arguments with - | [ Expr.Constant(Constant.Unit _) as argExpr ] -> - genSingleTextNode node.Operator - +> sepSpace - +> genExpr appNode.FunctionExpr - +> genExpr argExpr - | _ -> fallback + fun ctx -> + match mapAppNode ctx.Config appNode with + | AppNode.SingleParenArgument(appNode, px) -> + (genSingleTextNode node.Operator + +> sepSpace + +> genExpr appNode.FunctionExpr + +> genExpr px) + ctx + | AppNode.LongIdentAndSingleParenArgument(exprOptVarNode, parenthesesArgument) -> + (genSingleTextNode node.Operator + +> sepSpace + +> genExpr (Expr.OptVar exprOptVarNode) + +> genExpr parenthesesArgument) + ctx + | _ -> fallback ctx | _ -> fallback |> genNode node | Expr.SameInfixApps node -> @@ -853,8 +849,17 @@ let genExpr (e: Expr) = | Expr.IndexWithoutDot node -> let genIdentifierExpr = match node.Identifier with - | Expr.AppLongIdentAndSingleParenArg appNode -> genAppLongIdentAndSingleParenArgExpr sepNone appNode - | Expr.AppSingleParenArg appNode -> genAppSingleParenArgExpr sepNone appNode + | Expr.App appNode -> + fun ctx -> + match mapAppNode ctx.Config appNode with + | AppNode.SingleParenArgument(appNode, px) -> + genNode appNode (genAppSingleParenArgExpr sepNone appNode px) ctx + | AppNode.LongIdentAndSingleParenArgument(exprOptVarNode, parenthesesArgument) -> + genNode + appNode + (genAppLongIdentAndSingleParenArgExpr sepNone exprOptVarNode parenthesesArgument) + ctx + | _ -> genExpr node.Identifier ctx | _ -> genExpr node.Identifier let genIndexExpr = genExpr node.Index @@ -1015,19 +1020,6 @@ let genExpr (e: Expr) = (fun ctx -> isShortExpression ctx.Config.MaxDotGetExpressionWidth short long ctx) |> genNode node - // path.Replace("../../../", "....") - | Expr.AppLongIdentAndSingleParenArg node -> - let addSpace = - sepSpaceBeforeParenInFuncInvocation - (Expr.OptVar(ExprOptVarNode(false, node.FunctionName, node.FunctionName.Range))) - node.ArgExpr - - genAppLongIdentAndSingleParenArgExpr addSpace node - // fn (a, b, c) - | Expr.AppSingleParenArg node -> - let addSpace = sepSpaceBeforeParenInFuncInvocation node.FunctionExpr node.ArgExpr - genAppSingleParenArgExpr addSpace node - // functionName arg1 arg2 (fun x y z -> ...) | Expr.AppWithLambda node -> let sepSpaceAfterFunctionName = @@ -1055,10 +1047,10 @@ let genExpr (e: Expr) = | Expr.App node -> fun ctx -> - match node with - | EndsWithDualListApp ctx.Config (sequentialArgs: Expr list, - firstList: ExprArrayOrListNode, - lastList: ExprArrayOrListNode) -> + match mapAppNode ctx.Config node with + | AppNode.EndsWithDualList(sequentialArgs: Expr list, + firstList: ExprArrayOrListNode, + lastList: ExprArrayOrListNode) -> let genArrayOrList = genArrayOrList false // check if everything else beside the last array/list fits on one line @@ -1107,7 +1099,7 @@ let genExpr (e: Expr) = else short ctx - | EndsWithSingleListApp ctx.Config (sequentialArgs: Expr list, arrayOrList: ExprArrayOrListNode) -> + | AppNode.EndsWithSingleList(sequentialArgs: Expr list, arrayOrList: ExprArrayOrListNode) -> let genArrayOrList = genArrayOrList false // check if everything else beside the last array/list fits on one line @@ -1135,7 +1127,7 @@ let genExpr (e: Expr) = else short ctx - | EndsWithSingleRecordApp ctx.Config (sequentialArgs: Expr list, recordOrAnonRecord) -> + | AppNode.EndsWithSingleRecord(sequentialArgs: Expr list, recordOrAnonRecord) -> // check if everything else beside the last array/list fits on one line let singleLineTestExpr = genExpr node.FunctionExpr +> sepSpace +> col sepSpace sequentialArgs genExpr @@ -1160,7 +1152,17 @@ let genExpr (e: Expr) = long ctx else short ctx - | _ -> + + | AppNode.SingleParenArgument(appNode, px) -> + let addSpace = sepSpaceBeforeParenInFuncInvocation node.FunctionExpr px + genAppSingleParenArgExpr addSpace appNode px ctx + + | AppNode.LongIdentAndSingleParenArgument(exprOptVarNode, parenthesesArgument) -> + let addSpace = + sepSpaceBeforeParenInFuncInvocation (Expr.OptVar exprOptVarNode) parenthesesArgument + + genAppLongIdentAndSingleParenArgExpr addSpace exprOptVarNode parenthesesArgument ctx + | AppNode.Regular node -> let shortExpression = let sep ctx = match node.Arguments with @@ -1420,19 +1422,19 @@ let genExpr (e: Expr) = let idx = !- "." +> sepOpenLFixed +> genExpr node.IndexExpr +> sepCloseLFixed expressionFitsOnRestOfLine (short +> idx) (long +> idx) - match node.ObjectExpr with - | Expr.App appNode -> - match appNode.Arguments with - | [ Expr.Constant(Constant.Unit _) as ux ] -> - genDotIndexedGetWithApp (genExpr appNode.FunctionExpr) ux appNode - | _ -> genDotIndexedGet - | Expr.AppSingleParenArg appNode -> - genDotIndexedGetWithApp (genExpr appNode.FunctionExpr) appNode.ArgExpr appNode - - | Expr.AppLongIdentAndSingleParenArg appNode -> - genDotIndexedGetWithApp (genIdentListNode appNode.FunctionName) appNode.ArgExpr appNode - | _ -> genDotIndexedGet - |> genNode node + fun ctx -> + let genObjectExpr = + match node.ObjectExpr with + | Expr.App appNode -> + match mapAppNode ctx.Config appNode with + | AppNode.SingleParenArgument(appNode, px) -> + genDotIndexedGetWithApp (genExpr appNode.FunctionExpr) px appNode + | AppNode.LongIdentAndSingleParenArgument(exprOptVarNode, parenthesesArgument) -> + genDotIndexedGetWithApp (genIdentListNode exprOptVarNode.Identifier) parenthesesArgument appNode + | _ -> genDotIndexedGet + | _ -> genDotIndexedGet + + genNode node genObjectExpr ctx | Expr.DotIndexedSet node -> let genDotIndexedSet = addParenIfAutoNln node.ObjectExpr genExpr @@ -1456,21 +1458,19 @@ let genExpr (e: Expr) = +> idx +> autoIndentAndNlnIfExpressionExceedsPageWidthUnlessStroustrup genExpr node.Value) - match node.ObjectExpr with - | Expr.App appNode -> - match appNode.Arguments with - | [ Expr.Constant(Constant.Unit _) as ux ] -> - genDotIndexedSetWithApp (genExpr appNode.FunctionExpr) ux appNode - | _ -> genDotIndexedSet - - | Expr.AppSingleParenArg appNode -> - genDotIndexedSetWithApp (genExpr appNode.FunctionExpr) appNode.ArgExpr appNode - - | Expr.AppLongIdentAndSingleParenArg appNode -> - genDotIndexedSetWithApp (genIdentListNode appNode.FunctionName) appNode.ArgExpr appNode - - | _ -> genDotIndexedSet - |> genNode node + fun ctx -> + let genObjectExpr = + match node.ObjectExpr with + | Expr.App appNode -> + match mapAppNode ctx.Config appNode with + | AppNode.SingleParenArgument(appNode, px) -> + genDotIndexedSetWithApp (genExpr appNode.FunctionExpr) px appNode + | AppNode.LongIdentAndSingleParenArgument(exprOptVarNode, parenthesesArgument) -> + genDotIndexedSetWithApp (genIdentListNode exprOptVarNode.Identifier) parenthesesArgument appNode + | _ -> genDotIndexedSet + | _ -> genDotIndexedSet + + genNode node genObjectExpr ctx | Expr.NamedIndexedPropertySet node -> match node.Index with | Expr.ArrayOrList arrayNode when arrayNode.Elements.Length = 1 -> @@ -2001,32 +2001,37 @@ let genLambdaAux (includeClosingParen: bool) (node: ExprLambdaNode) = let genLambda = genLambdaAux false let genLambdaWithParen = genLambdaAux true -let genAppLongIdentAndSingleParenArgExpr (addSpace: Context -> Context) (node: ExprAppLongIdentAndSingleParenArgNode) = - let shortLids = genIdentListNode node.FunctionName - let short = shortLids +> addSpace +> genExpr node.ArgExpr +/// Does not invoke `genNode`, caller is expected to deal with this. +let genAppLongIdentAndSingleParenArgExpr + (addSpace: Context -> Context) + (lid: ExprOptVarNode) + (parenthesesArgument: Expr) + = + let shortLids = genIdentListNode lid.Identifier + let short = shortLids +> addSpace +> genExpr parenthesesArgument let long = let args = addSpace - +> expressionFitsOnRestOfLine (genExpr node.ArgExpr) (genMultilineFunctionApplicationArguments node.ArgExpr) + +> expressionFitsOnRestOfLine + (genExpr parenthesesArgument) + (genMultilineFunctionApplicationArguments parenthesesArgument) - ifElseCtx - (futureNlnCheck shortLids) - (genFunctionNameWithMultilineLids args node.FunctionName) - (shortLids +> args) + ifElseCtx (futureNlnCheck shortLids) (genFunctionNameWithMultilineLids args lid.Identifier) (shortLids +> args) - expressionFitsOnRestOfLine short long |> genNode node + expressionFitsOnRestOfLine short long -let genAppSingleParenArgExpr (addSpace: Context -> Context) (node: ExprAppSingleParenArgNode) = - let short = genExpr node.FunctionExpr +> addSpace +> genExpr node.ArgExpr +/// Does not invoke `genNode`, caller is expected to deal with this. +let genAppSingleParenArgExpr (addSpace: Context -> Context) (node: ExprAppNode) (argExpr: Expr) : Context -> Context = + let short = genExpr node.FunctionExpr +> addSpace +> genExpr argExpr let long ctx = let genDefaultLong = genExpr node.FunctionExpr +> addSpace - +> genMultilineFunctionApplicationArguments node.ArgExpr + +> genMultilineFunctionApplicationArguments argExpr - match node.ArgExpr with + match argExpr with | Expr.Paren parenNode when parenNode.HasContentBefore -> // We make a copy of the parenthesis argument (without the trivia being copied). // Then we check if that is multiline or not. @@ -2036,14 +2041,14 @@ let genAppSingleParenArgExpr (addSpace: Context -> Context) (node: ExprAppSingle let isSingleLineWithoutTriviaBefore = futureNlnCheck (genExpr parenNode') ctx if not isSingleLineWithoutTriviaBefore then - (genExpr node.FunctionExpr +> indentSepNlnUnindent (genExpr node.ArgExpr)) ctx + (genExpr node.FunctionExpr +> indentSepNlnUnindent (genExpr argExpr)) ctx else (genExpr node.FunctionExpr - +> indentSepNlnUnindent (genMultilineFunctionApplicationArguments node.ArgExpr)) + +> indentSepNlnUnindent (genMultilineFunctionApplicationArguments argExpr)) ctx | _ -> genDefaultLong ctx - expressionFitsOnRestOfLine short long |> genNode node + expressionFitsOnRestOfLine short long let genClauses (clauses: MatchClauseNode list) = let lastIndex = clauses.Length - 1 @@ -2309,48 +2314,52 @@ let genFunctionNameWithMultilineLids (trailing: Context -> Context) (longIdent: ) | _ -> sepNone -let (|EndsWithDualListApp|_|) (config: FormatConfig) (appNode: ExprAppNode) = - if not (config.ExperimentalElmish || config.IsStroustrupStyle) then - None - else - let mutable otherArgs = ListCollector() - - let rec visit (args: Expr list) = - match args with - | [] -> None - | [ Expr.ArrayOrList firstList; Expr.ArrayOrList lastList ] -> Some(otherArgs.Close(), firstList, lastList) - | arg :: args -> - otherArgs.Add(arg) - visit args - - visit appNode.Arguments +[] +type AppNode = + /// Example: div [ ClassName "container" ] [ str "meh" ] + | EndsWithDualList of otherArguments: Expr list * firstList: ExprArrayOrListNode * lastList: ExprArrayOrListNode + /// Example: input [ Type "text" ] + | EndsWithSingleList of otherArguments: Expr list * list: ExprArrayOrListNode + /// Example: a {| X = y |} + | EndsWithSingleRecord of otherArguments: Expr list * record: Expr + /// Example: Foo(a,b) + | SingleParenArgument of appNode: ExprAppNode * parenthesesArgument: Expr + /// Example: path.Replace("../../../", "....") + | LongIdentAndSingleParenArgument of longIdent: ExprOptVarNode * parenthesesArgument: Expr + /// Example: myFunc a b c + | Regular of appNode: ExprAppNode + +// TODO: memoize function based on the range of appNode +let mapAppNode (config: FormatConfig) (appNode: ExprAppNode) : AppNode = + let mkApp (appNode: ExprAppNode) = + match appNode.Arguments with + | [ Expr.Constant(Constant.Unit _) as px ] -> + match appNode.FunctionExpr with + | Expr.OptVar optVarNode -> AppNode.LongIdentAndSingleParenArgument(optVarNode, px) + | _ -> AppNode.SingleParenArgument(appNode, px) + | [ Expr.Paren parenExpr as px ] -> + match parenExpr.Expr with + | Expr.Lambda _ + | Expr.MatchLambda _ -> AppNode.Regular appNode + | _ -> + match appNode.FunctionExpr with + | Expr.OptVar optVarNode -> AppNode.LongIdentAndSingleParenArgument(optVarNode, px) + | _ -> AppNode.SingleParenArgument(appNode, px) + | _ -> AppNode.Regular appNode -let (|EndsWithSingleListApp|_|) (config: FormatConfig) (appNode: ExprAppNode) = if not (config.ExperimentalElmish || config.IsStroustrupStyle) then - None - else - let mutable otherArgs = ListCollector() - - let rec visit (args: Expr list) = - match args with - | [] -> None - | [ Expr.ArrayOrList singleList ] -> Some(otherArgs.Close(), singleList) - | arg :: args -> - otherArgs.Add(arg) - visit args - - visit appNode.Arguments - -let (|EndsWithSingleRecordApp|_|) (config: FormatConfig) (appNode: ExprAppNode) = - if not config.IsStroustrupStyle then - None + mkApp appNode else let mutable otherArgs = ListCollector() let rec visit (args: Expr list) = match args with - | [] -> None - | [ Expr.Record _ | Expr.AnonStructRecord _ as singleRecord ] -> Some(otherArgs.Close(), singleRecord) + | [] -> mkApp appNode + | [ Expr.ArrayOrList firstList; Expr.ArrayOrList lastList ] -> + AppNode.EndsWithDualList(otherArgs.Close(), firstList, lastList) + | [ Expr.ArrayOrList singleList ] -> AppNode.EndsWithSingleList(otherArgs.Close(), singleList) + | [ Expr.Record _ | Expr.AnonStructRecord _ as singleRecord ] -> + AppNode.EndsWithSingleRecord(otherArgs.Close(), singleRecord) | arg :: args -> otherArgs.Add(arg) visit args diff --git a/src/Fantomas.Core/Selection.fs b/src/Fantomas.Core/Selection.fs index f2e38b6eab..8dc04fb851 100644 --- a/src/Fantomas.Core/Selection.fs +++ b/src/Fantomas.Core/Selection.fs @@ -235,12 +235,6 @@ let mkTreeWithSingleNode (node: Node) : TreeForSelection = | :? ExprChain as node -> let expr = Expr.Chain node mkOakFromModuleDecl (ModuleDecl.DeclExpr expr) - | :? ExprAppLongIdentAndSingleParenArgNode as node -> - let expr = Expr.AppLongIdentAndSingleParenArg node - mkOakFromModuleDecl (ModuleDecl.DeclExpr expr) - | :? ExprAppSingleParenArgNode as node -> - let expr = Expr.AppSingleParenArg node - mkOakFromModuleDecl (ModuleDecl.DeclExpr expr) | :? ExprAppWithLambdaNode as node -> let expr = Expr.AppWithLambda node mkOakFromModuleDecl (ModuleDecl.DeclExpr expr) diff --git a/src/Fantomas.Core/SyntaxOak.fs b/src/Fantomas.Core/SyntaxOak.fs index d6a9186a2e..4ff8597dcf 100644 --- a/src/Fantomas.Core/SyntaxOak.fs +++ b/src/Fantomas.Core/SyntaxOak.fs @@ -1207,19 +1207,6 @@ type ExprChain(links: ChainLink list, range) = override val Children: Node array = List.map ChainLink.Node links |> List.toArray member val Links = links -type ExprAppLongIdentAndSingleParenArgNode(functionName: IdentListNode, argExpr: Expr, range) = - inherit NodeBase(range) - - override val Children: Node array = [| yield functionName; yield Expr.Node argExpr |] - member val FunctionName = functionName - member val ArgExpr = argExpr - -type ExprAppSingleParenArgNode(functionExpr: Expr, argExpr: Expr, range) = - inherit NodeBase(range) - override val Children: Node array = [| yield Expr.Node functionExpr; yield Expr.Node argExpr |] - member val FunctionExpr = functionExpr - member val ArgExpr = argExpr - type ExprAppWithLambdaNode ( functionName: Expr, @@ -1668,8 +1655,6 @@ type Expr = | SameInfixApps of ExprSameInfixAppsNode | InfixApp of ExprInfixAppNode | IndexWithoutDot of ExprIndexWithoutDotNode - | AppLongIdentAndSingleParenArg of ExprAppLongIdentAndSingleParenArgNode - | AppSingleParenArg of ExprAppSingleParenArgNode | AppWithLambda of ExprAppWithLambdaNode | NestedIndexWithoutDot of ExprNestedIndexWithoutDotNode | App of ExprAppNode @@ -1735,8 +1720,6 @@ type Expr = | SameInfixApps n -> n | InfixApp n -> n | IndexWithoutDot n -> n - | AppLongIdentAndSingleParenArg n -> n - | AppSingleParenArg n -> n | AppWithLambda n -> n | NestedIndexWithoutDot n -> n | App n -> n