From c2338f30a3d65871b5bd3266da52886a1e3cba8d Mon Sep 17 00:00:00 2001 From: Florian Verdonck Date: Sat, 19 Oct 2019 12:21:52 +0200 Subject: [PATCH] Fix: print trivia for infix app. (#522) * Fix: print trivia for infix app. * Added test for both code branches --- src/Fantomas.Tests/CompilerDirectivesTests.fs | 40 +++++++++++++++++++ src/Fantomas/CodePrinter.fs | 2 +- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/Fantomas.Tests/CompilerDirectivesTests.fs b/src/Fantomas.Tests/CompilerDirectivesTests.fs index 3f2cf1a331..57cd7f7a3d 100644 --- a/src/Fantomas.Tests/CompilerDirectivesTests.fs +++ b/src/Fantomas.Tests/CompilerDirectivesTests.fs @@ -966,4 +966,44 @@ try () #endif with _ -> () +""" + +[] +let ``preserve compile directive between piped functions (DEBUG), 512`` () = + formatSourceStringWithDefines ["DEBUG"] """let foo = [ 1 ] + |> List.sort +#if DEBUG + |> List.rev +#endif + |> List.sort +""" config + |> prepend newline + |> should equal """ +let foo = + [ 1 ] + |> List.sort +#if DEBUG + |> List.rev +#endif + |> List.sort +""" + +[] +let ``preserve compile directive between piped functions, 512`` () = + formatSourceString false """let foo = [ 1 ] + |> List.sort +#if DEBUG + |> List.rev +#endif + |> List.sort +""" config + |> prepend newline + |> should equal """ +let foo = + [ 1 ] + |> List.sort +#if DEBUG + |> List.rev +#endif + |> List.sort """ \ No newline at end of file diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index 673b1b7719..c0fd6be691 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -1229,7 +1229,7 @@ and genInfixApps astContext hasNewLine synExprs = genExpr ctx)) +> genInfixApps astContext (hasNewLine || checkNewLine e es) es | (s, opE, e)::es when(hasNewLine) -> - (sepNln +> tok opE.Range s +> sepSpace +> genExpr astContext e) + (sepNln +> (tok opE.Range s |> genTrivia opE.Range) +> sepSpace +> genExpr astContext e) +> genInfixApps astContext (hasNewLine || checkNewLine e es) es | (s, opE, e)::es when(NoSpaceInfixOps.Contains s) -> let wrapExpr f =