Skip to content

Commit

Permalink
Force infix expressions on next line for certain operators.
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Jul 10, 2020
1 parent d115581 commit 3f9ac69
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 31 deletions.
7 changes: 6 additions & 1 deletion src/Fantomas.Tests/ClassTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,12 @@ module Logging =
Helpers.nullValuesToOptions (fun (x: Func<string>) -> (fun () -> x.Invoke())) func
let wrappedException = Helpers.nullValuesToOptions id exc
f level wrappedFunction wrappedException (parameters |> List.ofArray)
f
level
wrappedFunction
wrappedException
(parameters
|> List.ofArray)
LogProvider.SetCurrentLogProvider(QuartzLoggerWrapper(loggerFunction))
Expand Down
5 changes: 4 additions & 1 deletion src/Fantomas.Tests/ComparisonTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ let ``should keep the = on the same line in record def``() =
""" config
|> should equal """type UnionTypeConverter() =
inherit JsonConverter()
let doRead (reader: JsonReader) = reader.Read() |> ignore
let doRead (reader: JsonReader) =
reader.Read()
|> ignore
override x.CanConvert(typ: Type) =
let result =
Expand Down
41 changes: 32 additions & 9 deletions src/Fantomas.Tests/ComputationExpressionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ let factors number =
MaxFunctionBindingWidth = 65 })
|> prepend newline
|> should equal """
let factors number = { 2L .. number / 2L } |> Seq.filter (fun x -> number % x = 0L)
let factors number =
{ 2L .. number / 2L }
|> Seq.filter (fun x -> number % x = 0L)
"""

[<Test>]
Expand Down Expand Up @@ -224,7 +226,9 @@ let f2 =
// on the right side to be indented.
let! r =
match 0 with
| _ -> () |> async.Return
| _ ->
()
|> async.Return
return r
}
Expand Down Expand Up @@ -252,11 +256,15 @@ let f2 =
// on the right side to be indented.
let! r =
match 0 with
| _ -> () |> async.Return
| _ ->
()
|> async.Return
and! s =
match 0 with
| _ -> () |> async.Return
| _ ->
()
|> async.Return
return r + s
}
Expand Down Expand Up @@ -1131,9 +1139,18 @@ let ``multiline let bang + return in ce`` () =
|> should equal """
let divideByWorkflow x y w z =
maybe {
let! a = x |> divideBy y
let! b = a |> divideBy w
let! c = b |> divideBy z
let! a =
x
|> divideBy y
let! b =
a
|> divideBy w
let! c =
b
|> divideBy z
return c
}
"""
Expand Down Expand Up @@ -1266,7 +1283,11 @@ let ``use and let bang, 876`` () =
let private getAST log (req: HttpRequest) =
async {
use stream = new StreamReader(req.Body)
let! json = stream.ReadToEndAsync() |> Async.AwaitTask
let! json =
stream.ReadToEndAsync()
|> Async.AwaitTask
let parseRequest = Decoders.decodeInputRequest json
match parseRequest with
Expand Down Expand Up @@ -1353,7 +1374,9 @@ let rec loop () =
match msg with
| Handle (eventSource, command, reply) ->
let! stream = eventSource |> eventStore.GetStream
let! stream =
eventSource
|> eventStore.GetStream
let newEvents =
stream
Expand Down
20 changes: 15 additions & 5 deletions src/Fantomas.Tests/ElmishTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -374,12 +374,18 @@ let viewEntry todo dispatch =
input [ ClassName "toggle"
Type "checkbox"
Checked todo.completed
OnChange(fun _ -> Check(todo.id, (not todo.completed)) |> dispatch) ]
label [ OnDoubleClick(fun _ -> EditingEntry(todo.id, true) |> dispatch) ] [
OnChange(fun _ ->
Check(todo.id, (not todo.completed))
|> dispatch) ]
label [ OnDoubleClick(fun _ ->
EditingEntry(todo.id, true)
|> dispatch) ] [
str todo.description
]
button [ ClassName "destroy"
OnClick(fun _ -> Delete todo.id |> dispatch) ] []
OnClick(fun _ ->
Delete todo.id
|> dispatch) ] []
]
input [ ClassName "edit"
valueOrDefault todo.description
Expand All @@ -388,7 +394,9 @@ let viewEntry todo dispatch =
OnInput(fun ev ->
UpdateEntry(todo.id, !!ev.target?value)
|> dispatch)
OnBlur(fun _ -> EditingEntry(todo.id, false) |> dispatch)
OnBlur(fun _ ->
EditingEntry(todo.id, false)
|> dispatch)
onEnter (EditingEntry(todo.id, false)) dispatch ]
]
"""
Expand All @@ -404,7 +412,9 @@ let ``multiline attributes, no children`` () =
|> should equal """
let a =
button [ ClassName "destroy"
OnClick(fun _ -> Delete todo.id |> dispatch) ] []
OnClick(fun _ ->
Delete todo.id
|> dispatch) ] []
"""

[<Test>]
Expand Down
7 changes: 5 additions & 2 deletions src/Fantomas.Tests/FormattingSelectionOnlyTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ let r =
"b"
"" ]
|> List.map id""" config
|> should equal """[ "abc"; "a"; "b"; "" ] |> List.map id"""
|> should equal """[ "abc"; "a"; "b"; "" ]
|> List.map id"""

[<Test>]
let ``should preserve line breaks before and after selection``() =
Expand All @@ -101,7 +102,9 @@ type T () =
let items = []
override x.Reorder () =
items |> List.iter ignore""" { config with MaxFunctionBindingWidth = 120 }
|> should equal """ override x.Reorder() = items |> List.iter ignore"""
|> should equal """ override x.Reorder() =
items
|> List.iter ignore"""

[<Test>]
let ``should format the and branch of recursive functions``() =
Expand Down
3 changes: 2 additions & 1 deletion src/Fantomas.Tests/FunctionDefinitionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,8 @@ let fold
collectedErrors <- error :: collectedErrors
| Ok output -> collectedOutputs <- output :: collectedOutputs
funcs |> Seq.iter (fun validator -> runValidator validator input)
funcs
|> Seq.iter (fun validator -> runValidator validator input)
match anyErrors with
| true -> Error collectedErrors
| false -> Ok collectedOutputs
Expand Down
4 changes: 3 additions & 1 deletion src/Fantomas.Tests/LazyTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ let v = // <- Lazy "1"
|> prepend newline
|> should equal """
let v = // <- Lazy "1"
lazy (1 |> string)
lazy
1
|> string
"""

[<Test>]
Expand Down
7 changes: 6 additions & 1 deletion src/Fantomas.Tests/LetBindingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,12 @@ let SetQuartzLoggingFunction f =
Helpers.nullValuesToOptions (fun (x: Func<string>) -> (fun () -> x.Invoke())) func
let wrappedException = Helpers.nullValuesToOptions id exc
f level wrappedFunction wrappedException (parameters |> List.ofArray)
f
level
wrappedFunction
wrappedException
(parameters
|> List.ofArray)
LogProvider.SetCurrentLogProvider(QuartzLoggerWrapper(loggerFunction))
"""
Expand Down
14 changes: 11 additions & 3 deletions src/Fantomas.Tests/ListTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,17 @@ let flatten (A:'a[,]) = A |> Seq.cast<'a>
let getColumn c (A:_[,]) = flatten A.[*,c..c] |> Seq.toArray""" config
|> prepend newline
|> should equal """
let cast<'a> (A: obj [,]): 'a [,] = A |> Array2D.map unbox
let flatten (A: 'a [,]) = A |> Seq.cast<'a>
let getColumn c (A: _ [,]) = flatten A.[*, c..c] |> Seq.toArray
let cast<'a> (A: obj [,]): 'a [,] =
A
|> Array2D.map unbox
let flatten (A: 'a [,]) =
A
|> Seq.cast<'a>
let getColumn c (A: _ [,]) =
flatten A.[*, c..c]
|> Seq.toArray
"""

[<Test>]
Expand Down
5 changes: 4 additions & 1 deletion src/Fantomas.Tests/NoTrailingSpacesTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ let ``should not confuse me with an extra space at end of line v2``() =
"--op"
"extrude" |]
(fun () -> parseCmdLine args |> ignore) |> should throw typeof<Argu.ArguParseException>
(fun () ->
parseCmdLine args
|> ignore)
|> should throw typeof<Argu.ArguParseException>
"""
30 changes: 27 additions & 3 deletions src/Fantomas.Tests/OperatorTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,9 @@ let ``short expression before and after pipe`` () =
formatSourceString false "let a = b |> c" config
|> prepend newline
|> should equal """
let a = b |> c
let a =
b
|> c
"""

[<Test>]
Expand Down Expand Up @@ -312,7 +314,12 @@ let ``multiple short pipes`` () =
""" config
|> prepend newline
|> should equal """
let result = a && b |> f |> g |> h
let result =
a
&& b
|> f
|> g
|> h
"""

[<Test>]
Expand Down Expand Up @@ -353,7 +360,9 @@ let ``operator before verbatim string add extra space, 736`` () =
""" config
|> prepend newline
|> should equal """
Target M.Tools (fun _ -> !! @"Tools\Tools.sln" |> rebuild)
Target M.Tools (fun _ ->
!! @"Tools\Tools.sln"
|> rebuild)
"""

[<Test>]
Expand Down Expand Up @@ -453,3 +462,18 @@ let ``addition via function`` () =
|> should equal """
let a = (+) 7 8
"""

[<Test>]
let ``lambda piped to lambda should be multiline, 942`` () =
formatSourceString false """
let r (f : 'a -> 'b) (a : 'a) : 'b =
fun () ->
f a
|> fun f -> f ()
""" config
|> prepend newline
|> should equal """
let r (f: 'a -> 'b) (a: 'a): 'b =
fun () -> f a
|> fun f -> f ()
"""
8 changes: 6 additions & 2 deletions src/Fantomas.Tests/RecordTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,12 @@ let newDocument = //somecomment
|> prepend newline
|> should equal """
let newDocument = //somecomment
{ program = Encoding.Default.GetBytes(document.Program) |> Encoding.UTF8.GetString
content = Encoding.Default.GetBytes(document.Content) |> Encoding.UTF8.GetString
{ program =
Encoding.Default.GetBytes(document.Program)
|> Encoding.UTF8.GetString
content =
Encoding.Default.GetBytes(document.Content)
|> Encoding.UTF8.GetString
created = document.Created.ToLocalTime() }
|> JsonConvert.SerializeObject
"""
Expand Down
7 changes: 6 additions & 1 deletion src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1972,7 +1972,12 @@ and genInfixApps astContext synExprs =
(fun (s, opE, e) -> genInfixApp s opE e astContext)

and genInfixApp s (opE: SynExpr) e astContext =
if (noBreakInfixOps.Contains s) then
if (newLineInfixOps.Contains s) then
sepNlnUnlessLastEventIsNewline
+> node opE.Range s
+> sepSpace
+> genExpr astContext e
elif (noBreakInfixOps.Contains s) then
(sepSpace
+> node opE.Range s
+> (fun ctx ->
Expand Down

0 comments on commit 3f9ac69

Please sign in to comment.