Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional whitespace for LineCommentAfterSourceCode when last character is a , #2589

Closed
3 tasks
stefan-schweiger opened this issue Oct 19, 2022 · 3 comments · Fixed by #2880
Closed
3 tasks

Comments

@stefan-schweiger
Copy link
Contributor

Issue created from fantomas-online

Code

let x
    (
        a: string, // test
        b: string // test
    ) =
    print "hello"

Result

let x
    (
        a: string,  // test
        b: string // test
    ) =
    print "hello"

Problem description

When there is a , between source code and a comment the result prints an additional whitespace which doesn't get added if there is no ,.

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.

Options

Fantomas main branch at 2022-10-14T16:59:37Z - 35f093c

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?

@stefan-schweiger
Copy link
Contributor Author

fsharp_space_after_comma always adds a space between commas, but this code already added a space before the comment:

let comment = sprintf "%s%s" (if addSpace then " " else String.empty) s
writerEvent (WriteBeforeNewline comment)

Maybe there should be a different WriteEvent which tries to handle the spaces?

@nojaf
Copy link
Contributor

nojaf commented Oct 19, 2022

The bigger problem here is that the comment is assigned to the string node and not the ,.
SynPat.Tuple does not contain any information about the commas. So we cannot construct a node to anchor it.

Theoretically speaking, we could allow the following to play out:
The comment is assigned to string, after visiting that node, we print it and force it*.
This would lead to

let x
    (
        a: string // comment

However, because of the comment, we can no longer put the comma on that same line

let x
    (
        a: string // comment, `<- Leads to invalid F# code`

There is a way to take this into account, but it would lead to the comment being on the next line:

let x
    (
        a: string // comment
        , b: string

I'm not exactly sure how it would play out, so it could also be more like

let x
    (
        a: string // comment
        , 
        b: string

Adding the commas to SynPat.Tuple isn't such a wild idea. SynExpr.Tuple has them for example. But that would require a PR to the F# compiler.

(*) The thing with comments after source code is that we don't write them directly but only once we move to the next line. This is to avoid exactly the invalid scenario as mentioned above.

@nojaf
Copy link
Contributor

nojaf commented Oct 19, 2022

On a slightly more practical level, we could also check inside the update function of WriterModel if their is a trailing and leading space:

let currentLine = String.Concat(List.head m.Lines, m.WriteBeforeNewline).TrimEnd()

Something like

let currentLine =
    let head = List.head m.Lines
    if head.EndsWith(" ") && m.WriteBeforeNewline.StartsWith(" ") then
        String.Concat(head, m.WriteBeforeNewline.TrimStart())
    else
        String.Concat(head, m.WriteBeforeNewline).TrimEnd()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants