fantomas icon indicating copy to clipboard operation
fantomas copied to clipboard

Wrong change of comment's position

Open knocte opened this issue 3 years ago • 5 comments

Issue created from fantomas-online

Code

exception LoadedSourceNotFoundIgnoring of (*filename*) string * range

Result

exception LoadedSourceNotFoundIgnoring of string * range (*filename*)

Extra information

  • [ ] The formatted result breaks by code.
  • [ ] The formatted result gives compiler warnings.
  • [ ] I or my company would be willing to help fix this.

Options

Fantomas 4.6 branch at 11/07/2021 14:33:13 - a4d21fe91323c394fdb2a0f97e0c980ff5378eaf

Default Fantomas configuration

Did you know that you can ignore files when formatting from fantomas-tool or the FAKE targets by using a .fantomasignore file?

knocte avatar Nov 07 '21 22:11 knocte

Hello, thank you for reporting this issue. The trivia is assigned to SynUnionCase_ while the first SynField_ might be the better candidate. Are you interested in submitting a PR?

nojaf avatar Nov 08 '21 08:11 nojaf

I saw that we are calling genField inside genUnionCase when the fields are not empty on CodePrinter.fs, but I can't find why it's not generating the trivia for the field

ribeirotomas1904 avatar Feb 16 '22 11:02 ribeirotomas1904

We also have other similar cases happening here

ribeirotomas1904 avatar Feb 16 '22 11:02 ribeirotomas1904

The trivia is not assigned to SynField_ but to SynUnionCase_, the main problem is that the Ident of the exception (LoadedSourceNotFoundIgnoring ) is not picked up in ASTTransformer:

https://github.com/fsprojects/fantomas/blob/fe22e3107033dbe0dc70db05190d72919058cfd3/src/Fantomas/AstTransformer.fs#L1113-L1118

should be something like:

    and visitSynUnionCase (uc: SynUnionCase) : TriviaNodeAssigner list =
        match uc with
        | SynUnionCase (attrs, ident, uct, _, _, range) ->
            [ yield mkNode SynUnionCase_ range
              yield visitIdent ident
              yield! visitSynUnionCaseType uct
              yield! (visitSynAttributeLists attrs) ]

that will lead to the comment being assigned to the right node.

However, for this to be processed correctly in CodePrinter, you would need to extend the active pattern in SourceParser:

https://github.com/fsprojects/fantomas/blob/18eda76ce035e4592cd58399c166274d0aa8ddc6/src/Fantomas/SourceParser.fs#L31-L44

to include the range and print the trivia in CodePrinter.

This is used in a lot of places so a helper function in CodePrinter might be a good idea.

nojaf avatar Feb 16 '22 13:02 nojaf

We also have other similar cases happening here

Similar kind of bug, different solution, please open a new issue.

nojaf avatar Feb 16 '22 13:02 nojaf