Skip to content

Commit

Permalink
Extend MaxDotGetExpressionWidth (#1145)
Browse files Browse the repository at this point in the history
* First implementation for MaxDotGetExpressionWidth setting.

* Include TypeApp expressions in nested DotGetApp.

* Add documentation for fsharp_max_dot_get_expression_width.

* Bump to 4.2.0-alpha-001.

* Also split LongIdent parts of DotGet.

* Also split LongIdent parts inside App/TypeApp of DotGet.

* Bump to 4.2.0-alpha-002
  • Loading branch information
nojaf authored Sep 19, 2020
1 parent 6eb91d8 commit 30175c0
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 105 deletions.
4 changes: 4 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 4.2.0-alpha-002 - 09/2020

* Extended feature MaxDotGetExpressionWidth. [#501](https://github.com/fsprojects/fantomas/issues/501#issuecomment-695054715)

### 4.2.0-alpha-001 - 09/2020

* Feature MaxDotGetExpressionWidth. [#501](https://github.com/fsprojects/fantomas/issues/501)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
<GenerateProgramFile>false</GenerateProgramFile>
<Version>4.2.0-alpha-001</Version>
<Version>4.2.0-alpha-002</Version>
<NoWarn>FS0988</NoWarn>
<WarningsAsErrors>FS0025</WarningsAsErrors>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.CoreGlobalTool/Fantomas.CoreGlobalTool.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<ToolCommandName>fantomas</ToolCommandName>
<PackAsTool>True</PackAsTool>
<Version>4.2.0-alpha-001</Version>
<Version>4.2.0-alpha-002</Version>
<AssemblyName>fantomas-tool</AssemblyName>
<WarningsAsErrors>FS0025</WarningsAsErrors>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.Extras/Fantomas.Extras.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>4.2.0-alpha-001</Version>
<Version>4.2.0-alpha-002</Version>
<Description>Utility package for Fantomas</Description>
<WarningsAsErrors>FS0025</WarningsAsErrors>
</PropertyGroup>
Expand Down
97 changes: 81 additions & 16 deletions src/Fantomas.Tests/DotGetTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ Microsoft.FSharp.Reflection.FSharpType.GetUnionCases(typeof<option<option<unit>>
""" config
|> prepend newline
|> should equal """
Microsoft.FSharp.Reflection.FSharpType.GetUnionCases(typeof<option<option<unit>>>
Microsoft
.FSharp
.Reflection
.FSharpType
.GetUnionCases(typeof<option<option<unit>>>
.GetGenericTypeDefinition()
.MakeGenericType(t))
.Assembly
Expand All @@ -25,7 +29,13 @@ System.Diagnostics.FileVersionInfo.GetVersionInfo(
""" { config with MaxLineLength = 80 }
|> prepend newline
|> should equal """
System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly()
System
.Diagnostics
.FileVersionInfo
.GetVersionInfo(System
.Reflection
.Assembly
.GetExecutingAssembly()
.Location)
.FileVersion
"""
Expand All @@ -44,7 +54,13 @@ let ``split chained method call expression, 246`` () =
root.SetAttribute
("driverVersion",
"AltCover.Recorder "
+ System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly()
+ System
.Diagnostics
.FileVersionInfo
.GetVersionInfo(System
.Reflection
.Assembly
.GetExecutingAssembly()
.Location)
.FileVersion)
"""
Expand All @@ -56,12 +72,9 @@ Equinox.EventStore.Resolver<'event, 'state, _>(gateway, codec, fold, initial, ca
""" { config with MaxLineLength = 100 }
|> prepend newline
|> should equal """
Equinox.EventStore.Resolver<'event, 'state, _>(gateway,
codec,
fold,
initial,
cacheStrategy,
accessStrategy)
Equinox
.EventStore
.Resolver<'event, 'state, _>(gateway, codec, fold, initial, cacheStrategy, accessStrategy)
.Resolve
"""

Expand Down Expand Up @@ -102,7 +115,9 @@ module Services =
snapshot: (('event -> bool) * ('state -> 'event))) =
match storage with
| Storage.MemoryStore store ->
Equinox.MemoryStore.Resolver(store, FsCodec.Box.Codec.Create(), fold, initial)
Equinox
.MemoryStore
.Resolver(store, FsCodec.Box.Codec.Create(), fold, initial)
.Resolve
| Storage.EventStore (gateway, cache) ->
let accessStrategy =
Expand All @@ -111,12 +126,9 @@ module Services =
let cacheStrategy =
Equinox.EventStore.CachingStrategy.SlidingWindow(cache, TimeSpan.FromMinutes 20.)
Equinox.EventStore.Resolver<'event, 'state, _>(gateway,
codec,
fold,
initial,
cacheStrategy,
accessStrategy)
Equinox
.EventStore
.Resolver<'event, 'state, _>(gateway, codec, fold, initial, cacheStrategy, accessStrategy)
.Resolve
"""

Expand Down Expand Up @@ -207,3 +219,56 @@ let c =
.UseSerilog(dispose = true)
.UseStartup<Startup>()
"""

[<Test>]
let ``inner SynExpr.LongIdent should also be split`` () =
formatSourceString false """
let firstName =
define
.Attribute
.ParsedRes(FirstName.value, FirstName.create)
.Get(fun u -> u.FirstName)
.SetRes(userSetter User.setFirstName)
""" config
|> prepend newline
|> should equal """
let firstName =
define
.Attribute
.ParsedRes(FirstName.value, FirstName.create)
.Get(fun u -> u.FirstName)
.SetRes(userSetter User.setFirstName)
"""

[<Test>]
let ``long ident with dots inside app inside dotget`` () =
formatSourceString false """
Equinox.MemoryStore.Resolver(store, FsCodec.Box.Codec.Create(), fold, initial)
.Resolve
""" config
|> prepend newline
|> should equal """
Equinox
.MemoryStore
.Resolver(store, FsCodec.Box.Codec.Create(), fold, initial)
.Resolve
"""

[<Test>]
let ``long ident with dots inside type app inside dotget`` () =
formatSourceString false """
Equinox.EventStore.Resolver<'event, 'state, _>(gateway,
codec,
fold,
initial,
cacheStrategy,
accessStrategy)
.Resolve
""" config
|> prepend newline
|> should equal """
Equinox
.EventStore
.Resolver<'event, 'state, _>(gateway, codec, fold, initial, cacheStrategy, accessStrategy)
.Resolve
"""
2 changes: 1 addition & 1 deletion src/Fantomas.Tests/Fantomas.Tests.fsproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>4.2.0-alpha-001</Version>
<Version>4.2.0-alpha-002</Version>
<NoWarn>FS0988</NoWarn>
<TargetFramework>netcoreapp3.1</TargetFramework>
<WarningsAsErrors>FS0025</WarningsAsErrors>
Expand Down
90 changes: 46 additions & 44 deletions src/Fantomas.Tests/RecordTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,8 @@ let ``meaningful space should be preserved, 353`` () =
Verbosity = Some DotNet.Verbosity.Minimal }).WithParameters""" config
|> prepend newline
|> should equal """
to'.WithCommon(fun o' ->
to'
.WithCommon(fun o' ->
{ dotnetOptions o' with
WorkingDirectory = Path.getFullName "RegressionTesting/issue29"
Verbosity = Some DotNet.Verbosity.Minimal })
Expand Down Expand Up @@ -448,71 +449,72 @@ type Database =
static member Default() =
Database
.Lowdb.defaults({ Version = CurrentVersion
Questions =
[| { Id = 0
AuthorId = 1
Title = \"What is the average wing speed of an unladen swallow?\"
Description = \"\"\"
.Lowdb
.defaults({ Version = CurrentVersion
Questions =
[| { Id = 0
AuthorId = 1
Title = \"What is the average wing speed of an unladen swallow?\"
Description = \"\"\"
Hello, yesterday I saw a flight of swallows and was wondering what their **average wing speed** is?
If you know the answer please share it.
\"\"\"
Answers =
[| { Id = 0
CreatedAt = DateTime.Parse \"2017-09-14T19:57:33.103Z\"
AuthorId = 0
Score = 2
Content = \"\"\"
Answers =
[| { Id = 0
CreatedAt = DateTime.Parse \"2017-09-14T19:57:33.103Z\"
AuthorId = 0
Score = 2
Content = \"\"\"
> What do you mean, an African or European Swallow?
>
> Monty Python’s: The Holy Grail
Ok I must admit, I use google to search the question and found a post explaining the reference :).
I thought you were asking it seriously, well done.
x\"\"\" }
{ Id = 1
CreatedAt = DateTime.Parse \"2017-09-14T20:07:27.103Z\"
AuthorId = 2
Score = 1
Content = \"\"\"
x\"\"\" }
{ Id = 1
CreatedAt = DateTime.Parse \"2017-09-14T20:07:27.103Z\"
AuthorId = 2
Score = 1
Content = \"\"\"
Maxime,
I believe you found [this blog post](http://www.saratoga.com/how-should-i-know/2013/07/what-is-the-average-air-speed-velocity-of-a-laden-swallow/).
And so Robin, the conclusion of the post is:
> In the end, it’s concluded that the airspeed velocity of a (European) unladen swallow is about 24 miles per hour or 11 meters per second.
\"\"\" } |]
CreatedAt = DateTime.Parse \"2017-09-14T17:44:28.103Z\" }
{ Id = 1
AuthorId = 0
Title = \"Why did you create Fable?\"
Description = \"\"\"
\"\"\" } |]
CreatedAt = DateTime.Parse \"2017-09-14T17:44:28.103Z\" }
{ Id = 1
AuthorId = 0
Title = \"Why did you create Fable?\"
Description = \"\"\"
Hello Alfonso,
I wanted to know why you created Fable. Did you always plan to use F#? Or were you thinking in others languages?
\"\"\"
Answers = [||]
CreatedAt = DateTime.Parse \"2017-09-12T09:27:28.103Z\" } |]
Users =
[| { Id = 0
Firstname = \"Maxime\"
Surname = \"Mangel\"
Avatar = \"maxime_mangel.png\" }
{ Id = 1
Firstname = \"Robin\"
Surname = \"Munn\"
Avatar = \"robin_munn.png\" }
{ Id = 2
Firstname = \"Alfonso\"
Surname = \"Garciacaro\"
Avatar = \"alfonso_garciacaro.png\" }
{ Id = 3
Firstname = \"Guest\"
Surname = \"\"
Avatar = \"guest.png\" } |] })
Answers = [||]
CreatedAt = DateTime.Parse \"2017-09-12T09:27:28.103Z\" } |]
Users =
[| { Id = 0
Firstname = \"Maxime\"
Surname = \"Mangel\"
Avatar = \"maxime_mangel.png\" }
{ Id = 1
Firstname = \"Robin\"
Surname = \"Munn\"
Avatar = \"robin_munn.png\" }
{ Id = 2
Firstname = \"Alfonso\"
Surname = \"Garciacaro\"
Avatar = \"alfonso_garciacaro.png\" }
{ Id = 3
Firstname = \"Guest\"
Surname = \"\"
Avatar = \"guest.png\" } |] })
.write()
Logger.debug \"Database restored\"
Expand Down
67 changes: 37 additions & 30 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1787,32 +1787,6 @@ and genExpr astContext synExpr =
+> sepSpace
+> genExpr astContext e3)

// This filters a few long examples of App
| DotGetAppSpecial (s, es) ->
let genIdent s r ts e =
(!-(sprintf ".%s" s) |> genTriviaFor Ident_ r)
+> genGenericTypeParameters astContext ts
+> ifElse (hasParenthesis e || isArrayOrList e) sepNone sepSpace

let shortExpr =
!-s
+> atCurrentColumn
(colAutoNlnSkip0 sepNone es (fun ((s, r), e, ts) -> (genIdent s r ts e +> genExpr astContext e)))

let longExpr =
!-s
+> indent
+> sepNln
+> col sepNln es (fun ((s, r), e, ts) ->
genIdent s r ts e
+> genExpr
{ astContext with
IsInsideDotGet = true }
e)
+> unindent

fun ctx -> isShortExpression ctx.Config.MaxDotGetExpressionWidth shortExpr longExpr ctx

| DotGetApp (e, es) as appNode ->
fun (ctx: Context) ->
// find all the lids recursively + range of do expr
Expand Down Expand Up @@ -1844,6 +1818,13 @@ and genExpr astContext synExpr =
+> genGenericTypeParameters astContext ts
+> genExpr astContext e2
+> unindent
| App (LongIdentPieces (lids), [ e2 ]) when (List.moreThanOne lids) ->
!-(List.head lids)
+> indent
+> sep
+> col sep (List.tail lids) (fun s -> !-(sprintf ".%s" s))
+> genExpr astContext e2
+> unindent
| App (e1, [ e2 ]) ->
noNln
(genExpr astContext e1
Expand Down Expand Up @@ -2538,10 +2519,36 @@ and genExpr astContext synExpr =
-- (sprintf ".%s" s)

let longExpr =
genExpr
{ astContext with
IsInsideDotGet = true }
e
let expr =
match e with
| App (LongIdentPieces (lids), [ e2 ]) when (List.moreThanOne lids) ->
!-(List.head lids)
+> indent
+> sepNln
+> col sepNln (List.tail lids) (fun s -> !-(sprintf ".%s" s))
+> genExpr
{ astContext with
IsInsideDotGet = true }
e2
+> unindent
| App (TypeApp (LongIdentPieces (lids), ts), [ e2 ]) when (List.moreThanOne lids) ->
!-(List.head lids)
+> indent
+> sepNln
+> col sepNln (List.tail lids) (fun s -> !-(sprintf ".%s" s))
+> genGenericTypeParameters astContext ts
+> genExpr
{ astContext with
IsInsideDotGet = true }
e2
+> unindent
| _ ->
genExpr
{ astContext with
IsInsideDotGet = true }
e

expr
+> indent
+> sepNln
-- (sprintf ".%s" s)
Expand Down
Loading

0 comments on commit 30175c0

Please sign in to comment.