fsprojects / fantomas

FSharp source code formatter
https://fsprojects.github.io/fantomas
Other
764 stars 190 forks source link

Removes type annotation without brackets #2942

Closed artemiipatov closed 2 months ago

artemiipatov commented 1 year ago

Issue created from fantomas-online

Code

let clReducedValues, clFirstActualKeys, clSecondActualKeys: ClArray<'a> * ClArray<int> * ClArray<int> =
    reduce processor DeviceOnly resultLength clOffsets clFirstKeys clSecondKeys clValues

Result

let clReducedValues, clFirstActualKeys, clSecondActualKeys =
    reduce processor DeviceOnly resultLength clOffsets clFirstKeys clSecondKeys clValues

Problem description

Fantomas removes type annotation without brackets. But when brackets are placed around names, annotations are not removed.

Extra information

Options

Fantomas main branch at 1/1/1990

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.

nojaf commented 11 months ago

Hello, thank you for your interest in this project! Tuple bindings are covered a bit differently due to historical reasons. Which is why the return type is missing.

I believe the problem is centered around: https://github.com/fsprojects/fantomas/blob/0ce91b778f216c2d7fa8286d1aa4aa5dbf835bcc/src/Fantomas.Core/CodePrinter.fs#L2866-L2867

There we could do something like:

let genDestructedTuples =
    expressionFitsOnRestOfLine (genPat pat) (sepOpenT +> genPat pat +> sepCloseT)
    +> optSingle
        (fun (rt: BindingReturnInfoNode) ->
            genSingleTextNode rt.Colon
            +> sepSpace
            +> atCurrentColumnIndent (genType rt.Type))
        b.ReturnType

to print the return type when it is present.

I'm not sure how well this exact fix would play when the tuple is multiline. Something like

let a, //
    b, //
    c 
         : int * int * int = 1,2,3

Might be a corner case.

Are you interested in submitting a PR for this one?