Skip to content

Commit

Permalink
Initial commit for setting NewlinesAroundInnerMultilineExpressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Apr 3, 2021
1 parent eebef38 commit 5fed76b
Show file tree
Hide file tree
Showing 5 changed files with 259 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Fantomas.Tests/Fantomas.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
<Compile Include="DotIndexedSetTests.fs" />
<Compile Include="MultilineFunctionApplicationsInConditionExpressionsTests.fs" />
<Compile Include="KeepIndentInBranch.fs" />
<Compile Include="NewlinesAroundInnerMultilineExpressions.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Fantomas.Extras\Fantomas.Extras.fsproj" />
Expand Down
245 changes: 245 additions & 0 deletions src/Fantomas.Tests/NewlinesAroundInnerMultilineExpressions.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
module Fantomas.Tests.NewlinesAroundInnerMultilineExpressions

open NUnit.Framework
open FsUnit
open Fantomas.Tests.TestHelper

let config =
{ config with
NewlinesAroundInnerMultilineExpressions = false }

[<Test>]
let ``basic behavior`` () =
formatSourceString
false
"""
let topLevelFunction () =
let innerValue = 23
let innerMultilineFunction () =
// some comment
printfn "foo"
()
"""
config
|> prepend newline
|> should
equal
"""
let topLevelFunction () =
let innerValue = 23
let innerMultilineFunction () =
// some comment
printfn "foo"
()
"""

[<Test>]
let ``existing newlines are preserved`` () =
formatSourceString
false
"""
let topLevelFunction () =
let innerValue = 23
let innerMultilineFunction () =
// some comment
printfn "foo"
()
"""
config
|> prepend newline
|> should
equal
"""
let topLevelFunction () =
let innerValue = 23
let innerMultilineFunction () =
// some comment
printfn "foo"
()
"""

[<Test>]
let ``with sequential expressions`` () =
formatSourceString
false
"""
let topLevelFunction () =
printfn "Something to print"
try
nothing ()
with
| ex ->
splash ()
()
"""
config
|> prepend newline
|> should
equal
"""
let topLevelFunction () =
printfn "Something to print"
try
nothing ()
with ex -> splash ()
()
"""

[<Test>]
let ``disable blank lines around multiline expressions inside let binding, 1370`` () =
formatSourceString
false
"""
let emit nodes =
let an = AssemblyName("a")
let ab =
AppDomain.CurrentDomain.DefineDynamicAssembly(
an,
AssemblyBuilderAccess.RunAndCollect
)
let mb = ab.DefineDynamicModule("a")
let tb = mb.DefineType("Program")
nodes
"""
config
|> prepend newline
|> should
equal
"""
let emit nodes =
let an = AssemblyName("a")
let ab =
AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.RunAndCollect)
let mb = ab.DefineDynamicModule("a")
let tb = mb.DefineType("Program")
nodes
"""

[<Test>]
let ``disable blank lines inside type constructor`` () =
formatSourceString
false
"""
type MNIST(path:string, ?urls:seq<string>, ?train:bool, ?transform:Tensor->Tensor, ?targetTransform:Tensor->Tensor) =
inherit Dataset()
let path = Path.Combine(path, "mnist") |> Path.GetFullPath
let train = defaultArg train true
let transform = defaultArg transform (fun t -> (t - 0.1307) / 0.3081)
let targetTransform = defaultArg targetTransform id
let urls = List.ofSeq <| defaultArg urls (Seq.ofList
["http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz";
"http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz";
"http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz";
"http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz"])
let files = [for url in urls do Path.Combine(path, Path.GetFileName(url))]
let filesProcessed = [for file in files do Path.ChangeExtension(file, ".tensor")]
let data, target =
Directory.CreateDirectory(path) |> ignore
let mutable data = dsharp.zero()
let mutable target = dsharp.zero()
if train then
if not (File.Exists(files.[0])) then download urls.[0] files.[0]
if not (File.Exists(files.[1])) then download urls.[1] files.[1]
if File.Exists(filesProcessed.[0]) then data <- dsharp.load(filesProcessed.[0]) else data <- MNIST.LoadMNISTImages(files.[0]); dsharp.save(data, filesProcessed.[0])
if File.Exists(filesProcessed.[1]) then target <- dsharp.load(filesProcessed.[1]) else target <- MNIST.LoadMNISTLabels(files.[1]); dsharp.save(target, filesProcessed.[1])
else
if not (File.Exists(files.[2])) then download urls.[2] files.[2]
if not (File.Exists(files.[3])) then download urls.[3] files.[3]
if File.Exists(filesProcessed.[2]) then data <- dsharp.load(filesProcessed.[2]) else data <- MNIST.LoadMNISTImages(files.[2]); dsharp.save(data, filesProcessed.[2])
if File.Exists(filesProcessed.[3]) then target <- dsharp.load(filesProcessed.[3]) else target <- MNIST.LoadMNISTLabels(files.[3]); dsharp.save(target, filesProcessed.[3])
data, target
"""
config
|> prepend newline
|> should
equal
"""
type MNIST
(
path: string,
?urls: seq<string>,
?train: bool,
?transform: Tensor -> Tensor,
?targetTransform: Tensor -> Tensor
) =
inherit Dataset()
let path =
Path.Combine(path, "mnist") |> Path.GetFullPath
let train = defaultArg train true
let transform =
defaultArg transform (fun t -> (t - 0.1307) / 0.3081)
let targetTransform = defaultArg targetTransform id
let urls =
List.ofSeq
<| defaultArg
urls
(Seq.ofList [ "http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz"
"http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz"
"http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz"
"http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz" ])
let files =
[ for url in urls do
Path.Combine(path, Path.GetFileName(url)) ]
let filesProcessed =
[ for file in files do
Path.ChangeExtension(file, ".tensor") ]
let data, target =
Directory.CreateDirectory(path) |> ignore
let mutable data = dsharp.zero ()
let mutable target = dsharp.zero ()
if train then
if not (File.Exists(files.[0])) then
download urls.[0] files.[0]
if not (File.Exists(files.[1])) then
download urls.[1] files.[1]
if File.Exists(filesProcessed.[0]) then
data <- dsharp.load (filesProcessed.[0])
else
data <- MNIST.LoadMNISTImages(files.[0])
dsharp.save (data, filesProcessed.[0])
if File.Exists(filesProcessed.[1]) then
target <- dsharp.load (filesProcessed.[1])
else
target <- MNIST.LoadMNISTLabels(files.[1])
dsharp.save (target, filesProcessed.[1])
else
if not (File.Exists(files.[2])) then
download urls.[2] files.[2]
if not (File.Exists(files.[3])) then
download urls.[3] files.[3]
if File.Exists(filesProcessed.[2]) then
data <- dsharp.load (filesProcessed.[2])
else
data <- MNIST.LoadMNISTImages(files.[2])
dsharp.save (data, filesProcessed.[2])
if File.Exists(filesProcessed.[3]) then
target <- dsharp.load (filesProcessed.[3])
else
target <- MNIST.LoadMNISTLabels(files.[3])
dsharp.save (target, filesProcessed.[3])
data, target
"""

[<Test>]
let ``computation expressions`` () =
formatSourceString
false
"""
let comp =
eventually { for x in 1 .. 2 do
printfn " x = %d" x
return 3 + 4 }"""
config
|> prepend newline
|> should
equal
"""
let comp =
eventually {
for x in 1 .. 2 do
printfn " x = %d" x
return 3 + 4
}
"""
9 changes: 5 additions & 4 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1461,7 +1461,7 @@ and genExpr astContext synExpr ctx =
let r = getRangeOfCompExprStatement ces
let sepNln = getSepNln ces r
ColMultilineItem(expr, sepNln, r))
|> colWithNlnWhenItemIsMultiline
|> colWithNlnWhenItemIsMultilineBasedOnSetting

| ArrayOrListOfSeqExpr (isArray, e) as alNode ->
let astContext = { astContext with IsNakedRange = true }
Expand Down Expand Up @@ -2077,7 +2077,8 @@ and genExpr astContext synExpr ctx =
) ]

let items = letBindings bs @ synExpr e
atCurrentColumn (colWithNlnWhenItemIsMultiline items) ctx

atCurrentColumn (colWithNlnWhenItemIsMultilineBasedOnSetting items) ctx

// Could customize a bit if e is single line
| TryWith (e, cs) ->
Expand Down Expand Up @@ -2154,7 +2155,7 @@ and genExpr astContext synExpr ctx =

ColMultilineItem(expr, sepNln, r))

atCurrentColumn (colWithNlnWhenItemIsMultiline items)
atCurrentColumn (colWithNlnWhenItemIsMultilineBasedOnSetting items)

// A generalization of IfThenElse
| ElIf ((e1, e2, _, _, _) :: es, enOpt) ->
Expand Down Expand Up @@ -4466,7 +4467,7 @@ and genMemberDefnList astContext nodes =
:: (collectItems rest)

collectItems nodes
|> colWithNlnWhenItemIsMultiline
|> colWithNlnWhenItemIsMultilineBasedOnSetting

and genMemberDefn astContext node =
match node with
Expand Down
6 changes: 6 additions & 0 deletions src/Fantomas/Context.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1507,6 +1507,12 @@ let internal colWithNlnWhenItemIsMultiline (items: ColMultilineItem list) =

impl items

let internal colWithNlnWhenItemIsMultilineBasedOnSetting (items: ColMultilineItem list) (ctx: Context) =
if ctx.Config.NewlinesAroundInnerMultilineExpressions then
colWithNlnWhenItemIsMultiline items ctx
else
col sepNln items (fun (ColMultilineItem (expr, _, _)) -> expr) ctx

let internal genTriviaBeforeClausePipe (rangeOfClause: Range) ctx =
(Map.tryFindOrEmptyList BAR ctx.TriviaTokenNodes)
|> List.tryFind
Expand Down
2 changes: 2 additions & 0 deletions src/Fantomas/FormatConfig.fs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ type FormatConfig =
DisableElmishSyntax: bool
EndOfLine: EndOfLineStyle
KeepIndentInBranch: bool
NewlinesAroundInnerMultilineExpressions: bool
/// Pretty printing based on ASTs only
StrictMode: bool }

Expand Down Expand Up @@ -134,4 +135,5 @@ type FormatConfig =
DisableElmishSyntax = false
EndOfLine = EndOfLineStyle.FromEnvironment
KeepIndentInBranch = false
NewlinesAroundInnerMultilineExpressions = true
StrictMode = false }

0 comments on commit 5fed76b

Please sign in to comment.