Commit Graph

930 Commits

Author SHA1 Message Date
Brent Westbrook a48dc8ed64
inline current FormatBody 2025-12-11 15:45:07 -05:00
Brent Westbrook 3dfda9f6cc
unwrap format_body 2025-12-11 15:43:00 -05:00
Brent Westbrook 00745159d1
consolidate preview checks 2025-12-11 15:34:10 -05:00
Brent Westbrook 6f25547098
delete outdated preview comments 2025-12-11 15:24:53 -05:00
Brent Westbrook 854062f838
inline format_body 2025-12-11 15:16:44 -05:00
Brent Westbrook d1b79f5400
share common code 2025-12-11 15:13:47 -05:00
Brent Westbrook 5fee7534f7
save f.context.comments() to comments 2025-12-11 15:06:44 -05:00
Brent Westbrook 0f1ea90c5c
reposition dangling comments 2025-12-11 11:43:03 -05:00
Brent Westbrook 8711b1a301
Revert leading comment changes
This reverts commit 2cb98d4cdb.
2025-12-11 11:42:49 -05:00
Brent Westbrook 2cb98d4cdb
working for Micha's exact example 2025-12-11 10:34:35 -05:00
Brent Westbrook 9d7d94c4f0
factor out FormatBody 2025-12-10 16:03:06 -05:00
Brent Westbrook 553b45e27f
move dangling comment handling back out of placement.rs
Revert "re-apply 'pass preview to handle_lambda_comment'"

This reverts commit 33fcca9c53.
2025-12-10 15:58:48 -05:00
Brent Westbrook b96cf96e8b
add a few more call tests with comments 2025-12-10 14:06:08 -05:00
Brent Westbrook 8022343bd6
maybe_parenthesize_lambda -> maybe_parenthesize_value 2025-12-10 13:43:39 -05:00
Brent Westbrook 7c94e1026c
update lambda placement docs 2025-12-10 12:37:47 -05:00
Brent Westbrook f999da0eec
move tokenizer checks into the stable branch 2025-12-10 12:23:00 -05:00
Brent Westbrook a8bebaa681
add preview function taking a PreviewMode 2025-12-10 12:19:55 -05:00
Brent Westbrook 9f9b76b035
move preview comment handling mostly into placement.rs 2025-12-10 09:38:15 -05:00
Brent Westbrook 33fcca9c53
re-apply 'pass preview to handle_lambda_comment' 2025-12-10 09:11:00 -05:00
Brent Westbrook 6e9e42d343
factor out maybe_parenthesize_lambda 2025-12-10 09:04:00 -05:00
Brent Westbrook bb053f8388
fix lambda formatting in assignments 2025-12-10 08:51:54 -05:00
Brent Westbrook 54c768989d
clean up deref 2025-12-09 17:42:25 -05:00
Brent Westbrook 829f10521f
Merge branch 'brent/fix-kwargs' into brent/indent-lambda-params 2025-12-09 17:26:25 -05:00
Brent Westbrook 2e0ee2e0a9
update lambda comment 2025-12-09 17:20:18 -05:00
Brent Westbrook acc49ac1e7
remove redundant parameters check
I believe parameters itself would be None if there were no next parameter
2025-12-09 17:17:26 -05:00
Brent Westbrook 2da4798def
update comments 2025-12-09 16:53:44 -05:00
Brent Westbrook 9d683da964
simplify a bit further 2025-12-09 16:51:55 -05:00
Brent Westbrook f639389e6a
simplify check with parameters.start() == parameter.start()
and fix some names

the start check handles both the `are_parameters_parenthesized` check (because
lambda parameters cannot be parenthesized and thus nothing can come between the
start of the parameters and the first parameter) and the comparison with
first.range() since the parameters start where the first parameter starts
2025-12-09 16:48:15 -05:00
Brent Westbrook d722155762
make comments leading on parameter_s_ 2025-12-09 16:45:26 -05:00
Brent Westbrook 2bd64095a6
revert FormatParameters changes 2025-12-09 16:35:14 -05:00
Brent Westbrook b823866155
revert preview and comment placement changes 2025-12-09 16:34:01 -05:00
Brent Westbrook fd34cd6042
update snaps 2025-12-09 15:56:20 -05:00
Brent Westbrook 65c943568c
Merge branch 'brent/fix-kwargs' into brent/indent-lambda-params 2025-12-09 15:42:05 -05:00
Brent Westbrook b0a82983af
avoid breaking when the first parameter has leading comments 2025-12-09 15:32:24 -05:00
Brent Westbrook 90f43bde84
add broken test cases
the new leading comment is causing the whole Parameters list to break. these
cases should instead format like:

```py
(
    lambda
    # comment
    *x, **y: x
)

(
    lambda
    # comment 2
    *x, **y: x
)
```

without line breaks in the parameter list
2025-12-09 15:06:48 -05:00
Brent Westbrook 4ffbd496e3
Merge branch 'main' into brent/indent-lambda-params 2025-12-09 14:34:28 -05:00
Brent Westbrook 0bec5c0362
Fix comment placement in lambda parameters (#21868)
Summary
--

This PR makes two changes to comment placement in lambda parameters.
First, we
now insert a line break if the first parameter has a leading comment:

```py
# input
(
    lambda
    * # comment 2
    x:
    x
)

# main
(
    lambda # comment 2
    *x: x
)

# this PR
(
    lambda
	# comment 2
    *x: x
)
```

Note the missing space in the output from main. This case is currently
unstable
on main. Also note that the new formatting is more consistent with our
stable
formatting in cases where the lambda has its own dangling comment:

```py
# input
(
    lambda # comment 1
    * # comment 2
    x:
    x
)

# output
(
    lambda  # comment 1
    # comment 2
    *x: x
)
```

and when a parameter without a comment precedes the split `*x`:

```py
# input
(
    lambda y,
    * # comment 2
    x:
    x
)

# output
(
    lambda y,
    # comment 2
    *x: x
)
```

This does change the stable formatting, but I think such cases are rare
(expecting zero hits in the ecosystem report), this fixes an existing
instability, and it should not change any code we've previously
formatted.

Second, this PR modifies the comment placement such that `# comment 2`
in these
outputs is still a leading comment on the parameter. This is also not
the case
on main, where it becomes a [dangling lambda
comment](https://play.ruff.rs/3b29bb7e-70e4-4365-88e0-e60fe1857a35?secondary=Comments).
This doesn't cause any
instability that I'm aware of on main, but it does cause problems when
trying to
adjust the placement of dangling lambda comments in #21385. Changing the
placement in this way should not affect any formatting here.

Test Plan
--

New lambda tests, plus existing tests covering the cases above with
multiple
comments around the parameters (see lambda.py 122-143, and 122-205 or so
more
broadly)

I also checked manually that the comments are now leading on the
parameter:

```shell
❯ cargo run --bin ruff_python_formatter -- --emit stdout --target-version 3.10 --print-comments <<EOF
(
    lambda
        # comment 2
    *x: x
)
EOF
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.15s
     Running `target/debug/ruff_python_formatter --emit stdout --target-version 3.10 --print-comments`
# Comment decoration: Range, Preceding, Following, Enclosing, Comment
21..32, None, Some((Parameters, 37..39)), (ExprLambda, 6..42), "# comment 2"
{
    Node {
        kind: Parameter,
        range: 37..39,
        source: `*x`,
    }: {
        "leading": [
            SourceComment {
                text: "# comment 2",
                position: OwnLine,
                formatted: true,
            },
        ],
        "dangling": [],
        "trailing": [],
    },
}
(
    lambda
    # comment 2
    *x: x
)
```

But I didn't see a great place to put a test like this. Is there
somewhere I can assert this comment placement since it doesn't affect
any formatting yet? Or is it okay to wait until we use this in #21385?
2025-12-09 14:07:48 -05:00
Brent Westbrook 711dd6eed4
pass preview to handle_lambda_comment 2025-12-09 10:27:45 -05:00
Brent Westbrook 21b442a4fc
accept snapshots 2025-12-09 08:53:53 -05:00
Brent Westbrook ea30464a2d
clippy 2025-12-09 08:52:39 -05:00
Brent Westbrook c7b1089631
apply patch
Co-authored-by: Micha Reiser <micha@reiser.io>
2025-12-09 08:51:59 -05:00
Brent Westbrook e8540d9b08
format new dangling comments 2025-12-08 12:56:57 -05:00
Brent Westbrook 8ede14a083
move comments within lambda parameters to dangling lambda comments 2025-12-08 12:29:37 -05:00
Brent Westbrook f20f3e0d49
fix assignment instability without parameters too 2025-12-05 16:08:37 -05:00
Brent Westbrook df42aa29b5
Reapply "try parenthesizing the body without parameters"
This reverts commit 4585a0b3e4.
2025-12-05 16:04:23 -05:00
Brent Westbrook 0710e0bc3e
fix assignment instability with dangling comments 2025-12-05 16:03:46 -05:00
Brent Westbrook 1531c94b4e
revert the last two commits, back to a stable formatting 2025-12-05 15:44:21 -05:00
Brent Westbrook 86406c0bb1
wip 2025-12-05 15:41:49 -05:00
Brent Westbrook 80852c1769
pairing 2025-12-05 15:40:47 -05:00
Brent Westbrook 4d76cd7b15
factor out parameters_have_comments 2025-12-05 14:57:54 -05:00