Skip to content

Commit

Permalink
fix by using Oak knowledge and altering the config for the dotlambda …
Browse files Browse the repository at this point in the history
…construction
  • Loading branch information
dawedawe committed May 25, 2024
1 parent 489cfbf commit 9dda7f0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
23 changes: 18 additions & 5 deletions src/Fantomas.Core/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1553,9 +1553,24 @@ let genExpr (e: Expr) =
| Expr.IndexFromEnd node -> !- "^" +> genExpr node.Expr |> genNode node
| Expr.Typar node -> genSingleTextNode node
| Expr.DotLambda node ->
let genDotLambdaExpr expr =
fun ctx ->
let currentConfig = ctx.Config

let dotLambdaConfig =
{ currentConfig with
// override the SpaceBefore[Upper|Lower]caseInvocation to avoid generating breaking code.
// see unit tests for issue 3050``
SpaceBeforeUppercaseInvocation = false
SpaceBeforeLowercaseInvocation = false }

genExpr expr { ctx with Config = dotLambdaConfig }
// Restore the existing configuration after printing the DotLambda expression
|> fun ctx -> { ctx with Config = currentConfig }

genSingleTextNode node.Underscore
+> genSingleTextNode node.Dot
+> genExpr node.Expr
+> genDotLambdaExpr node.Expr
|> genNode node
| Expr.BeginEnd node ->
let short =
Expand Down Expand Up @@ -2523,10 +2538,8 @@ let sepSpaceBeforeParenInFuncInvocation (functionExpr: Expr) (argExpr: Expr) ctx
| Expr.DotLambda _, _ -> ctx
| Expr.Constant _, _ -> sepSpace ctx
| ParenExpr _, _ -> sepSpace ctx
| UppercaseExpr, ParenExpr _ ->
onlyIf (ctx.Config.SpaceBeforeUppercaseInvocation && not (lastWriteEventIsDotLambda ctx)) sepSpace ctx
| LowercaseExpr, ParenExpr _ ->
onlyIf (ctx.Config.SpaceBeforeLowercaseInvocation && not (lastWriteEventIsDotLambda ctx)) sepSpace ctx
| UppercaseExpr, ParenExpr _ -> onlyIf ctx.Config.SpaceBeforeUppercaseInvocation sepSpace ctx
| LowercaseExpr, ParenExpr _ -> onlyIf ctx.Config.SpaceBeforeLowercaseInvocation sepSpace ctx
| Expr.Ident _, Expr.Ident _ -> sepSpace ctx
| _ -> sepSpace ctx

Expand Down
12 changes: 0 additions & 12 deletions src/Fantomas.Core/Context.fs
Original file line number Diff line number Diff line change
Expand Up @@ -338,18 +338,6 @@ let lastWriteEventIsNewline ctx =
| _ -> false)
|> Option.defaultValue false

let lastWriteEventIsDotLambda ctx =
ctx.WriterEvents
|> Queue.rev
|> List.ofSeq
|> function
| WriterEvent.Write f :: Write "." :: Write "_" :: _ ->
String.isNotNullOrWhitespace f
&& (Char.IsLetter(f[0])
|| (f.StartsWith("``", StringComparison.Ordinal)
&& f.EndsWith("``", StringComparison.Ordinal)))
| _ -> false

let (|EmptyHashDefineBlock|_|) (events: WriterEvent array) =
match Array.tryHead events, Array.tryLast events with
| Some(CommentOrDefineEvent _), Some(CommentOrDefineEvent _) ->
Expand Down
1 change: 0 additions & 1 deletion src/Fantomas.Core/Context.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ val hasWriteBeforeNewlineContent: ctx: Context -> bool
val dump: isSelection: bool -> ctx: Context -> FormatResult
val dumpAndContinue: ctx: Context -> Context
val lastWriteEventIsNewline: ctx: Context -> bool
val lastWriteEventIsDotLambda: ctx: Context -> bool

/// Indent one more level based on configuration
val indent: ctx: Context -> Context
Expand Down

0 comments on commit 9dda7f0

Please sign in to comment.