From 6aaf7afa8cbb55daeb72a216aff939ae9483c8dc Mon Sep 17 00:00:00 2001 From: nojaf Date: Sat, 4 Dec 2021 18:42:47 +0100 Subject: [PATCH] Put stars in long tuple in long signature at the end. --- src/Fantomas.Tests/InterfaceTests.fs | 44 ++++---- src/Fantomas.Tests/SignatureTests.fs | 161 ++++++++++++++++++++++++++- src/Fantomas/CodePrinter.fs | 34 +++++- src/Fantomas/Context.fs | 18 +++ 4 files changed, 228 insertions(+), 29 deletions(-) diff --git a/src/Fantomas.Tests/InterfaceTests.fs b/src/Fantomas.Tests/InterfaceTests.fs index 47cc936dea..91003abb5b 100644 --- a/src/Fantomas.Tests/InterfaceTests.fs +++ b/src/Fantomas.Tests/InterfaceTests.fs @@ -274,18 +274,18 @@ type Test = """ type Test = abstract RunJobs: - folder: string - * ?jobs: string - * ?ctm: string - * ?createDuplicate: bool - * ?hold: bool - * ?ignoreCriteria: bool - * ?independentFlow: bool - * ?orderDate: string - * ?orderIntoFolder: string - * ?variables: Dictionary [] - * ?waitForOrderDate: bool -> - string + folder: string * + ?jobs: string * + ?ctm: string * + ?createDuplicate: bool * + ?hold: bool * + ?ignoreCriteria: bool * + ?independentFlow: bool * + ?orderDate: string * + ?orderIntoFolder: string * + ?variables: Dictionary [] * + ?waitForOrderDate: bool -> + string override this.RunJobs ( @@ -384,7 +384,7 @@ type IFoo = foo : string -> bar : string -> baz : string -> - int + int """ [] @@ -409,7 +409,7 @@ type IFoo = string -> int -> string -> - string + string """ [] @@ -429,9 +429,9 @@ type IFoo = """ type IFoo = abstract Bar : - [] bar : string - * [] baz : string -> - Task + [] bar : string * + [] baz : string -> + Task """ [] @@ -453,7 +453,7 @@ type IFoo = abstract Bar : i : int -> a : string * foo : int -> - string + string """ [] @@ -474,10 +474,10 @@ type IFoo = type IFoo = abstract Bar : i : int -> - a : string - * foo : int - * someReallyLongNameThatMakesTheTupleMultiLine : string -> - string + a : string * + foo : int * + someReallyLongNameThatMakesTheTupleMultiLine : string -> + string """ [] diff --git a/src/Fantomas.Tests/SignatureTests.fs b/src/Fantomas.Tests/SignatureTests.fs index eb23aae6b8..5c6d46768b 100644 --- a/src/Fantomas.Tests/SignatureTests.fs +++ b/src/Fantomas.Tests/SignatureTests.fs @@ -604,7 +604,7 @@ type CodeFormatter = /// Parse a source string using given config static member ParseAsync: fileName: string * source: SourceOrigin * parsingOptions: FSharpParsingOptions * checker: FSharpChecker -> - Async<(ParsedInput * string list) array> + Async<(ParsedInput * string list) array> """ [] @@ -1256,7 +1256,7 @@ val create: something_really_long: unit -> another_really_long_thing: unit -> and_another_to_make_the_line_long_enough: unit -> - unit + unit """ [] @@ -1586,9 +1586,9 @@ type Bar = member Hello : thing : XLongLongLongLongLongLongLongLong 'a, bool -> 'b, bool -> 'c, bool -> 'd, bool -> ('e -> 'f) - -> 'g, ('h -> 'i) -> 'j> - * item : int list -> - LongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLong + -> 'g, ('h -> 'i) -> 'j> * + item : int list -> + LongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLong """ [] @@ -1711,3 +1711,154 @@ open FSharp.Compiler.AbstractIL.IL [] exception SyntaxError of obj * range: range """ + +[] +let ``stars in the back`` () = + formatSourceString + true + """ + namespace Oslo + type Meh = + member ResolveDependencies: + scriptDirectory: string + * scriptName: string + * scriptExt: string + * timeout: int -> + obj +""" + { config with MaxLineLength = 60 } + |> prepend newline + |> should + equal + """ +namespace Oslo + +type Meh = + member ResolveDependencies: + scriptDirectory: string * + scriptName: string * + scriptExt: string * + timeout: int -> + obj +""" + +[] +let ``add extra indent when the next parameter is a tuple`` () = + formatSourceString + true + """ +namespace Oslo + +type Meh = + member ResolveDependencies: + scriptDirectory: string * scriptName: string -> + scriptName: string + * scriptExt: string + * timeout: int -> + obj +""" + { config with MaxLineLength = 5 } + |> prepend newline + |> should + equal + """ +namespace Oslo + +type Meh = + member ResolveDependencies: + scriptDirectory: string * + scriptName: string -> + scriptName: string * + scriptExt: string * + timeout: int -> + obj +""" + +[] +let ``mixed tuple non tuple`` () = + formatSourceString + true + """ +namespace Oslo + +type Meh = + member ResolveDependencies: + scriptDirectory: string * scriptName: string -> + scriptName: string -> + obj +""" + { config with MaxLineLength = 30 } + |> prepend newline + |> should + equal + """ +namespace Oslo + +type Meh = + member ResolveDependencies: + scriptDirectory: string * + scriptName: string -> + scriptName: string -> + obj +""" + +[] +let ``unindent correctly after type signature`` () = + formatSourceString + true + """ +namespace Oslo + +type Meh = + member ResolveDependencies: + scriptDirectory: string * scriptName: string -> + scriptName: string -> + obj + +val x : int +""" + { config with MaxLineLength = 30 } + |> prepend newline + |> should + equal + """ +namespace Oslo + +type Meh = + member ResolveDependencies: + scriptDirectory: string * + scriptName: string -> + scriptName: string -> + obj + +val x: int +""" + +[] +let ``only indent after tuple in non last position`` () = + formatSourceString + true + """ +namespace Oslo + +type Meh = + member ResolveDependencies: + criptName: string -> foo: string -> scriptDirectory: string * scriptName: string -> // after a tuple, mixed needs an indent + scriptName: string -> obj +""" + { config with MaxLineLength = 30 } + |> prepend newline + |> should + equal + """ +namespace Oslo + +type Meh = + member ResolveDependencies: + criptName: string -> + foo: string -> + scriptDirectory: string * + scriptName: string -> // after a tuple, mixed needs an indent + scriptName: string -> + obj +""" diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index d6dc86dfac..64396320e6 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -4599,7 +4599,7 @@ and genTypeList astContext node = col sepBefore ts' (snd >> genType astContext hasBracket) let shortExpr = gt sepStar - let longExpr = gt (sepNln +> sepStarFixed) + let longExpr = gt (sepSpace +> sepStarFixed +> sepNln) expressionFitsOnRestOfLine shortExpr longExpr | _, [ ArgInfo (ats, so, isOpt) ] -> @@ -4621,7 +4621,37 @@ and genTypeList astContext node = let shortExpr = col sepArrow node gt - let longExpr = col (sepArrow +> sepNln) node gt + let longExpr = + let lastIndex = node.Length - 1 + + let isTupleOrLastIndex index = + index = lastIndex + || match List.tryItem (index - 1) node with + | Some (TTuple _, _) -> true + | _ -> false + + let resetIndent = + if lastIndex < 0 then + id + else + [ 0 .. lastIndex ] + |> List.choose + (fun idx -> + if isTupleOrLastIndex idx then + Some unindent + else + None) + |> List.reduce (+>) + + colii + (fun idx -> + sepSpace + +> sepArrowFixed + +> onlyIf (isTupleOrLastIndex idx) indent + +> sepNln) + node + (fun _ -> gt) + +> resetIndent expressionFitsOnRestOfLine shortExpr longExpr diff --git a/src/Fantomas/Context.fs b/src/Fantomas/Context.fs index 99bcf333c0..37e281da65 100644 --- a/src/Fantomas/Context.fs +++ b/src/Fantomas/Context.fs @@ -557,6 +557,24 @@ let internal coli f' (c: seq<'T>) f (ctx: Context) = st +/// Similar to coli, and supply index as well to f' +let internal colii f' (c: seq<'T>) f (ctx: Context) = + let mutable tryPick = true + let mutable st = ctx + let mutable i = 0 + let e = c.GetEnumerator() + + while (e.MoveNext()) do + if tryPick then + tryPick <- false + else + st <- f' i st + + st <- f i e.Current st + i <- i + 1 + + st + /// Process collection - keeps context through the whole processing /// calls f for every element in sequence and f' between every two elements /// as a separator. This is a variant that works on typed collections.