Skip to content

Commit

Permalink
Fix issues with multiline generic type parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh DeGraw committed Apr 19, 2023
1 parent d3f2daa commit 4d8d328
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 28 deletions.
6 changes: 4 additions & 2 deletions src/Fantomas.Core.Tests/ClassTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1260,8 +1260,10 @@ module rec Xterm
[<AllowNullLiteral>]
type Terminal =
abstract onKey:
IEvent<{| key: string
domEvent: KeyboardEvent |}> with get, set
IEvent<
{| key: string
domEvent: KeyboardEvent |}
> with get, set

abstract onLineFeed: IEvent<unit> with get, set
"""
Expand Down
13 changes: 8 additions & 5 deletions src/Fantomas.Core.Tests/DallasTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1596,11 +1596,14 @@ let autoCompleteItems: cmap<DeclName, DeclarationListItem * Position * string<Lo
equal
"""
let autoCompleteItems
: cmap<DeclName, DeclarationListItem *
Position *
string<LocalPath> *
(Position -> option<string>) *
FSharp.Compiler.Syntax.ParsedInput> =
: cmap<
DeclName,
DeclarationListItem *
Position *
string<LocalPath> *
(Position -> option<string>) *
FSharp.Compiler.Syntax.ParsedInput
> =
cmap ()
"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,10 @@ let ``multiline abstract member without constraints, 2175`` () =
type FuseSortFunctionItem =
abstract Item:
key: string ->
U2<{| ``$``: string |}, ResizeArray<{| ``$``:
string
idx:
float |}>> with get, set
U2<
{| ``$``: string |},
ResizeArray<{| ``$``: string; idx: float |}>
> with get, set

abstract X: int
"""
11 changes: 8 additions & 3 deletions src/Fantomas.Core.Tests/SignatureTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1608,9 +1608,14 @@ namespace Foo
type Bar =
member Hello :
thing :
XLongLongLongLongLongLongLongLong<bool -> 'a, bool -> 'b, bool -> 'c, bool -> 'd, bool -> ('e -> 'f) -> 'g, ('h
-> 'i)
-> 'j> *
XLongLongLongLongLongLongLongLong<
bool -> 'a,
bool -> 'b,
bool -> 'c,
bool -> 'd,
bool -> ('e -> 'f) -> 'g,
('h -> 'i) -> 'j
> *
item : int list ->
LongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLong
"""
Expand Down
81 changes: 77 additions & 4 deletions src/Fantomas.Core.Tests/TypeAnnotationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Fantomas.Core.Tests.TypeAnnotationTests

open NUnit.Framework
open FsUnit
open Fantomas.Core
open Fantomas.Core.Tests.TestHelpers

[<Test>]
Expand Down Expand Up @@ -77,9 +78,13 @@ type X =
equal
"""
type X =
Teq<int, list int, System.DateTime array,
//
int>
Teq<
int,
list int,
System.DateTime array,
//
int
>
"""

[<Test>]
Expand Down Expand Up @@ -122,11 +127,79 @@ type CancellableTaskResultBuilderBase with
and ^Awaiter: (member GetResult: unit -> Result<'TResult1, 'Error>)>
(
sm:
byref<ResumableStateMachine<CancellableTaskResultStateMachineData<'TOverall, 'Error>>>,
byref<
ResumableStateMachine<
CancellableTaskResultStateMachineData<'TOverall, 'Error>
>
>,
task: CancellationToken -> ^TaskLike,
continuation:
('TResult1
-> CancellableTaskResultCode<'TOverall, 'Error, 'TResult2>)
) : bool =
true
"""

[<Test>]
let `` Aligned bracket style in anonymous record is respected, #2706`` () =
formatSourceString
false
"""
let private asJson (arm: IArmResource) =
arm.JsonModel
|> convertTo<{|
kind: string
properties: {| statisticsEnabled: bool |}
|}>
"""
{ config with
MultilineBracketStyle = Aligned }
|> prepend newline
|> should
equal
"""
let private asJson (arm: IArmResource) =
arm.JsonModel
|> convertTo<
{|
kind: string
properties: {| statisticsEnabled: bool |}
|}
>
"""

[<Test>]
let `` Aligned bracket style in anonymous record is respected for multiple types, #2706`` () =
formatSourceString
false
"""
let private asJson (arm: IArmResource) =
arm.JsonModel
|> convertTo<{|
kind: string
properties: {| statisticsEnabled: bool |}
|},{|
kind: string
properties: {| statisticsEnabled: bool |}
|}
>
"""
{ config with
MultilineBracketStyle = Aligned }
|> prepend newline
|> should
equal
"""
let private asJson (arm: IArmResource) =
arm.JsonModel
|> convertTo<
{|
kind: string
properties: {| statisticsEnabled: bool |}
|},
{|
kind: string
properties: {| statisticsEnabled: bool |}
|}
>
"""
42 changes: 40 additions & 2 deletions src/Fantomas.Core.Tests/TypeDeclarationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2945,8 +2945,12 @@ and [<CustomEquality ; NoComparison>] Bar<'context, 'a> =
(fun inner ->
if inner then
let bv =
unbox<Foo<'innerContextLongLongLong, 'bb
-> 'b>>
unbox<
Foo<
'innerContextLongLongLong,
'bb -> 'b
>
>
bf

this.InnerEquals af bf cont
Expand All @@ -2959,6 +2963,40 @@ and [<CustomEquality ; NoComparison>] Bar<'context, 'a> =
}
"""

[<Test>]
let ``multiple nested generic types`` () =
formatSourceString
false
"""
let bv =
unbox<
Fooadfadadfdadfadfadfadfadfadfsfdsfadfadadfada<
Foo<
innerContextLongLongLong,
bb
>
>
>
bf
"""
{ config with MaxLineLength = 10 }
|> prepend newline
|> should
equal
"""
let bv =
unbox<
Fooadfadadfdadfadfadfadfadfadfsfdsfadfadadfada<
Foo<
innerContextLongLongLong,
bb
>
>
>

bf
"""

[<Test>]
let ``a huge amount of type declarations`` () =
let sourceCode =
Expand Down
23 changes: 16 additions & 7 deletions src/Fantomas.Core/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2191,13 +2191,21 @@ let genKeepIdent (startNode: Node) (e: Expr) ctx =
indentSepNlnUnindent (genExpr e) ctx

let colGenericTypeParameters typeParameters =
coli sepComma typeParameters (fun idx t ->
let leadingSpace =
match t with
| Type.Var n when idx = 0 && n.Text.StartsWith("^") -> sepSpace
| _ -> sepNone
let short sep =
coli sep typeParameters (fun idx t ->
let leadingSpace =
match t with
| Type.Var n when idx = 0 && n.Text.StartsWith("^") -> sepSpace
| _ -> sepNone

leadingSpace +> genType t)

let long = indentSepNlnUnindent (short (sepComma +> sepNln)) +> sepNln

leadingSpace +> genType t)
// Multiline text type params should be unmodified
match typeParameters with
| [ Type.StaticConstant(Constant.FromText textNode) ] when textNode.Text.Contains("\n") -> (short sepComma)
| _ -> expressionFitsOnRestOfLine (short sepComma) long

let genFunctionNameWithMultilineLids (trailing: Context -> Context) (longIdent: IdentListNode) =
match longIdent.Content with
Expand Down Expand Up @@ -3037,7 +3045,8 @@ let genType (t: Type) =
+> optSingle genIdentListNodeWithDot node.PostIdentifier
+> genSingleTextNode node.LessThen
+> addExtraSpace
+> col sepComma node.Arguments genType
+> leadingExpressionIsMultiline (colGenericTypeParameters node.Arguments) (fun isMultiline ->
if isMultiline then !- " " else sepNone)
+> addExtraSpace
+> genSingleTextNode node.GreaterThan
|> genNode node
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.Core/Context.fs
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ let leadingExpressionResult leadingExpression continuationExpression (ctx: Conte

continuationExpression ((lineCountBefore, columnBefore), (lineCountAfter, columnAfter)) contextAfterLeading

/// A leading expression is not consider multiline if it has a comment before it.
/// A leading expression is not considered multiline if it has a comment before it.
/// For example
/// let a = 7
/// // foo
Expand Down

0 comments on commit 4d8d328

Please sign in to comment.