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

Handle comment as last leaf item when collapsing brackets #36

Open
char101 opened this issue Oct 6, 2023 · 2 comments
Open

Handle comment as last leaf item when collapsing brackets #36

char101 opened this issue Oct 6, 2023 · 2 comments

Comments

@char101
Copy link

char101 commented Oct 6, 2023

Hi,

Having a comment as a last item break bracket collapsing, e.g.

input:

call({
    'a': 1,
})

call({
    'a': 1,
    # 'c': 2,
})

output:

call({
    'a': 1,
})

call(
    {
        'a': 1,
        # 'b': 2,
    }
)

A comment as the last leaf item is practically the same as having an ending comma.

This patch seems to fix it

diff --git a/src/cercis/utils_linegen.py b/src/cercis/utils_linegen.py
index e93c279..d3e0b23 100644
--- a/src/cercis/utils_linegen.py
+++ b/src/cercis/utils_linegen.py
@@ -68,7 +68,7 @@ def perform_collapse_nested_brackets(
                 mode=mode,
             )
             if inner_body.should_split_rhs or (
-                inner_body_leaves and inner_body_leaves[-1].type == token.COMMA
+                inner_body_leaves and inner_body_leaves[-1].type in (token.COMMA, 153)
             ):
                 # Only when the inner body itself will be split or ends with a comma,
                 # should we prefer not break immediately nested brackets.

The token type is 153. I couldn't find the name for the token in token.py.

@jsh9
Copy link
Owner

jsh9 commented Oct 18, 2023

Hi @char101 , thanks for offering a solution! And sorry for the delayed reply -- I've been very busy with other things these past few weeks.

I'll make a code change by the end of this week then.

@char101
Copy link
Author

char101 commented Oct 27, 2023

This seems to be implemented in black now.

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

No branches or pull requests

2 participants