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

formatter: comment formatting instability in named expr (and dict, others) #5695

Closed
davidszotten opened this issue Jul 11, 2023 · 4 comments · Fixed by #6634
Closed

formatter: comment formatting instability in named expr (and dict, others) #5695

davidszotten opened this issue Jul 11, 2023 · 4 comments · Fixed by #6634
Assignees
Labels
bug Something isn't working formatter Related to the formatter

Comments

@davidszotten
Copy link
Contributor

input

if (
    # 1
    x # 2
    # 2.5
    := # 3
    # 3.5
    y # 4
):
    pass
Formatted once:
---
if (
    # 1
    x := # 2.5  # 2  # 3
    # 3.5
    y  # 4
):
    pass

---

Formatted twice:
---
if (
    # 1
    x := # 3.5  # 2.5  # 2  # 3
    y  # 4
):
    pass

i think the issue is that we use a space() around the operator (:=) and not a soft_line_break_or_space so nothing is inserting the newline before the leading ownline comment

@MichaReiser
Copy link
Member

The use of space might be intentional to avoid breaking after the operator. But it might be necessary to emit a soft line break if the right side has a leading comment

@MichaReiser MichaReiser added bug Something isn't working formatter Related to the formatter labels Jul 11, 2023
@davidszotten
Copy link
Contributor Author

yes sorry for being unclear. wasn't suggesting switching out the space, only thinking out loud why we hadn't seen this more elsewhere

@konstin konstin changed the title formatter: bug in named expr (and dict, others) formatter: comment formatting instability in named expr (and dict, others) Jul 16, 2023
@MichaReiser MichaReiser added this to the Formatter: Alpha milestone Aug 16, 2023
@charliermarsh charliermarsh self-assigned this Aug 16, 2023
@charliermarsh
Copy link
Member

I'll take this one. There are similarities to with... as... so I'll wait for that to be approved before pushing anything.

@charliermarsh
Copy link
Member

Didn't end up being that similar to with so PR here: #6634.

charliermarsh added a commit that referenced this issue Aug 18, 2023
## Summary

Attaches comments around the `:=` operator in a named expression as
dangling, and formats them manually in the `named_expr.rs` formatter.

Closes #5695.

## Test Plan

`cargo test`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working formatter Related to the formatter
Projects
None yet
3 participants