Skip to content

Commit

Permalink
Further indent constraint in multi line member definition. Fixes #1394.…
Browse files Browse the repository at this point in the history
… (#1411)
  • Loading branch information
nojaf committed Jan 29, 2021
1 parent 7eede44 commit 8c5011b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 10 deletions.
21 changes: 19 additions & 2 deletions src/Fantomas.Tests/SignatureTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -614,14 +614,31 @@ let ``operator with constraint`` () =
"""namespace Bar
val inline (.+.) : x : ^a Foo -> y : ^b Foo -> ^c Foo when (^a or ^b) : (static member (+) : ^a * ^b -> ^c)
"""
config
{ config with SpaceBeforeColon = true }
|> prepend newline
|> should
equal
"""
namespace Bar
val inline (.+.) : x : ^a Foo -> y : ^b Foo -> ^c Foo when (^a or ^b) : (static member (+) : ^a * ^b -> ^c)
"""

[<Test>]
let ``operator with named constraint`` () =
formatSourceString
true
"""namespace Bar
val inline (.+.) : x : ^a Foo -> y : ^b Foo -> z: ^c Foo when (^a or ^b) : (static member (+) : ^a * ^b -> ^c)
"""
{ config with SpaceBeforeColon = true }
|> prepend newline
|> should
equal
"""
namespace Bar
val inline (.+.): x : ^a Foo -> y : ^b Foo -> ^c Foo when (^a or ^b): (static member (+): ^a * ^b -> ^c)
val inline (.+.) : x : ^a Foo -> y : ^b Foo -> z : ^c Foo when (^a or ^b) : (static member (+) : ^a * ^b -> ^c)
"""

[<Test>]
Expand Down
42 changes: 40 additions & 2 deletions src/Fantomas.Tests/TypeDeclarationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1836,8 +1836,7 @@ type OuterType =
equal
"""
type OuterType =
abstract Apply<'r> : InnerType<'r>
-> 'r when 'r : comparison
abstract Apply<'r> : InnerType<'r> -> 'r when 'r : comparison
"""

[<Test>]
Expand Down Expand Up @@ -2374,3 +2373,42 @@ let deserialize (e: RecordedEvent): MyEvent =
| nameof BData -> BData(JsonSerializer.Deserialize<string> e.Data)
| t -> failwithf "Invalid EventType: %s" t
"""

[<Test>]
let ``member constraint on next line should have extra indent, 1394`` () =
formatSourceString
false
"""
type Bar = | Bar of int
and Foo<'ret> = abstract Barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr<'a> : 'a -> 'ret when 'a : comparison
"""
{ config with MaxLineLength = 80 }
|> prepend newline
|> should
equal
"""
type Bar = Bar of int
and Foo<'ret> =
abstract Barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr<'a> :
'a -> 'ret when 'a: comparison
"""

[<Test>]
let ``member constraint on next line with long return type`` () =
formatSourceString
false
"""
type Foo =
abstract Baaaaaaaaaaaaaarrrrrrr<'a> : 'a -> int -> string -> string -> bool when 'a : comparison
"""
{ config with MaxLineLength = 60 }
|> prepend newline
|> should
equal
"""
type Foo =
abstract Baaaaaaaaaaaaaarrrrrrr<'a> :
'a -> int -> string -> string -> bool
when 'a: comparison
"""
32 changes: 26 additions & 6 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@ and genVal astContext (Val (ats, px, ao, s, t, vi, isInline, _) as node) =
+> ifElse
(List.isNotEmpty namedArgs)
(autoNlnIfExpressionExceedsPageWidth (genTypeList astContext namedArgs))
(genConstraints astContext t)
(genConstraints astContext t vi)
+> unindent
)
|> genTriviaFor ValSpfn_ range
Expand Down Expand Up @@ -3741,7 +3741,7 @@ and genMemberSig astContext node =
+> genTypeParamPostfix astContext tds tcs
+> sepColonX
+> genTypeList astContext namedArgs
+> genConstraints astContext t
+> genConstraints astContext t vi
-- (genPropertyKind (not isFunctionProperty) mf.MemberKind)
)

Expand All @@ -3751,10 +3751,30 @@ and genMemberSig astContext node =
| MSNestedType _ -> invalidArg "md" "This is not implemented in F# compiler"
|> genTriviaFor mainNodeName range

and genConstraints astContext (t: SynType) =
and genConstraints astContext (t: SynType) (vi: SynValInfo) =
match t with
| TWithGlobalConstraints (t, tcs) ->
genTypeByLookup astContext t
| TWithGlobalConstraints (ti, tcs) ->
let genType =
match ti, vi with
| TFuns ts, SynValInfo (curriedArgInfos, returnType) ->
let namedArgInfos =
(List.map List.head curriedArgInfos)
@ [ returnType ]
|> List.map (fun (SynArgInfo (_, _, i)) -> i)

coli
sepArrow
ts
(fun i t ->
let genNamedArg =
List.tryItem i namedArgInfos
|> Option.bind id
|> optSingle (fun (Ident (s)) -> !-s +> sepColon)

genNamedArg +> genType astContext false t)
| _ -> genType astContext false ti

genType
+> sepSpaceOrNlnIfExpressionExceedsPageWidth (
ifElse (List.isNotEmpty tcs) (!- "when ") sepSpace
+> col wordAnd tcs (genTypeConstraint astContext)
Expand Down Expand Up @@ -4313,7 +4333,7 @@ and genMemberDefn astContext node =
+> sepColonX
+> genTypeList astContext namedArgs
-- genPropertyKind (not isFunctionProperty) mk
+> genConstraints astContext t
+> autoIndentAndNlnIfExpressionExceedsPageWidth (genConstraints astContext t vi)

| md -> failwithf "Unexpected member definition: %O" md
|> genTriviaFor (synMemberDefnToFsAstType node) node.Range
Expand Down

0 comments on commit 8c5011b

Please sign in to comment.