Skip to content

Commit

Permalink
Long generic constraints (#2273)
Browse files Browse the repository at this point in the history
* Put long type constraints on the next line.

* Deal with long constraints in function, similar to in type definitions.

* Add changelog entries.
  • Loading branch information
nojaf committed May 24, 2022
1 parent 5fdc87d commit 434df47
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
### Changed
* Formatting of multi-constrained SRTP functions needs improvement. [#2230](https://github.com/fsprojects/fantomas/issues/2230)
* Try/With with a single clause. [#1881](https://github.com/fsprojects/fantomas/issues/1881)
* Long type argument with constraints is not respecting max_line_length. [#2266](https://github.com/fsprojects/fantomas/issues/2266)
* Long function argument with constraints is not respecting max_line_length. [#2267](https://github.com/fsprojects/fantomas/issues/2267)

### Removed

Expand Down
4 changes: 2 additions & 2 deletions src/Fantomas.Core.Tests/ClassTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -821,8 +821,8 @@ type ISingleExpressionValue<'p, 'o, 'v when 'p :> IProperty and 'o :> IOperator
|> should
equal
"""
type ISingleExpressionValue<'p, 'o, 'v when 'p :> IProperty and 'o :> IOperator and 'p: equality and 'o: equality and 'v: equality>
() =
type ISingleExpressionValue<'p, 'o, 'v
when 'p :> IProperty and 'o :> IOperator and 'p: equality and 'o: equality and 'v: equality>() =
abstract Property: 'p
abstract Operator: 'o
abstract Value: 'v
Expand Down
39 changes: 39 additions & 0 deletions src/Fantomas.Core.Tests/LetBindingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1994,3 +1994,42 @@ let escapeEarth myVelocity mySpeed =
else
"Come back"
"""

[<Test>]
let ``long value with constraints, 2267`` () =
formatSourceString
false
"""
let inline NonStructural<'TInput when 'TInput: (static member (<): 'TInput * 'TInput -> bool) and 'TInput: (static member (>): 'TInput * 'TInput -> bool)> : IComparer<'TInput> = ()
"""
config
|> prepend newline
|> should
equal
"""
let inline NonStructural<'TInput
when 'TInput: (static member (<): 'TInput * 'TInput -> bool)
and 'TInput: (static member (>): 'TInput * 'TInput -> bool)> : IComparer<'TInput> =
()
"""

[<Test>]
let ``long function with constraints, 2267`` () =
formatSourceString
false
"""
let inline NonStructural<'TInput when 'TInput: (static member (<): 'TInput * 'TInput -> bool) and 'TInput: (static member (>): 'TInput * 'TInput -> bool)> (a:'TInput) (b:'TInput) : IComparer<'TInput> = ()
"""
config
|> prepend newline
|> should
equal
"""
let inline NonStructural<'TInput
when 'TInput: (static member (<): 'TInput * 'TInput -> bool)
and 'TInput: (static member (>): 'TInput * 'TInput -> bool)>
(a: 'TInput)
(b: 'TInput)
: IComparer<'TInput> =
()
"""
43 changes: 42 additions & 1 deletion src/Fantomas.Core.Tests/TypeDeclarationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1659,7 +1659,8 @@ type SomeType =
let ``access modifier before long constructor`` () =
formatSourceString
false
"""type INotifications<'a,'b,'c,'d,'e> =
"""
type INotifications<'a,'b,'c,'d,'e> =
class
end
type DeviceNotificationHandler<'Notification, 'CallbackId, 'RegisterInputData, 'RegisterOutputData, 'UnregisterOutputData> private (client: INotifications<'Notification, 'CallbackId, 'RegisterInputData, 'RegisterOutputData, 'UnregisterOutputData>, callbackId: 'CallbackId, validateUnregisterOutputData: 'UnregisterOutputData -> unit) =
Expand Down Expand Up @@ -3082,3 +3083,43 @@ type MethInfo =
member ProvidedStaticParameterInfo: (Tainted<ProvidedMethodBase> * Tainted<ProvidedParameterInfo>[]) option
#endif
"""

[<Test>]
let ``long type argument with constraints, 2266`` () =
formatSourceString
false
"""
type Event<'Delegate, 'Args when 'Delegate: delegate<'Args, unit> and 'Delegate :> System.Delegate and 'Delegate: not struct> () =
class end
"""
config
|> prepend newline
|> should
equal
"""
type Event<'Delegate, 'Args
when 'Delegate: delegate<'Args, unit> and 'Delegate :> System.Delegate and 'Delegate: not struct>() =
class
end
"""

[<Test>]
let ``long type argument with constraints, short max_line_length`` () =
formatSourceString
false
"""
type Event<'Delegate, 'Args when 'Delegate: delegate<'Args, unit> and 'Delegate :> System.Delegate and 'Delegate: not struct> () =
class end
"""
{ config with MaxLineLength = 80 }
|> prepend newline
|> should
equal
"""
type Event<'Delegate, 'Args
when 'Delegate: delegate<'Args, unit>
and 'Delegate :> System.Delegate
and 'Delegate: not struct>() =
class
end
"""
16 changes: 14 additions & 2 deletions src/Fantomas.Core/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ and genTypeAndParam astContext (typeName: Context -> Context) (tds: SynTyparDecl
let types openSep tds tcs closeSep =
(!-openSep
+> coli sepComma tds (fun i -> genTyparDecl { astContext with IsFirstTypeParam = i = 0 })
+> colPre (!- " when ") wordAnd tcs (genTypeConstraint astContext)
+> genSynTypeConstraintList astContext tcs
-- closeSep)

match tds with
Expand All @@ -633,10 +633,22 @@ and genTypeParamPostfix astContext tds =
| Some (SynTyparDecls.PostfixList (tds, tcs, _range)) ->
(!- "<"
+> coli sepComma tds (fun i -> genTyparDecl { astContext with IsFirstTypeParam = i = 0 })
+> colPre (!- " when ") wordAnd tcs (genTypeConstraint astContext)
+> genSynTypeConstraintList astContext tcs
-- ">")
| _ -> sepNone

and genSynTypeConstraintList astContext tcs =
match tcs with
| [] -> sepNone
| _ ->
let short =
colPre (sepSpace +> !- "when ") wordAnd tcs (genTypeConstraint astContext)

let long =
colPre (!- "when ") (sepNln +> wordAndFixed +> sepSpace) tcs (genTypeConstraint astContext)

autoIndentAndNlnIfExpressionExceedsPageWidth (expressionFitsOnRestOfLine short long)

and genLetBinding astContext pref b =
let genPref letKeyword =
genTriviaForOption SynBinding_Let letKeyword !-pref
Expand Down

0 comments on commit 434df47

Please sign in to comment.