mirror of
https://github.com/astral-sh/ruff
synced 2026-01-22 05:51:03 -05:00
## Summary This fixes two problems discovered when trying to format the cpython repo with `cargo run --bin ruff_dev -- check-formatter-stability projects/cpython`: The first is to ignore try/except trailing comments for now since they lead to unstable formatting on the dummy. The second is to avoid dropping trailing if comments through placement: This changes the placement to keep a comment trailing an if-elif or if-elif-else to keep the comment a trailing comment on the entire if. Previously the last comment would have been lost. ```python if "first if": pass elif "first elif": pass ``` The last remaining problem in cpython so far is function signature argument separator comment placement which is its own PR on top of this. ## Test Plan I added test fixtures of minimized examples with links back to the original cpython location
18 lines
590 B
Rust
18 lines
590 B
Rust
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
|
|
use ruff_formatter::{write, Buffer, FormatResult};
|
|
use rustpython_parser::ast::StmtTry;
|
|
|
|
#[derive(Default)]
|
|
pub struct FormatStmtTry;
|
|
|
|
impl FormatNodeRule<StmtTry> for FormatStmtTry {
|
|
fn fmt_fields(&self, item: &StmtTry, f: &mut PyFormatter) -> FormatResult<()> {
|
|
write!(f, [not_yet_implemented(item)])
|
|
}
|
|
|
|
fn fmt_dangling_comments(&self, _node: &StmtTry, _f: &mut PyFormatter) -> FormatResult<()> {
|
|
// TODO(konstin): Needs node formatting or this leads to unstable formatting
|
|
Ok(())
|
|
}
|
|
}
|