Format StmtAugAssign (#5655)

## Summary

Format statements such as `tree_depth += 1`. This is a statement that
does not allow any line breaks, the only thing to be mindful of is to
parenthesize the assigned expression

Jaccard index on django: 0.915 -> 0.918

## Test Plan

black tests, and two new tests, a basic one and one that ensures that
the child gets parentheses. I ran the django stability check.
This commit is contained in:
konsti 2023-07-11 09:06:23 +02:00 committed by GitHub
parent 15c7b6bcf7
commit b7794f855b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 56 additions and 27 deletions

View file

@ -1,4 +1,6 @@
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use crate::expression::parentheses::Parenthesize;
use crate::{AsFormat, FormatNodeRule, PyFormatter};
use ruff_formatter::prelude::{space, text};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::StmtAugAssign;
@ -7,6 +9,22 @@ pub struct FormatStmtAugAssign;
impl FormatNodeRule<StmtAugAssign> for FormatStmtAugAssign {
fn fmt_fields(&self, item: &StmtAugAssign, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [not_yet_implemented(item)])
let StmtAugAssign {
target,
op,
value,
range: _,
} = item;
write!(
f,
[
target.format(),
space(),
op.format(),
text("="),
space(),
value.format().with_options(Parenthesize::IfBreaks)
]
)
}
}