Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Long generic constraints #2273

Merged
merged 3 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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