Skip to content

Commit

Permalink
Fix 2043: Comment after [] is lost (#2074)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dawedawe authored Feb 9, 2022
1 parent 298b74f commit 6e88224
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 23 deletions.
38 changes: 38 additions & 0 deletions src/Fantomas.Tests/CommentTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,44 @@ let print_30_permut () =
permutation
"""

[<Test>]
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 ]
"""

[<Test>]
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 }
"""

[<Test>]
let ``xml documentation`` () =
Expand Down
49 changes: 27 additions & 22 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -5416,7 +5422,6 @@ and genSynBindingValue
+> sepSpace
+> genValueName
+> genReturnType
+> (fun ctx -> genEqualsInBinding (equalsRange ctx) ctx)

let short = prefix +> genExprKeepIndentInBranch astContext e

Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas/SourceParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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|_|) =
Expand Down

0 comments on commit 6e88224

Please sign in to comment.