From 2003ce2429ae00e114137359a41358c2b951f47b Mon Sep 17 00:00:00 2001 From: dawe Date: Wed, 9 Feb 2022 22:29:31 +0100 Subject: [PATCH] Fix 2043: Comment after [] is lost (#2074) * Fix 2043: - comments after let bindings with return types - comments after SynType_Array in record type * - Fix trivia placement by using @nojaf's review suggestion - Adjust test --- src/Fantomas.Tests/CommentTests.fs | 38 +++++++++++++++++++++++ src/Fantomas/CodePrinter.fs | 49 ++++++++++++++++-------------- src/Fantomas/SourceParser.fs | 2 +- 3 files changed, 66 insertions(+), 23 deletions(-) diff --git a/src/Fantomas.Tests/CommentTests.fs b/src/Fantomas.Tests/CommentTests.fs index 870539dfb9..60c3565630 100644 --- a/src/Fantomas.Tests/CommentTests.fs +++ b/src/Fantomas.Tests/CommentTests.fs @@ -86,6 +86,44 @@ let print_30_permut () = permutation """ +[] +let ``comments on let bindings with return type, 2043`` () = + formatSourceString + false + """ +let count: int [] // foo + = [2] + """ + config + |> prepend newline + |> should + equal + """ +let count: int [] // foo + = + [ 2 ] +""" + +[] +let ``comments after SynType_Array in record type, 2043`` () = + formatSourceString + false + """ +type Model = + { Flags: bool [] // foo + Name: string // bar + Street: string } + """ + config + |> prepend newline + |> should + equal + """ +type Model = + { Flags: bool [] // foo + Name: string // bar + Street: string } +""" [] let ``xml documentation`` () = diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index 588861f155..cfb4b71833 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -4286,7 +4286,9 @@ and genType astContext outerBracket t = loop t1 -- "=" +> addSpaceIfSynTypeStaticConstantHasAtSignBeforeString t2 +> loop t2 - | TArray (t, n) -> loop t -- " [" +> rep (n - 1) (!- ",") -- "]" + | TArray (t, n, r) -> + loop t -- " [" +> rep (n - 1) (!- ",") -- "]" + |> genTriviaFor SynType_Array r | TAnon -> sepWild | TVar (tp, r) -> genTypar astContext tp @@ -5375,27 +5377,15 @@ and genSynBindingValue let genValueName = genPat astContext valueName - let genReturnType = - match returnType with - | Some rt -> - let hasGenerics = - match valueName with - | SynPat.LongIdent (_, _, Some _, _, _, _) -> true - | _ -> false - - ifElse hasGenerics sepColonWithSpacesFixed sepColon - +> genType astContext false rt - | None -> sepNone - - let equalsRange (ctx: Context) = - let endPos = - match returnType with - | Some rt -> rt.Range.End - | None -> valueName.Range.End + let genEqualsInBinding (ctx: Context) = + let equalsRange = + let endPos = + match returnType with + | Some rt -> rt.Range.End + | None -> valueName.Range.End - ctx.MkRange endPos e.Range.Start + ctx.MkRange endPos e.Range.Start - let genEqualsInBinding (equalsRange: Range) (ctx: Context) = let space = ctx.TriviaTokenNodes |> Map.tryFindOrEmptyList EQUALS @@ -5404,7 +5394,23 @@ and genSynBindingValue | Some tn when (List.isNotEmpty tn.ContentAfter) -> sepNone | _ -> sepSpace - (tokN equalsRange EQUALS sepEq +> space) ctx + (tokN equalsRange EQUALS (sepSpace +> sepEqFixed) + +> space) + ctx + + let genReturnType = + match returnType with + | Some rt -> + let hasGenerics = + match valueName with + | SynPat.LongIdent (_, _, Some _, _, _, _) -> true + | _ -> false + + ifElse hasGenerics sepColonWithSpacesFixed sepColon + +> (genType astContext false rt + |> genTriviaFor SynBindingReturnInfo_ rt.Range) + +> autoIndentAndNlnWhenWriteBeforeNewlineNotEmpty genEqualsInBinding + | None -> genEqualsInBinding genPreXmlDoc px +> genAttrIsFirstChild @@ -5416,7 +5422,6 @@ and genSynBindingValue +> sepSpace +> genValueName +> genReturnType - +> (fun ctx -> genEqualsInBinding (equalsRange ctx) ctx) let short = prefix +> genExprKeepIndentInBranch astContext e diff --git a/src/Fantomas/SourceParser.fs b/src/Fantomas/SourceParser.fs index 80820130bd..daedbb2fb0 100644 --- a/src/Fantomas/SourceParser.fs +++ b/src/Fantomas/SourceParser.fs @@ -1450,7 +1450,7 @@ let (|TStaticConstantNamed|_|) = let (|TArray|_|) = function - | SynType.Array (n, t, _) -> Some(t, n) + | SynType.Array (n, t, r) -> Some(t, n, r) | _ -> None let (|TAnon|_|) =