fantomas icon indicating copy to clipboard operation
fantomas copied to clipboard

Type annotation ends up with many spaces, adjusted for the previous expression's length

Open jp-fournier-dev opened this issue 5 months ago • 2 comments

While applying Fantomas to our code base, I fell upon this issue where the type annotation will be filled with spaces to match up with the previous expressions length. In the example, if you remove one call of anotherOne, the formatting will adjust the length of the spacing that will be added in consequence.

Seems like we might be setting the position of the closing > in accordance to the wrong expression

There is an easy workaround. This will not materialize itself if the faulty expression is directly on the next line from the first expression, ie. :

module SomeModule =

    let someFunctionCall() = "hello"

    let anotherOne s = s 
    
    let alsoAnotherOne s = s |> ignore

    type SomeType with
        member this.Reraise() =
            (someFunctionCall()) |> anotherOne |> anotherOne |> alsoAnotherOne
            Unchecked.defaultof<_>

Issue created from fantomas-online

Code

module SomeModule =

    let someFunctionCall() = "hello"

    let anotherOne s = s 
    
    let alsoAnotherOne s = s |> ignore

    type SomeType with
        member this.Reraise() =
            (someFunctionCall()) |> anotherOne |> anotherOne |> alsoAnotherOne

            Unchecked.defaultof<_>

Result

module SomeModule =

    let someFunctionCall () = "hello"

    let anotherOne s = s

    let alsoAnotherOne s = s |> ignore

    type SomeType with
        member this.Reraise() =
            (someFunctionCall ()) |> anotherOne |> anotherOne |> alsoAnotherOne

            Unchecked.defaultof<_                                               >

Problem description

Please describe here the Fantomas problem you encountered. Check out our Contribution Guidelines.

Extra information

  • [ ] The formatted result breaks my code.
  • [ ] The formatted result gives compiler warnings.
  • [ ] I or my company would be willing to help fix this.
  • [ ] I would like a release if this problem is solved.

Options

Fantomas main branch at 2025-08-06T12:58:25Z - cbee488db82d62dcea03bbc08328dc259da6c09e

Default Fantomas configuration

Did you know that you can ignore files when formatting by using a .fantomasignore file? PS: It's unlikely that someone else will solve your specific issue, as it's something that you have a personal stake in.

jp-fournier-dev avatar Aug 11 '25 20:08 jp-fournier-dev

Hello, thank you for reporting this issue.

Something must be going wrong in:

https://github.com/fsprojects/fantomas/blob/cbee488db82d62dcea03bbc08328dc259da6c09e/src/Fantomas.Core/CodePrinter.fs#L2077-L2089

Are you interested in submitting a PR?

nojaf avatar Aug 12 '25 05:08 nojaf

I will give it a look and report back !

jp-fournier-dev avatar Aug 13 '25 13:08 jp-fournier-dev