From 466c6f5da51a1be9c10fa853f42b4770975f4af2 Mon Sep 17 00:00:00 2001 From: nojaf Date: Sat, 21 Nov 2020 19:56:31 +0100 Subject: [PATCH] Indent and add newline for multiline do(bang) expressions. Fixes #1265. --- src/Fantomas.Tests/CompilerDirectivesTests.fs | 16 ++--- .../ComputationExpressionTests.fs | 72 ++++++++++++++++++- src/Fantomas.Tests/LetBindingTests.fs | 7 +- src/Fantomas/CodePrinter.fs | 4 +- 4 files changed, 86 insertions(+), 13 deletions(-) diff --git a/src/Fantomas.Tests/CompilerDirectivesTests.fs b/src/Fantomas.Tests/CompilerDirectivesTests.fs index 8b64045311..5eb3f1862a 100644 --- a/src/Fantomas.Tests/CompilerDirectivesTests.fs +++ b/src/Fantomas.Tests/CompilerDirectivesTests.fs @@ -389,10 +389,10 @@ let internal UpdateStrongNaming (assembly: AssemblyDefinition) (key: StrongNameK match key with | None -> #endif - assembly.MainModule.Attributes <- assembly.MainModule.Attributes &&& (~~~ModuleAttributes.StrongNameSigned) - assemblyName.HasPublicKey <- false - assemblyName.PublicKey <- null - assemblyName.PublicKeyToken <- null + assembly.MainModule.Attributes <- assembly.MainModule.Attributes &&& (~~~ModuleAttributes.StrongNameSigned) + assemblyName.HasPublicKey <- false + assemblyName.PublicKey <- null + assemblyName.PublicKeyToken <- null #if NETCOREAPP2_0 #else | Some key' -> @@ -479,10 +479,10 @@ let internal UpdateStrongNaming (assembly: AssemblyDefinition) (key: StrongNameK #endif - assembly.MainModule.Attributes <- assembly.MainModule.Attributes &&& (~~~ModuleAttributes.StrongNameSigned) - assemblyName.HasPublicKey <- false - assemblyName.PublicKey <- null - assemblyName.PublicKeyToken <- null + assembly.MainModule.Attributes <- assembly.MainModule.Attributes &&& (~~~ModuleAttributes.StrongNameSigned) + assemblyName.HasPublicKey <- false + assemblyName.PublicKey <- null + assemblyName.PublicKeyToken <- null #if NETCOREAPP2_0 #else diff --git a/src/Fantomas.Tests/ComputationExpressionTests.fs b/src/Fantomas.Tests/ComputationExpressionTests.fs index af75af204c..6c38666597 100644 --- a/src/Fantomas.Tests/ComputationExpressionTests.fs +++ b/src/Fantomas.Tests/ComputationExpressionTests.fs @@ -1514,7 +1514,8 @@ let sendPushNotifications = with :? WebPushException as wpex -> log.LogError(sprintf "Couldn't send notification to %s, %A" user.UserId wpex) - do! filterSubscriptionsAndPersist + do! + filterSubscriptionsAndPersist managementToken user.UserId subscriptions @@ -1872,3 +1873,72 @@ let password = with_validators (String.exists Char.IsUpper) (String.exists Char.IsDigit) (fun s -> s.Length >= 6) } """ + +[] +let ``multiline do bang`` () = + formatSourceString false """ +type ProjectController(checker: FSharpChecker) = + member x.LoadWorkspace (files: string list) (tfmForScripts: FSIRefs.TFM) onProjectLoaded (generateBinlog: bool) = + async { + match Environment.workspaceLoadDelay () with + | delay when delay > TimeSpan.Zero -> + do! Async.Sleep( + Environment.workspaceLoadDelay().TotalMilliseconds + |> int + ) + | _ -> () + + return true + } + +""" { config with IndentSize = 2 } + |> prepend newline + |> should equal """ +type ProjectController(checker: FSharpChecker) = + member x.LoadWorkspace (files: string list) (tfmForScripts: FSIRefs.TFM) onProjectLoaded (generateBinlog: bool) = + async { + match Environment.workspaceLoadDelay () with + | delay when delay > TimeSpan.Zero -> + do! + Async.Sleep( + Environment.workspaceLoadDelay().TotalMilliseconds + |> int + ) + | _ -> () + + return true + } +""" + +[] +let ``multiline do`` () = + formatSourceString false """ +type ProjectController(checker: FSharpChecker) = + member x.LoadWorkspace (files: string list) (tfmForScripts: FSIRefs.TFM) onProjectLoaded (generateBinlog: bool) = + async { + match Environment.workspaceLoadDelay () with + | delay when delay > TimeSpan.Zero -> + do NonAsync.Sleep( Environment.workspaceLoadDelay().TotalMilliseconds |> int ) + | _ -> () + + return true + } + +""" { config with IndentSize = 2 } + |> prepend newline + |> should equal """ +type ProjectController(checker: FSharpChecker) = + member x.LoadWorkspace (files: string list) (tfmForScripts: FSIRefs.TFM) onProjectLoaded (generateBinlog: bool) = + async { + match Environment.workspaceLoadDelay () with + | delay when delay > TimeSpan.Zero -> + do + NonAsync.Sleep( + Environment.workspaceLoadDelay().TotalMilliseconds + |> int + ) + | _ -> () + + return true + } +""" diff --git a/src/Fantomas.Tests/LetBindingTests.fs b/src/Fantomas.Tests/LetBindingTests.fs index 9830ab8117..661a048fb5 100644 --- a/src/Fantomas.Tests/LetBindingTests.fs +++ b/src/Fantomas.Tests/LetBindingTests.fs @@ -1137,9 +1137,10 @@ do """ config |> prepend newline |> should equal """ -do let rec f = () - and g = () in - () +do + let rec f = () + and g = () in + () """ [] diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index 8f52cb2555..6688997252 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -1275,7 +1275,9 @@ and genExpr astContext synExpr ctx = | YieldFrom | Yield | Return - | ReturnFrom -> autoIndentAndNlnIfExpressionExceedsPageWidth (genExpr astContext e) + | ReturnFrom + | Do + | DoBang -> autoIndentAndNlnIfExpressionExceedsPageWidth (genExpr astContext e) | _ -> genExpr astContext e) | ConstExpr (c, r) -> genConst c r