Skip to content
Closed
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
18 changes: 0 additions & 18 deletions src/Fantomas.Core/ASTTransformer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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

Expand Down
249 changes: 129 additions & 120 deletions src/Fantomas.Core/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 ->
Expand Down Expand Up @@ -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
/// <remarks>Does not invoke `genNode`, caller is expected to deal with this.</remarks>
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
/// <remarks>Does not invoke `genNode`, caller is expected to deal with this.</remarks>
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.
Expand All @@ -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
Expand Down Expand Up @@ -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<Expr>()

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
[<RequireQualifiedAccess>]
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is memoizing something you'd like specific to this task, or is that a general improvement you'd like to make at some point?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifically in this case.
During the pipeline of CodePrinter on multiple occasions, we need to transform ExprAppNode to AppNode. This transformation is always going to be the same (in a single run), so it should not be recalculated each time we invoke the function.

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<Expr>()

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<Expr>()

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
Expand Down
Loading