Skip to content

Commit

Permalink
Respect page width for long abstract member declarations. Fixes fspro…
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Aug 14, 2020
1 parent ee6dd12 commit 9f53d49
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 11 deletions.
46 changes: 43 additions & 3 deletions src/Fantomas.Tests/InterfaceTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ type MyLogInteface() =
override x.Version() = ()
"""


[<Test>]
let ``Interface with comment after equal`` () =
formatSourceString false """
Expand All @@ -185,7 +184,6 @@ type IArgParserTemplate =
abstract Usage: string
"""


[<Test>]
let ``generic interface member should have space after name`` () =
let source = """
Expand All @@ -197,4 +195,46 @@ type IFunc<'R> =
|> fun formatted -> formatSourceString false formatted config
|> should equal """type IFunc<'R> =
abstract Invoke<'T> : unit -> 'R // without this space the code is invalid
"""
"""

[<Test>]
let ``long abstract member definition, 435`` () =
formatSourceString false """
type Test =
abstract RunJobs: folder:string * ?jobs:string * ?ctm:string * ?createDuplicate:bool * ?hold:bool * ?ignoreCriteria:bool * ?independentFlow:bool * ?orderDate:string * ?orderIntoFolder:string * ?variables:Dictionary<string, string> [] * ?waitForOrderDate:bool
-> string
override this.RunJobs(folder: string, ?jobs: string, ?ctm: string, ?createDuplicate: bool, ?hold: bool,
?ignoreCriteria: bool, ?independentFlow: bool, ?orderDate: string, ?orderIntoFolder: string,
?variables: Dictionary<string, string> [], ?waitForOrderDate: bool) =
""
""" config
|> prepend newline
|> should equal """
type Test =
abstract RunJobs: folder:string
* ?jobs:string
* ?ctm:string
* ?createDuplicate:bool
* ?hold:bool
* ?ignoreCriteria:bool
* ?independentFlow:bool
* ?orderDate:string
* ?orderIntoFolder:string
* ?variables:Dictionary<string, string> []
* ?waitForOrderDate:bool
-> string
override this.RunJobs(folder: string,
?jobs: string,
?ctm: string,
?createDuplicate: bool,
?hold: bool,
?ignoreCriteria: bool,
?independentFlow: bool,
?orderDate: string,
?orderIntoFolder: string,
?variables: Dictionary<string, string> [],
?waitForOrderDate: bool) =
""
"""
7 changes: 5 additions & 2 deletions src/Fantomas.Tests/SignatureTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -508,8 +508,11 @@ open FSharp.Compiler.SourceCodeServices
[<Sealed>]
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>
static member ParseAsync: fileName:string
* source:SourceOrigin
* parsingOptions:FSharpParsingOptions
* checker:FSharpChecker
-> Async<(ParsedInput * string list) array>
"""

[<Test>]
Expand Down
16 changes: 10 additions & 6 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2740,12 +2740,16 @@ and genTypeList astContext node =
| (TTuple ts', argInfo)::ts ->
// The '/' separator shouldn't appear here
let hasBracket = not ts.IsEmpty
let gt = col sepStar (Seq.zip argInfo (Seq.map snd ts'))
(fun (ArgInfo(ats, so, isOpt), t) ->
genOnelinerAttributes astContext ats
+> opt sepColonFixed so (if isOpt then (sprintf "?%s" >> (!-)) else (!-))
+> genType astContext hasBracket t)
gt +> ifElse ts.IsEmpty sepNone (autoNlnIfExpressionExceedsPageWidth (sepArrow +> genTypeList astContext ts))
let gt sepBefore =
col sepBefore (Seq.zip argInfo (Seq.map snd ts'))
(fun (ArgInfo(ats, so, isOpt), t) ->
genOnelinerAttributes astContext ats
+> opt sepColonFixed so (if isOpt then (sprintf "?%s" >> (!-)) else (!-))
+> genType astContext hasBracket t)

let shortExpr = gt sepStar +> ifElse ts.IsEmpty sepNone (sepArrow +> genTypeList astContext ts)
let longExpr = gt (sepNln +> sepStarFixed) +> ifElse ts.IsEmpty sepNone (sepNln +> sepArrowFixed +> genTypeList astContext ts)
atCurrentColumn (expressionFitsOnRestOfLine shortExpr longExpr)

| (t, _)::ts ->
let gt = genType astContext false t
Expand Down
2 changes: 2 additions & 0 deletions src/Fantomas/Context.fs
Original file line number Diff line number Diff line change
Expand Up @@ -460,9 +460,11 @@ let internal sepNlnUnlessLastEventIsNewline (ctx: Context) =
else sepNln ctx

let internal sepStar = !- " * "
let internal sepStarFixed = !- "* "
let internal sepEq = !- " ="
let internal sepEqFixed = !- "="
let internal sepArrow = !- " -> "
let internal sepArrowFixed = !- "-> "
let internal sepWild = !- "_"

let internal sepBar = !- "| "
Expand Down

0 comments on commit 9f53d49

Please sign in to comment.