diff --git a/crates/ruff_python_formatter/src/other/arg.rs b/crates/ruff_python_formatter/src/other/arg.rs index a0eebd3a96..3974223faa 100644 --- a/crates/ruff_python_formatter/src/other/arg.rs +++ b/crates/ruff_python_formatter/src/other/arg.rs @@ -1,7 +1,6 @@ use crate::prelude::*; use crate::FormatNodeRule; use ruff_formatter::write; -use ruff_text_size::{TextLen, TextRange}; use rustpython_parser::ast::Arg; #[derive(Default)] @@ -10,21 +9,13 @@ pub struct FormatArg; impl FormatNodeRule for FormatArg { fn fmt_fields(&self, item: &Arg, f: &mut PyFormatter) -> FormatResult<()> { let Arg { - range, + range: _, arg, annotation, type_comment: _, } = item; - write!( - f, - [ - // The name of the argument - source_text_slice( - TextRange::at(range.start(), arg.text_len()), - ContainsNewlines::No - ) - ] - )?; + + arg.format().fmt(f)?; if let Some(annotation) = annotation { write!(f, [text(":"), space(), annotation.format()])?; diff --git a/crates/ruff_python_formatter/src/other/identifier.rs b/crates/ruff_python_formatter/src/other/identifier.rs new file mode 100644 index 0000000000..40e588b491 --- /dev/null +++ b/crates/ruff_python_formatter/src/other/identifier.rs @@ -0,0 +1,28 @@ +use crate::prelude::*; +use crate::AsFormat; +use ruff_formatter::{FormatOwnedWithRule, FormatRefWithRule}; +use rustpython_parser::ast::{Identifier, Ranged}; + +pub struct FormatIdentifier; + +impl FormatRule> for FormatIdentifier { + fn fmt(&self, item: &Identifier, f: &mut PyFormatter) -> FormatResult<()> { + source_text_slice(item.range(), ContainsNewlines::No).fmt(f) + } +} + +impl<'ast> AsFormat> for Identifier { + type Format<'a> = FormatRefWithRule<'a, Identifier, FormatIdentifier, PyFormatContext<'ast>>; + + fn format(&self) -> Self::Format<'_> { + FormatRefWithRule::new(self, FormatIdentifier) + } +} + +impl<'ast> IntoFormat> for Identifier { + type Format = FormatOwnedWithRule>; + + fn into_format(self) -> Self::Format { + FormatOwnedWithRule::new(self, FormatIdentifier) + } +} diff --git a/crates/ruff_python_formatter/src/other/mod.rs b/crates/ruff_python_formatter/src/other/mod.rs index fc2530e7c2..76c0b2857d 100644 --- a/crates/ruff_python_formatter/src/other/mod.rs +++ b/crates/ruff_python_formatter/src/other/mod.rs @@ -5,6 +5,7 @@ pub(crate) mod arguments; pub(crate) mod comprehension; pub(crate) mod decorator; pub(crate) mod except_handler_except_handler; +pub(crate) mod identifier; pub(crate) mod keyword; pub(crate) mod match_case; pub(crate) mod type_ignore_type_ignore; diff --git a/crates/ruff_python_formatter/src/statement/stmt_function_def.rs b/crates/ruff_python_formatter/src/statement/stmt_function_def.rs index 560ab462c8..491429b2af 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_function_def.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_function_def.rs @@ -85,7 +85,7 @@ impl FormatRule, PyFormatContext<'_>> for FormatAnyFun [ text("def"), space(), - dynamic_text(name.as_str(), None), + name.format(), item.arguments().format(), ] )?;