From 551273d8408ada10aee22eed2f85cd19dcd281e3 Mon Sep 17 00:00:00 2001 From: nojaf Date: Fri, 3 Jan 2020 10:05:26 +0100 Subject: [PATCH] Add indent when line comment precedes type info, #565 --- src/Fantomas.Tests/LetBindingTests.fs | 22 ++++++++++++++++++++++ src/Fantomas/CodePrinter.fs | 8 +++++++- src/Fantomas/TriviaHelpers.fs | 8 +++++++- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/Fantomas.Tests/LetBindingTests.fs b/src/Fantomas.Tests/LetBindingTests.fs index 91bf40758a..85ea864ef0 100644 --- a/src/Fantomas.Tests/LetBindingTests.fs +++ b/src/Fantomas.Tests/LetBindingTests.fs @@ -317,4 +317,26 @@ let x = elif true then printfn "b" if true then 1 else 0 +""" + +[] +let ``line comment before return type info should indent before colon, 565`` () = + formatSourceString false """module Bar = + let f a + // foo + : int + = + 0 +""" ({ config with + SpaceAfterComma = false + SpaceAfterSemicolon = false + SpaceAroundDelimiter = false + SpaceBeforeArgument = false }) + |> prepend newline + |> should equal """ +module Bar = + let f a + // foo + : int = + 0 """ \ No newline at end of file diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index 2d377692a5..24effbed0d 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -489,7 +489,13 @@ and genExprSepEqPrependType astContext prefix (pat:SynPat) e ctx = addSpaceAfterGenericConstructBeforeColon | _ -> sepNone - (prefix +> addExtraSpaceBeforeGenericType +> sepColon +> genType astContext false t +> sepEq + let genCommentBeforeColon ctx = + let hasLineComment = TriviaHelpers.``has line comment before`` t.Range ctx.Trivia + (ifElse hasLineComment indent sepNone +> enterNode t.Range) ctx + + (prefix +> addExtraSpaceBeforeGenericType + +> genCommentBeforeColon + +> sepColon +> genType astContext false t +> sepEq +> breakNlnOrAddSpace astContext (hasTriviaContentAfterEqual || multilineCheck || checkPreserveBreakForExpr e ctx) e) ctx | e -> diff --git a/src/Fantomas/TriviaHelpers.fs b/src/Fantomas/TriviaHelpers.fs index 9089e2eaaa..4edfe1af4d 100644 --- a/src/Fantomas/TriviaHelpers.fs +++ b/src/Fantomas/TriviaHelpers.fs @@ -45,4 +45,10 @@ module TriviaHelpers = | Comment(LineCommentAfterSourceCode(_)) -> true | _ -> false ) - |> (List.isEmpty >> not) \ No newline at end of file + |> (List.isEmpty >> not) + + let internal ``has line comment before`` range triviaNodes = + triviaNodes + |> List.tryFind (fun tv -> tv.Range = range) + |> Option.map (fun tv -> tv.ContentBefore |> List.exists (function | Comment(LineCommentOnSingleLine(_)) -> true | _ -> false)) + |> Option.defaultValue false \ No newline at end of file