Skip to content

Commit

Permalink
Refactor IsStroustrupStyleExpr and isStroustrupStyleType into Context…
Browse files Browse the repository at this point in the history
….fs (#2756)
  • Loading branch information
josh-degraw authored and nojaf committed Mar 27, 2023
1 parent 96286a4 commit 2b479be
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 53 deletions.
18 changes: 5 additions & 13 deletions src/Fantomas.Core/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ let genExpr (e: Expr) =
onlyIf
(isMultiline
&& ctx.Config.MultiLineLambdaClosingNewline
&& not (ctx.Config.ExperimentalStroustrupStyle && node.Lambda.Expr.IsStroustrupStyleExpr))
&& not (isStroustrupStyleExpr ctx.Config node.Lambda.Expr))
sepNln
ctx)
+> genSingleTextNode node.ClosingParen
Expand Down Expand Up @@ -1052,9 +1052,7 @@ let genExpr (e: Expr) =
+> onlyIfCtx
(fun ctx ->
ctx.Config.MultiLineLambdaClosingNewline
&& (not (
ctx.Config.ExperimentalStroustrupStyle && lambdaNode.Expr.IsStroustrupStyleExpr
)))
&& (not (isStroustrupStyleExpr ctx.Config lambdaNode.Expr)))
sepNln
+> genSingleTextNode appParen.Paren.ClosingParen
| _ ->
Expand Down Expand Up @@ -1928,7 +1926,7 @@ let genClause (isLastItem: bool) (node: MatchClauseNode) =
ctx)

let genPatAndBody ctx =
if ctx.Config.ExperimentalStroustrupStyle && node.BodyExpr.IsStroustrupStyleExpr then
if isStroustrupStyleExpr ctx.Config node.BodyExpr then
let startColumn = ctx.Column
(genPatInClause node.Pattern +> atIndentLevel false startColumn genWhenAndBody) ctx
else
Expand Down Expand Up @@ -2187,9 +2185,7 @@ let genAppWithLambda sep (node: ExprAppWithLambdaNode) =
| Choice1Of2 lambdaNode ->
genSingleTextNode node.OpeningParen
+> (genLambdaWithParen lambdaNode |> genNode lambdaNode)
+> onlyIf
(not (ctx.Config.ExperimentalStroustrupStyle && lambdaNode.Expr.IsStroustrupStyleExpr))
sepNln
+> onlyIf (not (isStroustrupStyleExpr ctx.Config lambdaNode.Expr)) sepNln
+> genSingleTextNode node.ClosingParen
| Choice2Of2 matchLambdaNode ->
genSingleTextNode node.OpeningParen
Expand All @@ -2211,11 +2207,7 @@ let genAppWithLambda sep (node: ExprAppWithLambdaNode) =
+> (genLambdaWithParen lambdaNode |> genNode lambdaNode))
(fun isMultiline ->
onlyIf
(isMultiline
&& not (
ctx.Config.ExperimentalStroustrupStyle
&& lambdaNode.Expr.IsStroustrupStyleExpr
))
(isMultiline && not (isStroustrupStyleExpr ctx.Config lambdaNode.Expr))
sepNln
+> genSingleTextNode node.ClosingParen)
| Choice2Of2 matchLambdaNode ->
Expand Down
62 changes: 42 additions & 20 deletions src/Fantomas.Core/Context.fs
Original file line number Diff line number Diff line change
Expand Up @@ -752,21 +752,51 @@ let sepSpaceOrDoubleIndentAndNlnIfExpressionExceedsPageWidth expr (ctx: Context)
expr
ctx

let isStroustrupStyleExpr (config: FormatConfig) (e: Expr) =
let isStroustrupEnabled = config.MultilineBracketStyle = ExperimentalStroustrup

match e with
| Expr.Record node when isStroustrupEnabled ->
match node.Extra with
| RecordNodeExtra.Inherit _ -> false
| RecordNodeExtra.With _
| RecordNodeExtra.None -> true
| Expr.AnonRecord _ when isStroustrupEnabled -> true
| Expr.NamedComputation node when isStroustrupEnabled ->
match node.Name with
| Expr.Ident _ -> true
| _ -> false
| Expr.ArrayOrList _ when isStroustrupEnabled -> true
| _ -> false

let isStroustrupStyleType (config: FormatConfig) (t: Type) =
let isStroustrupEnabled = config.MultilineBracketStyle = ExperimentalStroustrup

match t with
| Type.AnonRecord _ when isStroustrupEnabled -> true
| _ -> false

let canSafelyUseStroustrup (node: Node) = not node.HasContentBefore

let sepSpaceOrIndentAndNlnIfExceedsPageWidthUnlessStroustrup isStroustrup f (node: Node) (ctx: Context) =
if
ctx.Config.ExperimentalStroustrupStyle
&& isStroustrup
&& Seq.isEmpty node.ContentBefore
then
if isStroustrup && canSafelyUseStroustrup node then
(sepSpace +> f) ctx
else
sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidth f ctx

let sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidthUnlessStroustrup f (expr: Expr) =
sepSpaceOrIndentAndNlnIfExceedsPageWidthUnlessStroustrup expr.IsStroustrupStyleExpr (f expr) (Expr.Node expr)
let sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidthUnlessStroustrup f (expr: Expr) (ctx: Context) =
sepSpaceOrIndentAndNlnIfExceedsPageWidthUnlessStroustrup
(isStroustrupStyleExpr ctx.Config expr)
(f expr)
(Expr.Node expr)
ctx

let sepSpaceOrIndentAndNlnIfTypeExceedsPageWidthUnlessStroustrup f (t: Type) =
sepSpaceOrIndentAndNlnIfExceedsPageWidthUnlessStroustrup t.IsStroustrupStyleType (f t) (Type.Node t)
let sepSpaceOrIndentAndNlnIfTypeExceedsPageWidthUnlessStroustrup f (t: Type) (ctx: Context) =
sepSpaceOrIndentAndNlnIfExceedsPageWidthUnlessStroustrup
(isStroustrupStyleType ctx.Config t)
(f t)
(Type.Node t)
ctx

let autoNlnIfExpressionExceedsPageWidth expr (ctx: Context) =
expressionExceedsPageWidth
Expand Down Expand Up @@ -907,10 +937,7 @@ let addParenIfAutoNln expr f =

let autoIndentAndNlnExpressUnlessStroustrup (f: Expr -> Context -> Context) (e: Expr) (ctx: Context) =
let shouldUseStroustrup =
ctx.Config.ExperimentalStroustrupStyle
&& e.IsStroustrupStyleExpr
&& let node = Expr.Node e in
Seq.isEmpty node.ContentBefore
isStroustrupStyleExpr ctx.Config e && canSafelyUseStroustrup (Expr.Node e)

if shouldUseStroustrup then
f e ctx
Expand All @@ -919,10 +946,7 @@ let autoIndentAndNlnExpressUnlessStroustrup (f: Expr -> Context -> Context) (e:

let autoIndentAndNlnTypeUnlessStroustrup (f: Type -> Context -> Context) (t: Type) (ctx: Context) =
let shouldUseStroustrup =
ctx.Config.ExperimentalStroustrupStyle
&& t.IsStroustrupStyleType
&& let node = Type.Node t in
Seq.isEmpty node.ContentBefore
isStroustrupStyleType ctx.Config t && canSafelyUseStroustrup (Type.Node t)

if shouldUseStroustrup then
f t ctx
Expand All @@ -935,9 +959,7 @@ let autoIndentAndNlnIfExpressionExceedsPageWidthUnlessStroustrup
(ctx: Context)
=
let isStroustrup =
ctx.Config.ExperimentalStroustrupStyle
&& e.IsStroustrupStyleExpr
&& Seq.isEmpty (Expr.Node e).ContentBefore
isStroustrupStyleExpr ctx.Config e && canSafelyUseStroustrup (Expr.Node e)

if isStroustrup then
f e ctx
Expand Down
2 changes: 2 additions & 0 deletions src/Fantomas.Core/Context.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ val sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidthUnlessStroustrup:
val sepSpaceOrIndentAndNlnIfTypeExceedsPageWidthUnlessStroustrup:
f: (Type -> Context -> Context) -> t: Type -> (Context -> Context)

val isStroustrupStyleExpr: config: FormatConfig -> e: Expr -> bool

val autoParenthesisIfExpressionExceedsPageWidth: expr: (Context -> Context) -> ctx: Context -> Context
val futureNlnCheck: f: (Context -> Context) -> ctx: Context -> bool
/// similar to futureNlnCheck but validates whether the expression is going over the max page width
Expand Down
20 changes: 0 additions & 20 deletions src/Fantomas.Core/SyntaxOak.fs
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,6 @@ type Type =
| Or n -> n
| LongIdentApp n -> n

member e.IsStroustrupStyleType: bool =
match e with
| AnonRecord _ -> true
| _ -> false

/// A pattern composed from a left hand-side pattern, a single text token/operator and a right hand-side pattern.
type PatLeftMiddleRight(lhs: Pattern, middle: Choice<SingleTextNode, string>, rhs: Pattern, range) =
inherit NodeBase(range)
Expand Down Expand Up @@ -1728,21 +1723,6 @@ type Expr =
| Typar n -> n
| Chain n -> n

member e.IsStroustrupStyleExpr: bool =
match e with
| Expr.Record node ->
match node.Extra with
| RecordNodeExtra.Inherit _ -> false
| RecordNodeExtra.With _
| RecordNodeExtra.None -> true
| Expr.AnonRecord _ -> true
| Expr.NamedComputation node ->
match node.Name with
| Expr.Ident _ -> true
| _ -> false
| Expr.ArrayOrList _ -> true
| _ -> false

member e.HasParentheses: bool =
match e with
| Expr.Paren _ -> true
Expand Down

0 comments on commit 2b479be

Please sign in to comment.