Skip to content

Handle comment as last leaf item when collapsing brackets #36

@char101

Description

@char101

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions