Fix placement of inline parameter comments (#13379)

This commit is contained in:
Micha Reiser 2024-09-18 08:26:06 +02:00 committed by GitHub
parent c7b2e336f0
commit 6ac61d7b89
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 419 additions and 63 deletions

View file

@ -1,7 +1,6 @@
use ruff_formatter::write;
use ruff_python_ast::Parameter;
use crate::expression::parentheses::is_expression_parenthesized;
use crate::prelude::*;
use ruff_python_ast::Parameter;
#[derive(Default)]
pub struct FormatParameter;
@ -16,8 +15,22 @@ impl FormatNodeRule<Parameter> for FormatParameter {
name.format().fmt(f)?;
if let Some(annotation) = annotation {
write!(f, [token(":"), space(), annotation.format()])?;
if let Some(annotation) = annotation.as_deref() {
token(":").fmt(f)?;
if f.context().comments().has_leading(annotation)
&& !is_expression_parenthesized(
annotation.into(),
f.context().comments().ranges(),
f.context().source(),
)
{
hard_line_break().fmt(f)?;
} else {
space().fmt(f)?;
}
annotation.format().fmt(f)?;
}
Ok(())