Skip to content

Commit

Permalink
Merge pull request #721 from nojaf/feature/short-record-expression
Browse files Browse the repository at this point in the history
Short expression check.
  • Loading branch information
nojaf committed Apr 13, 2020
2 parents 233e314 + 11af14d commit 1ef27b7
Show file tree
Hide file tree
Showing 57 changed files with 1,706 additions and 1,255 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
- name: Setup .NET Core
uses: actions/setup-dotnet@v1.4.0
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/myget.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
token: ${{secrets.github_token}}
- name: Print new build number
run: echo Build number is $BUILD_NUMBER
- uses: actions/checkout@v1
- uses: actions/checkout@v2
- name: Setup .NET Core
uses: actions/setup-dotnet@v1.4.0
with:
Expand Down
33 changes: 0 additions & 33 deletions docs/Documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,38 +223,6 @@ For that reason, this setting is considered **deprecated** and will be **removed

if being set, pretty printing is only done via ASTs. Compiler directives, inline comments and block comments will be ignored.

##### `--keepNewlineAfter`

if being set, newlines found in the source text will be kept in certain conditions.

```fsharp
let a =
42
```

will remain the same, the newline after the `=` was detected and preserved.

```fsharp
let config =
Builder()
.A()
.B()
.C()
```

will remain the same, the newline before the `.` was detected and preserved.

```fsharp
match meh with
| Foo ->
printfn "foo"
| Bar ->
printfn "bar"
```

will remain the same, the newline after `->` was detected and preserved.


##### `--maxIfThenElseShortWidth <number>`

`number` if being set, controls when if/then/else expressions will be formatted as single line or as multiple lines.
Expand Down Expand Up @@ -307,7 +275,6 @@ A default configuration file would look like
"IndentOnTryWith":false,
"ReorderOpenDeclaration":false,
"SpaceAroundDelimiter":true ,
"KeepNewlineAfter":false,
"MaxIfThenElseShortWidth":40,
"StrictMode":false
}
Expand Down
3 changes: 0 additions & 3 deletions src/Fantomas.CoreGlobalTool/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ type Arguments =
| [<Unique;AltCommandLine("--indentOnTryWith")>] IndentOnTryWith
| [<Unique;AltCommandLine("--reorderOpenDeclaration")>] ReorderOpenDeclaration
| [<Unique;AltCommandLine("--noSpaceAroundDelimiter")>] NoSpaceAroundDelimiter
| [<Unique;AltCommandLine("--keepNewlineAfter")>] KeepNewlineAfter
| [<Unique;AltCommandLine("--maxIfThenElseShortWidth ")>] MaxIfThenElseShortWidth of int
| [<Unique;AltCommandLine("--strictMode")>] StrictMode
| [<Unique;AltCommandLine("-c")>] Config of string
Expand All @@ -51,7 +50,6 @@ with
| IndentOnTryWith -> "Enable indentation on try/with block (default = false)."
| ReorderOpenDeclaration -> "[DEPRECATED] Enable reordering open declarations (default = false)."
| NoSpaceAroundDelimiter -> "Disable spaces after starting and before ending of lists, arrays, sequences and records (default = true)."
| KeepNewlineAfter -> "Keep newlines found after = in let bindings, -> in pattern matching and chained function calls (default = false)."
| MaxIfThenElseShortWidth _ -> "Set the max length of any expression in an if expression before formatting on multiple lines (default = 40)."
| StrictMode -> "Enable strict mode (ignoring directives and comments and printing literals in canonical forms) (default = false)."
| Config _ -> "Use configuration found in file or folder."
Expand Down Expand Up @@ -277,7 +275,6 @@ let main argv =
writeInColor ConsoleColor.DarkYellow "Warning: ReorderOpenDeclaration will be removed in the next major version. Using this feature can lead to compilation errors after formatting."
{ acc with ReorderOpenDeclaration = true }
| NoSpaceAroundDelimiter -> { acc with SpaceAroundDelimiter = false }
| KeepNewlineAfter -> { acc with KeepNewlineAfter = true }
| MaxIfThenElseShortWidth m -> { acc with MaxIfThenElseShortWidth = m }
| StrictMode -> { acc with StrictMode = true }
) defaultConfig
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.Tests/ActivePatternTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ let (|ParseRegex|_|) regex str =
let m = Regex(regex).Match(str)
if m.Success
then Some (List.tail [ for x in m.Groups -> x.Value ])
else None""" config
else None""" ({ config with MaxLetBindingWidth = 30 })
|> prepend newline
|> should equal """
let (|Even|Odd|) input =
Expand Down
10 changes: 6 additions & 4 deletions src/Fantomas.Tests/AppTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ let a =
let a =
b
|> List.exists (fun p ->
p.a && p.b |> List.exists (fun o -> o.a = "lorem ipsum dolor sit amet"))
p.a
&& p.b
|> List.exists (fun o -> o.a = "lorem ipsum dolor sit amet"))
"""

// compile error due to expression starting before the beginning of the function expression
Expand All @@ -39,12 +41,12 @@ let a s =
|> prepend newline
|> should equal @"
let a s =
if s <> """" then
printfn """"""fooo
if s <> """"
then printfn """"""fooo
%s
%s
%s
%s"""""" (llloooooooooooooooooooooooooo s) s s s
%s"""""" (llloooooooooooooooooooooooooo s) s s s
"

// compile error due to expression starting before the beginning of the function expression
Expand Down
62 changes: 51 additions & 11 deletions src/Fantomas.Tests/ClassTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ type Shape2D(x0: float, y0: float) =
and set yval = y <- yval
abstract Area: float
abstract Perimeter: float
abstract Name: string
Expand All @@ -117,6 +118,7 @@ type Shape2D(x0: float, y0: float) =
y <- y + dy
abstract Rotate: float -> unit
default this.Rotate(angle) = rotAngle <- rotAngle + angle
"""

Expand Down Expand Up @@ -157,7 +159,7 @@ type DerivedClass =
inherit BaseClass
val string2 : string
new (str1, str2) = { inherit BaseClass(str1); string2 = str2 }
new (str2) = { inherit BaseClass(); string2 = str2 }""" config
new (str2) = { inherit BaseClass(); string2 = str2 }""" ({ config with MaxRecordWidth = 45 })
|> prepend newline
|> should equal """
type BaseClass =
Expand Down Expand Up @@ -243,12 +245,14 @@ type MyClassDerived2(y: int) =
|> should equal """
type MyClassBase2(x: int) =
let mutable z = x * x
do
for i in 1 .. z do
printf "%d " i
type MyClassDerived2(y: int) =
inherit MyClassBase2(y * 2)
do
for i in 1 .. y do
printf "%d " i
Expand Down Expand Up @@ -278,7 +282,8 @@ let ``should keep parens in class inheritance in the right place``() =
class
inherit DGMLClass()
let functions = System.Collections.Generic.Dictionary<string, IState>()
let functions =
System.Collections.Generic.Dictionary<string, IState>()
end
"""

Expand Down Expand Up @@ -372,8 +377,8 @@ let longNamedClasslongNamedClasslongNamedClasslongNamedClasslongNamedClasslongNa
System.String.Concat
("a",
"b"
+ (longNamedFunlongNamedFunlongNamedFunlongNamedFunlongNamedFun
(longNamedClasslongNamedClasslongNamedClasslongNamedClasslongNamedClasslongNamedClass)).Property)
+ longNamedFunlongNamedFunlongNamedFunlongNamedFunlongNamedFun
(longNamedClasslongNamedClasslongNamedClasslongNamedClasslongNamedClasslongNamedClass).Property)
"""

[<Test>]
Expand All @@ -390,8 +395,42 @@ type Exception with
"""

[<Test>]
let ``no extra new lines between interface members, 569``() =
shouldNotChangeAfterFormat """
let ``no extra new lines between interface members, 569`` () =
formatSourceString false """
namespace Quartz.Fsharp
module Logging =
open Quartz.Logging
open System
//todo: it seems that quartz doesn't use mapped and nested context,
//however, check if this is the best implementation for this interface
type private QuartzLoggerWrapper(f) =
interface ILogProvider with
member this.OpenMappedContext(_, _) =
{ new IDisposable with
member this.Dispose() = () }
member this.OpenNestedContext _ =
{ new IDisposable with
member this.Dispose() = () }
member this.GetLogger _name = new Logger(f)
let SetQuartzLoggingFunction f =
let loggerFunction level (func: Func<string>) exc parameters =
let wrappedFunction =
Helpers.nullValuesToOptions (fun (x: Func<string>) -> (fun () -> x.Invoke())) func
let wrappedException = Helpers.nullValuesToOptions id exc
f level wrappedFunction wrappedException (parameters |> List.ofArray)
LogProvider.SetCurrentLogProvider(QuartzLoggerWrapper(loggerFunction))
let SetQuartzLogger l = LogProvider.SetCurrentLogProvider(l)
""" config
|> prepend newline
|> should equal """
namespace Quartz.Fsharp
module Logging =
Expand All @@ -415,9 +454,12 @@ module Logging =
let SetQuartzLoggingFunction f =
let loggerFunction level (func: Func<string>) exc parameters =
let wrappedFunction = Helpers.nullValuesToOptions (fun (x: Func<string>) -> (fun () -> x.Invoke())) func
let wrappedFunction =
Helpers.nullValuesToOptions (fun (x: Func<string>) -> (fun () -> x.Invoke())) func
let wrappedException = Helpers.nullValuesToOptions id exc
f level wrappedFunction wrappedException (parameters |> List.ofArray)
LogProvider.SetCurrentLogProvider(QuartzLoggerWrapper(loggerFunction))
let SetQuartzLogger l = LogProvider.SetCurrentLogProvider(l)
Expand All @@ -428,11 +470,9 @@ let ``no extra new lines between type members, 569``() =
shouldNotChangeAfterFormat """
type A() =
member this.MemberA =
if true then 0 else 1
member this.MemberA = if true then 0 else 1
member this.MemberB =
if true then 2 else 3
member this.MemberB = if true then 2 else 3
member this.MemberC = 0
"""
Expand Down
12 changes: 9 additions & 3 deletions src/Fantomas.Tests/CommentTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ let print_30_permut () =
Array.init n (fun i ->
Console.Write(i + 1)
i)
permutation
"""

Expand All @@ -66,6 +67,7 @@ let print_30_permut () =
Array.init n (fun (i, j) ->
Console.Write(i + 1)
i)
permutation
"""

Expand Down Expand Up @@ -435,7 +437,6 @@ type C () =
|> prepend newline
|> should equal """
type C() =

let rec g x = h x
and h x = g x

Expand Down Expand Up @@ -588,7 +589,7 @@ type substring =
strB.String, strB.Offset,
min strA.Length strB.Length)
#endif
""" config
""" ({ config with MaxInfixOperatorExpression = 60 })
|> should equal """(*

Copyright 2010-2012 TidePowerd Ltd.
Expand Down Expand Up @@ -661,7 +662,12 @@ type substring =
// NOTE: we don't have to null check here because System.String.Compare
// gives reliable results on null values.
System.String.Compare
(strA.String, strA.Offset, strB.String, strB.Offset, min strA.Length strB.Length, false,
(strA.String,
strA.Offset,
strB.String,
strB.Offset,
min strA.Length strB.Length,
false,
CultureInfo.InvariantCulture)
#else
// NOTE: we don't have to null check here because System.String.CompareOrdinal
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 @@ -18,9 +18,12 @@ let ``should keep the = on the same line in record def``() =
|> should equal """type UnionTypeConverter() =
inherit JsonConverter()
let doRead (reader: JsonReader) = reader.Read() |> ignore
override x.CanConvert(typ: Type) =
let result =
((typ.GetInterface(typeof<System.Collections.IEnumerable>.FullName) = null) && FSharpType.IsUnion typ)
((typ.GetInterface(typeof<System.Collections.IEnumerable>.FullName) = null)
&& FSharpType.IsUnion typ)
result
"""

Expand Down
Loading

0 comments on commit 1ef27b7

Please sign in to comment.