Skip to content

Commit

Permalink
Visit SynTypar indent in AstTransformer.fs. Fixes #2052. (#2053)
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf authored Feb 1, 2022
1 parent 4a65cb4 commit 503a471
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
15 changes: 15 additions & 0 deletions src/Fantomas.Tests/CommentTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1744,3 +1744,18 @@ let a = 9
// bar
let a = 9
"""

[<Test>]
let ``comment after SynTypar, 2052`` () =
formatSourceString
false
"""
let Foo<'T (* TODO *)> () = ()
"""
config
|> prepend newline
|> should
equal
"""
let Foo<'T (* TODO *) > () = ()
"""
6 changes: 3 additions & 3 deletions src/Fantomas.Tests/InterfaceTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,13 @@ type IArgParserTemplate =

[<Test>]
let ``generic interface member should have space after name`` () =
let source =
formatSourceString
false
"""
type IFunc<'R> =
abstract Invoke<'T> : unit -> 'R // without this space the code is invalid
"""

formatSourceString false source config
config
|> fun formatted -> formatSourceString false formatted config
|> should
equal
Expand Down
6 changes: 5 additions & 1 deletion src/Fantomas/AstTransformer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,11 @@ module private Ast =

and visitSynTyparDecl (std: SynTyparDecl) : TriviaNodeAssigner list =
match std with
| SynTyparDecl (attrs, _) -> [ yield! (visitSynAttributeLists attrs) ]
| SynTyparDecl (attrs, synTypar) ->
[ yield! (visitSynAttributeLists attrs)
yield visitSynTypar synTypar ]

and visitSynTypar (SynTypar (ident, _typarStaticReq, _isCompGen)) = mkNode Ident_ ident.idRange

and visitSynBindingReturnInfo (returnInfo: SynBindingReturnInfo) : TriviaNodeAssigner list =
match returnInfo with
Expand Down
11 changes: 6 additions & 5 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4260,7 +4260,9 @@ and genType astContext outerBracket t =
+> loop t2
| TArray (t, n) -> loop t -- " [" +> rep (n - 1) (!- ",") -- "]"
| TAnon -> sepWild
| TVar tp -> genTypar astContext tp
| TVar (tp, r) ->
genTypar astContext tp
|> genTriviaFor SynType_Var r
// Drop bracket around tuples before an arrow
| TFun (TTuple ts, t) -> loopTTupleList ts +> sepArrow +> loop t
// Do similar for tuples after an arrow
Expand Down Expand Up @@ -4399,7 +4401,7 @@ and genPrefixTypes astContext node (range: Range) ctx =
match node with
| [] -> ctx
// Where < and ^ meet, we need an extra space. For example: seq< ^a >
| TVar (Typar (_, true)) as t :: ts ->
| TVar (Typar (_, _, true), _r) as t :: ts ->
(!- "< "
+> col sepComma (t :: ts) (genType astContext false)
-- " >")
Expand Down Expand Up @@ -4491,10 +4493,9 @@ and genTypeList astContext node =

expressionFitsOnRestOfLine shortExpr longExpr

and genTypar astContext (Typar (s, isHead) as node) =
and genTypar astContext (Typar (s, idRange, isHead)) =
ifElse isHead (ifElse astContext.IsFirstTypeParam (!- " ^") (!- "^")) (!- "'")
-- s
|> genTriviaFor SynType_Var node.Range
+> genTriviaFor Ident_ idRange !-s

and genTypeConstraint astContext node =
match node with
Expand Down
12 changes: 6 additions & 6 deletions src/Fantomas/SourceParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ let (|OpNameFull|) (x: Identifier) =

// Type params

let inline (|Typar|) (SynTypar.SynTypar (Ident s, req, _)) =
let inline (|Typar|) (SynTypar.SynTypar (Ident s as ident, req, _)) =
match req with
| TyparStaticReq.None -> (s, false)
| TyparStaticReq.HeadType -> (s, true)
| TyparStaticReq.None -> (s, ident.idRange, false)
| TyparStaticReq.HeadType -> (s, ident.idRange, true)

let inline (|ValTyparDecls|) (SynValTyparDecls (tds, b)) = (tds, b)

Expand All @@ -199,7 +199,7 @@ let rec (|RationalConst|) =
let (|Measure|) x =
let rec loop =
function
| SynMeasure.Var (Typar (s, _), _) -> s
| SynMeasure.Var (Typar (s, _, _), _) -> s
| SynMeasure.Anon _ -> "_"
| SynMeasure.One -> "1"
| SynMeasure.Product (m1, m2, _) ->
Expand Down Expand Up @@ -1446,7 +1446,7 @@ let (|TAnon|_|) =

let (|TVar|_|) =
function
| SynType.Var (tp, _) -> Some tp
| SynType.Var (tp, r) -> Some(tp, r)
| _ -> None

let (|TFun|_|) =
Expand Down Expand Up @@ -1648,7 +1648,7 @@ let rec (|UppercaseSynType|LowercaseSynType|) (synType: SynType) =

match synType with
| SynType.LongIdent (LongIdentWithDots lid) -> lid.Split('.') |> Seq.last |> upperOrLower
| SynType.Var (Typar (s, _), _) -> upperOrLower s
| SynType.Var (Typar (s, _, _), _) -> upperOrLower s
| SynType.App (st, _, _, _, _, _, _) -> (|UppercaseSynType|LowercaseSynType|) st
| _ -> failwithf "cannot determine if synType %A is uppercase or lowercase" synType

Expand Down

0 comments on commit 503a471

Please sign in to comment.