Support fmt: skip on compound statements#6593
Conversation
|
Current dependencies on/for this PR:
This comment was auto-generated by Graphite. |
crates/ruff_python_formatter/tests/snapshots/black_compatibility@simple_cases__fmtskip8.py.snap
Show resolved
Hide resolved
| if SuppressionKind::has_skip_comment(trailing_definition_comments, f.context().source()) { | ||
| SuppressedClauseHeader::Class(item).fmt(f)?; | ||
| } else { | ||
| write!(f, [text("class"), space(), name.format()])?; |
There was a problem hiding this comment.
The pattern is always the same. Test if there's a trailing skip comment and, if so, call SuppressedClauseHeader. The enclosing node remains responsible for formatting the body and the trailing comments.
We could wrap this in a ClauseHeader struct but it would result in the same level of nesting because it would need to accept a Format object to write the content. It may be worth adding considering that @tjkuson needs it in #6592 to collapse ... bodies.
PR Check ResultsBenchmarkLinuxWindows |
ef86a00 to
8215d9d
Compare
e008ba2 to
b9667c7
Compare
|
@charliermarsh The PR has two commits:
I'm undecided whether 2 is over-engineer. Wdyt? |
| clause_header( | ||
| ClauseHeader::ExceptHandler(item), | ||
| dangling_comments, | ||
| &format_with(|f| { |
There was a problem hiding this comment.
The formatting of the case header has been moved into the format_with. The clause_header implementation now takes care of writing the : and the trailing colon comments.
There was a problem hiding this comment.
The ClauseHeader abstractin could potentially also be useful in placement.rs
|
Haha yeah, this is a close one, but I like what you have in the second commit, it doesn't feel over-engineered to me and is more declarative + feels very consistent with the other design patterns in the formatter. |
8215d9d to
247979d
Compare
89f9f04 to
ca611a8
Compare
247979d to
142d47b
Compare
9fc3fd4 to
6fc6c13
Compare
47988c9 to
f20577f
Compare
142d47b to
e2de436
Compare
f20577f to
c8b4a9e
Compare

Summary
This PR adds support for trailing
fmt: skipcomments suppressing the formatting of a compound statement's clause header (if,elif,try...)Test Plan
I added new test cases