From 410e2baccc9943140881ffcedfe4aaa6239de6bf Mon Sep 17 00:00:00 2001 From: nojaf Date: Sat, 28 Mar 2020 11:20:21 +0100 Subject: [PATCH] Add extra space between prefix operator and string. Fixes #736 --- src/Fantomas.Tests/OperatorTests.fs | 9 +++++++++ src/Fantomas/CodePrinter.fs | 8 +++++++- src/Fantomas/SourceParser.fs | 7 ++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Fantomas.Tests/OperatorTests.fs b/src/Fantomas.Tests/OperatorTests.fs index 5a33ec6a3d..cd549a01d7 100644 --- a/src/Fantomas.Tests/OperatorTests.fs +++ b/src/Fantomas.Tests/OperatorTests.fs @@ -341,4 +341,13 @@ let ``equal sign operator should not move to next line`` () = |> should equal """ let result = (typ.GetInterface(typeof.FullName) = null) +""" + +[] +let ``operator before verbatim string add extra space, 736`` () = + formatSourceString false """Target M.Tools (fun _ -> !! @"Tools\Tools.sln" |> rebuild) +""" config + |> prepend newline + |> should equal """ +Target M.Tools (fun _ -> !! @"Tools\Tools.sln" |> rebuild) """ \ No newline at end of file diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index 0d46fe85b2..bdb31a987f 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -988,7 +988,13 @@ and genExpr astContext synExpr = ifElse astContext.IsNakedRange expr (sepOpenS +> expr +> sepCloseS) // Separate two prefix ops by spaces | PrefixApp(s1, PrefixApp(s2, e)) -> !- (sprintf "%s %s" s1 s2) +> genExpr astContext e - | PrefixApp(s, e) -> !- s +> genExpr astContext e + | PrefixApp(s, e) -> + let extraSpaceBeforeString = + match e with + | String(_) -> sepSpace + | _ -> sepNone + + !- s +> extraSpaceBeforeString +> genExpr astContext e // Handle spaces of infix application based on which category it belongs to | InfixApps(e, es) -> let sepAfterExpr f = diff --git a/src/Fantomas/SourceParser.fs b/src/Fantomas/SourceParser.fs index da7b80a529..751fe01d1d 100644 --- a/src/Fantomas/SourceParser.fs +++ b/src/Fantomas/SourceParser.fs @@ -184,9 +184,14 @@ let rec (|Const|) c = // Auto print may cut off the array | SynConst.UInt16s us -> sprintf "%A" us +let (|String|_|) e = + match e with + | SynExpr.Const(SynConst.String(s,_),_) -> Some s + | _ -> None + let (|MultilineString|_|) e = match e with - | SynExpr.Const(SynConst.String(s,_),_) when (String.isMultiline s) -> Some e + | String(s) when (String.isMultiline s) -> Some e | _ -> None let (|Unresolved|) (Const s as c, r) = (c, r, s)