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: Trailing end of line comments in binary like expressions #7004

Closed
MichaReiser opened this issue Aug 30, 2023 · 1 comment · Fixed by #7269
Closed

Formatter: Trailing end of line comments in binary like expressions #7004

MichaReiser opened this issue Aug 30, 2023 · 1 comment · Fixed by #7269
Assignees
Labels
formatter Related to the formatter

Comments

@MichaReiser
Copy link
Member

MichaReiser commented Aug 30, 2023

# Input
if (
    (1 + 2) or # test
    (3 + 4) or # other
    (4 + 5) # more
): pass


if (
    (1 and 2) + # test
    (3 and 4) + # other
    (4 and 5) # more
): pass


if (
    (1 + 2) < # test
    (3 + 4) > # other
    (4 + 5) # more
): pass

# Output
if (
    (
        1 + 2  # test
    )
    or (
        3 + 4  # other
    )
    or (4 + 5)  # more
):
    pass


if (
    (
        1 and 2  # test
    )
    + (3 and 4)  # other
    + (4 and 5)  # more
):
    pass


if (
    (
        1 + 2  # test
    )
    < (
        3 + 4  # other
    )
    > (4 + 5)  # more
):
    pass

Expected
Ruff not to expand the parenthesized right sides.

Note: Prettier handles this correctly

@MichaReiser MichaReiser added the formatter Related to the formatter label Aug 30, 2023
@MichaReiser MichaReiser added this to the Formatter: Beta milestone Aug 30, 2023
@MichaReiser MichaReiser self-assigned this Sep 8, 2023
@MichaReiser
Copy link
Member Author

MichaReiser commented Sep 8, 2023

Uff this is harder than I expected. The issue is that our comment logic doesn't distinguish between

(
  a # comment
)

(a) # comment

It always formats the comment inside of the parentheses instead of only doing so if the comment is inside the parentheses in the source. I thought I have a clever quick fix that manually moves the trailing comments out inside of FormatExpr if it is passed a ) but I now run into stability issues with function return type annotations 🙅

Another example:

(
    a + 
    # comment 
    (a + b) 
     > c
)

Gets formatted as

(
    a + 
    (
	    # comment 
		a + b
	) 
     > c
)

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

Successfully merging a pull request may close this issue.

1 participant