diff --git a/crates/ruff_python_formatter/src/context.rs b/crates/ruff_python_formatter/src/context.rs index 3d516a4f74..36827dd6de 100644 --- a/crates/ruff_python_formatter/src/context.rs +++ b/crates/ruff_python_formatter/src/context.rs @@ -3,23 +3,21 @@ use std::rc::Rc; use ruff_formatter::{FormatContext, SimpleFormatOptions}; use ruff_python_ast::source_code::Locator; -pub struct ASTFormatContext<'a> { +pub struct ASTFormatContext { options: SimpleFormatOptions, contents: Rc, - locator: Locator<'a>, } -impl<'a> ASTFormatContext<'a> { - pub fn new(options: SimpleFormatOptions, locator: Locator<'a>) -> Self { +impl ASTFormatContext { + pub fn new(options: SimpleFormatOptions, contents: &str) -> Self { Self { options, - contents: Rc::from(locator.contents()), - locator, + contents: Rc::from(contents), } } } -impl FormatContext for ASTFormatContext<'_> { +impl FormatContext for ASTFormatContext { type Options = SimpleFormatOptions; fn options(&self) -> &Self::Options { @@ -27,12 +25,12 @@ impl FormatContext for ASTFormatContext<'_> { } } -impl<'a> ASTFormatContext<'a> { - pub fn contents(&'a self) -> Rc { +impl ASTFormatContext { + pub fn contents(&self) -> Rc { self.contents.clone() } - pub fn locator(&'a self) -> &'a Locator { - &self.locator + pub fn locator(&self) -> Locator { + Locator::new(&self.contents) } } diff --git a/crates/ruff_python_formatter/src/format/alias.rs b/crates/ruff_python_formatter/src/format/alias.rs index 5a11247a55..1feae1610e 100644 --- a/crates/ruff_python_formatter/src/format/alias.rs +++ b/crates/ruff_python_formatter/src/format/alias.rs @@ -11,7 +11,7 @@ pub struct FormatAlias<'a> { item: &'a Alias, } -impl AsFormat> for Alias { +impl AsFormat for Alias { type Format<'a> = FormatAlias<'a>; fn format(&self) -> Self::Format<'_> { @@ -19,7 +19,7 @@ impl AsFormat> for Alias { } } -impl Format> for FormatAlias<'_> { +impl Format for FormatAlias<'_> { fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { let alias = self.item; diff --git a/crates/ruff_python_formatter/src/format/arg.rs b/crates/ruff_python_formatter/src/format/arg.rs index c4609901fe..72ab232c5c 100644 --- a/crates/ruff_python_formatter/src/format/arg.rs +++ b/crates/ruff_python_formatter/src/format/arg.rs @@ -11,7 +11,7 @@ pub struct FormatArg<'a> { item: &'a Arg, } -impl AsFormat> for Arg { +impl AsFormat for Arg { type Format<'a> = FormatArg<'a>; fn format(&self) -> Self::Format<'_> { @@ -19,7 +19,7 @@ impl AsFormat> for Arg { } } -impl Format> for FormatArg<'_> { +impl Format for FormatArg<'_> { fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { let arg = self.item; diff --git a/crates/ruff_python_formatter/src/format/arguments.rs b/crates/ruff_python_formatter/src/format/arguments.rs index d2a83f7d88..a49220fb0c 100644 --- a/crates/ruff_python_formatter/src/format/arguments.rs +++ b/crates/ruff_python_formatter/src/format/arguments.rs @@ -9,7 +9,7 @@ pub struct FormatArguments<'a> { item: &'a Arguments, } -impl AsFormat> for Arguments { +impl AsFormat for Arguments { type Format<'a> = FormatArguments<'a>; fn format(&self) -> Self::Format<'_> { @@ -17,8 +17,8 @@ impl AsFormat> for Arguments { } } -impl Format> for FormatArguments<'_> { - fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { +impl Format for FormatArguments<'_> { + fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { let args = self.item; let mut first = true; diff --git a/crates/ruff_python_formatter/src/format/bool_op.rs b/crates/ruff_python_formatter/src/format/bool_op.rs index eb705f0d47..e148c9c59c 100644 --- a/crates/ruff_python_formatter/src/format/bool_op.rs +++ b/crates/ruff_python_formatter/src/format/bool_op.rs @@ -10,7 +10,7 @@ pub struct FormatBoolOp<'a> { item: &'a BoolOp, } -impl AsFormat> for BoolOp { +impl AsFormat for BoolOp { type Format<'a> = FormatBoolOp<'a>; fn format(&self) -> Self::Format<'_> { @@ -18,7 +18,7 @@ impl AsFormat> for BoolOp { } } -impl Format> for FormatBoolOp<'_> { +impl Format for FormatBoolOp<'_> { fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { let bool_op = self.item; write!(f, [leading_comments(bool_op)])?; diff --git a/crates/ruff_python_formatter/src/format/builders.rs b/crates/ruff_python_formatter/src/format/builders.rs index 31b156c109..d5bb00927d 100644 --- a/crates/ruff_python_formatter/src/format/builders.rs +++ b/crates/ruff_python_formatter/src/format/builders.rs @@ -12,8 +12,8 @@ pub struct Block<'a> { body: &'a Body, } -impl Format> for Block<'_> { - fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { +impl Format for Block<'_> { + fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { for (i, stmt) in self.body.node.iter().enumerate() { if i > 0 { write!(f, [hard_line_break()])?; @@ -49,8 +49,8 @@ pub struct Statements<'a> { suite: &'a [Stmt], } -impl Format> for Statements<'_> { - fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { +impl Format for Statements<'_> { + fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { for (i, stmt) in self.suite.iter().enumerate() { if i > 0 { write!(f, [hard_line_break()])?; @@ -70,8 +70,8 @@ pub struct Literal { range: TextRange, } -impl Format> for Literal { - fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { +impl Format for Literal { + fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { let text = f.context().contents(); f.write_element(FormatElement::StaticTextSlice { diff --git a/crates/ruff_python_formatter/src/format/cmp_op.rs b/crates/ruff_python_formatter/src/format/cmp_op.rs index fe90676fc4..da66c361c2 100644 --- a/crates/ruff_python_formatter/src/format/cmp_op.rs +++ b/crates/ruff_python_formatter/src/format/cmp_op.rs @@ -10,7 +10,7 @@ pub struct FormatCmpOp<'a> { item: &'a CmpOp, } -impl AsFormat> for CmpOp { +impl AsFormat for CmpOp { type Format<'a> = FormatCmpOp<'a>; fn format(&self) -> Self::Format<'_> { @@ -18,7 +18,7 @@ impl AsFormat> for CmpOp { } } -impl Format> for FormatCmpOp<'_> { +impl Format for FormatCmpOp<'_> { fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { let cmp_op = self.item; write!(f, [leading_comments(cmp_op)])?; diff --git a/crates/ruff_python_formatter/src/format/comments.rs b/crates/ruff_python_formatter/src/format/comments.rs index c21a1fd8d2..4fc40ac507 100644 --- a/crates/ruff_python_formatter/src/format/comments.rs +++ b/crates/ruff_python_formatter/src/format/comments.rs @@ -11,8 +11,8 @@ pub struct LeadingComments<'a, T> { item: &'a Attributed, } -impl Format> for LeadingComments<'_, T> { - fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { +impl Format for LeadingComments<'_, T> { + fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { for trivia in &self.item.trivia { if trivia.relationship.is_leading() { match trivia.kind { @@ -40,8 +40,8 @@ pub struct TrailingComments<'a, T> { item: &'a Attributed, } -impl Format> for TrailingComments<'_, T> { - fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { +impl Format for TrailingComments<'_, T> { + fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { for trivia in &self.item.trivia { if trivia.relationship.is_trailing() { match trivia.kind { @@ -69,8 +69,8 @@ pub struct EndOfLineComments<'a, T> { item: &'a Attributed, } -impl Format> for EndOfLineComments<'_, T> { - fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { +impl Format for EndOfLineComments<'_, T> { + fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { let mut first = true; for range in self .item @@ -97,8 +97,8 @@ pub struct DanglingComments<'a, T> { item: &'a Attributed, } -impl Format> for DanglingComments<'_, T> { - fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { +impl Format for DanglingComments<'_, T> { + fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { for trivia in &self.item.trivia { if trivia.relationship.is_dangling() { if let TriviaKind::OwnLineComment(range) = trivia.kind { diff --git a/crates/ruff_python_formatter/src/format/comprehension.rs b/crates/ruff_python_formatter/src/format/comprehension.rs index ab02bd3d3e..449aeba73a 100644 --- a/crates/ruff_python_formatter/src/format/comprehension.rs +++ b/crates/ruff_python_formatter/src/format/comprehension.rs @@ -9,7 +9,7 @@ pub struct FormatComprehension<'a> { item: &'a Comprehension, } -impl AsFormat> for Comprehension { +impl AsFormat for Comprehension { type Format<'a> = FormatComprehension<'a>; fn format(&self) -> Self::Format<'_> { @@ -17,8 +17,8 @@ impl AsFormat> for Comprehension { } } -impl Format> for FormatComprehension<'_> { - fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { +impl Format for FormatComprehension<'_> { + fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { let comprehension = self.item; write!(f, [soft_line_break_or_space()])?; diff --git a/crates/ruff_python_formatter/src/format/excepthandler.rs b/crates/ruff_python_formatter/src/format/excepthandler.rs index 898ab100be..106ee54ff4 100644 --- a/crates/ruff_python_formatter/src/format/excepthandler.rs +++ b/crates/ruff_python_formatter/src/format/excepthandler.rs @@ -12,7 +12,7 @@ pub struct FormatExcepthandler<'a> { item: &'a Excepthandler, } -impl AsFormat> for Excepthandler { +impl AsFormat for Excepthandler { type Format<'a> = FormatExcepthandler<'a>; fn format(&self) -> Self::Format<'_> { @@ -20,7 +20,7 @@ impl AsFormat> for Excepthandler { } } -impl Format> for FormatExcepthandler<'_> { +impl Format for FormatExcepthandler<'_> { fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { let excepthandler = self.item; let ExcepthandlerKind::ExceptHandler { type_, name, body } = &excepthandler.node; diff --git a/crates/ruff_python_formatter/src/format/expr.rs b/crates/ruff_python_formatter/src/format/expr.rs index 80f82cc15a..51bfbc469c 100644 --- a/crates/ruff_python_formatter/src/format/expr.rs +++ b/crates/ruff_python_formatter/src/format/expr.rs @@ -24,7 +24,7 @@ pub struct FormatExpr<'a> { } fn format_starred( - f: &mut Formatter>, + f: &mut Formatter, expr: &Expr, value: &Expr, ) -> FormatResult<()> { @@ -33,18 +33,14 @@ fn format_starred( Ok(()) } -fn format_name( - f: &mut Formatter>, - expr: &Expr, - _id: &str, -) -> FormatResult<()> { +fn format_name(f: &mut Formatter, expr: &Expr, _id: &str) -> FormatResult<()> { write!(f, [literal(expr.range())])?; write!(f, [end_of_line_comments(expr)])?; Ok(()) } fn format_subscript( - f: &mut Formatter>, + f: &mut Formatter, expr: &Expr, value: &Expr, slice: &Expr, @@ -76,7 +72,7 @@ fn format_subscript( } fn format_tuple( - f: &mut Formatter>, + f: &mut Formatter, expr: &Expr, elts: &[Expr], ) -> FormatResult<()> { @@ -97,7 +93,7 @@ fn format_tuple( write!( f, [group(&format_with( - |f: &mut Formatter>| { + |f: &mut Formatter| { if expr.parentheses.is_if_expanded() { write!(f, [if_group_breaks(&text("("))])?; } @@ -108,7 +104,7 @@ fn format_tuple( write!( f, [soft_block_indent(&format_with( - |f: &mut Formatter>| { + |f: &mut Formatter| { let magic_trailing_comma = expr .trivia .iter() @@ -164,7 +160,7 @@ fn format_tuple( } fn format_slice( - f: &mut Formatter>, + f: &mut Formatter, expr: &Expr, lower: &SliceIndex, upper: &SliceIndex, @@ -247,7 +243,7 @@ fn format_slice( } fn format_formatted_value( - f: &mut Formatter>, + f: &mut Formatter, expr: &Expr, value: &Expr, _conversion: usize, @@ -263,7 +259,7 @@ fn format_formatted_value( } fn format_list( - f: &mut Formatter>, + f: &mut Formatter, expr: &Expr, elts: &[Expr], ) -> FormatResult<()> { @@ -294,11 +290,7 @@ fn format_list( Ok(()) } -fn format_set( - f: &mut Formatter>, - expr: &Expr, - elts: &[Expr], -) -> FormatResult<()> { +fn format_set(f: &mut Formatter, expr: &Expr, elts: &[Expr]) -> FormatResult<()> { if elts.is_empty() { write!(f, [text("set()")])?; Ok(()) @@ -333,7 +325,7 @@ fn format_set( } fn format_call( - f: &mut Formatter>, + f: &mut Formatter, expr: &Expr, func: &Expr, args: &[Expr], @@ -396,7 +388,7 @@ fn format_call( } fn format_list_comp( - f: &mut Formatter>, + f: &mut Formatter, expr: &Expr, elt: &Expr, generators: &[Comprehension], @@ -417,7 +409,7 @@ fn format_list_comp( } fn format_set_comp( - f: &mut Formatter>, + f: &mut Formatter, expr: &Expr, elt: &Expr, generators: &[Comprehension], @@ -438,7 +430,7 @@ fn format_set_comp( } fn format_dict_comp( - f: &mut Formatter>, + f: &mut Formatter, expr: &Expr, key: &Expr, value: &Expr, @@ -463,7 +455,7 @@ fn format_dict_comp( } fn format_generator_exp( - f: &mut Formatter>, + f: &mut Formatter, expr: &Expr, elt: &Expr, generators: &[Comprehension], @@ -482,7 +474,7 @@ fn format_generator_exp( } fn format_await( - f: &mut Formatter>, + f: &mut Formatter, expr: &Expr, value: &Expr, ) -> FormatResult<()> { @@ -504,7 +496,7 @@ fn format_await( } fn format_yield( - f: &mut Formatter>, + f: &mut Formatter, expr: &Expr, value: Option<&Expr>, ) -> FormatResult<()> { @@ -528,7 +520,7 @@ fn format_yield( } fn format_yield_from( - f: &mut Formatter>, + f: &mut Formatter, expr: &Expr, value: &Expr, ) -> FormatResult<()> { @@ -558,7 +550,7 @@ fn format_yield_from( } fn format_compare( - f: &mut Formatter>, + f: &mut Formatter, expr: &Expr, left: &Expr, ops: &[CmpOp], @@ -578,7 +570,7 @@ fn format_compare( } fn format_joined_str( - f: &mut Formatter>, + f: &mut Formatter, expr: &Expr, _values: &[Expr], ) -> FormatResult<()> { @@ -588,7 +580,7 @@ fn format_joined_str( } fn format_constant( - f: &mut Formatter>, + f: &mut Formatter, expr: &Expr, constant: &Constant, _kind: Option<&str>, @@ -615,7 +607,7 @@ fn format_constant( } fn format_dict( - f: &mut Formatter>, + f: &mut Formatter, expr: &Expr, keys: &[Option], values: &[Expr], @@ -677,7 +669,7 @@ fn format_dict( } fn format_attribute( - f: &mut Formatter>, + f: &mut Formatter, expr: &Expr, value: &Expr, attr: &str, @@ -690,7 +682,7 @@ fn format_attribute( } fn format_named_expr( - f: &mut Formatter>, + f: &mut Formatter, expr: &Expr, target: &Expr, value: &Expr, @@ -704,7 +696,7 @@ fn format_named_expr( } fn format_bool_op( - f: &mut Formatter>, + f: &mut Formatter, expr: &Expr, ops: &[BoolOp], values: &[Expr], @@ -721,7 +713,7 @@ fn format_bool_op( } fn format_bin_op( - f: &mut Formatter>, + f: &mut Formatter, expr: &Expr, left: &Expr, op: &Operator, @@ -744,7 +736,7 @@ fn format_bin_op( } fn format_unary_op( - f: &mut Formatter>, + f: &mut Formatter, expr: &Expr, op: &UnaryOp, operand: &Expr, @@ -773,7 +765,7 @@ fn format_unary_op( } fn format_lambda( - f: &mut Formatter>, + f: &mut Formatter, expr: &Expr, args: &Arguments, body: &Expr, @@ -791,7 +783,7 @@ fn format_lambda( } fn format_if_exp( - f: &mut Formatter>, + f: &mut Formatter, expr: &Expr, test: &Expr, body: &Expr, @@ -809,8 +801,8 @@ fn format_if_exp( Ok(()) } -impl Format> for FormatExpr<'_> { - fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { +impl Format for FormatExpr<'_> { + fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { if self.item.parentheses.is_always() { write!(f, [text("(")])?; } @@ -894,7 +886,7 @@ impl Format> for FormatExpr<'_> { } } -impl AsFormat> for Expr { +impl AsFormat for Expr { type Format<'a> = FormatExpr<'a>; fn format(&self) -> Self::Format<'_> { diff --git a/crates/ruff_python_formatter/src/format/keyword.rs b/crates/ruff_python_formatter/src/format/keyword.rs index 0aa533bdfc..5c34acf072 100644 --- a/crates/ruff_python_formatter/src/format/keyword.rs +++ b/crates/ruff_python_formatter/src/format/keyword.rs @@ -11,7 +11,7 @@ pub struct FormatKeyword<'a> { item: &'a Keyword, } -impl AsFormat> for Keyword { +impl AsFormat for Keyword { type Format<'a> = FormatKeyword<'a>; fn format(&self) -> Self::Format<'_> { @@ -19,7 +19,7 @@ impl AsFormat> for Keyword { } } -impl Format> for FormatKeyword<'_> { +impl Format for FormatKeyword<'_> { fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { let keyword = self.item; diff --git a/crates/ruff_python_formatter/src/format/match_case.rs b/crates/ruff_python_formatter/src/format/match_case.rs index 6161e0f764..858d62302c 100644 --- a/crates/ruff_python_formatter/src/format/match_case.rs +++ b/crates/ruff_python_formatter/src/format/match_case.rs @@ -11,7 +11,7 @@ pub struct FormatMatchCase<'a> { item: &'a MatchCase, } -impl AsFormat> for MatchCase { +impl AsFormat for MatchCase { type Format<'a> = FormatMatchCase<'a>; fn format(&self) -> Self::Format<'_> { @@ -19,7 +19,7 @@ impl AsFormat> for MatchCase { } } -impl Format> for FormatMatchCase<'_> { +impl Format for FormatMatchCase<'_> { fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { let MatchCase { pattern, diff --git a/crates/ruff_python_formatter/src/format/numbers.rs b/crates/ruff_python_formatter/src/format/numbers.rs index 84066e62a3..86f3b37c84 100644 --- a/crates/ruff_python_formatter/src/format/numbers.rs +++ b/crates/ruff_python_formatter/src/format/numbers.rs @@ -12,8 +12,8 @@ struct FloatAtom { range: TextRange, } -impl Format> for FloatAtom { - fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { +impl Format for FloatAtom { + fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { let contents = f.context().contents(); let content = &contents[self.range]; @@ -68,8 +68,8 @@ pub struct FloatLiteral { range: TextRange, } -impl Format> for FloatLiteral { - fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { +impl Format for FloatLiteral { + fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { let contents = f.context().contents(); let content = &contents[self.range]; @@ -118,8 +118,8 @@ pub struct IntLiteral { range: TextRange, } -impl Format> for IntLiteral { - fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { +impl Format for IntLiteral { + fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { let contents = f.context().contents(); for prefix in ["0b", "0B", "0o", "0O", "0x", "0X"] { @@ -165,8 +165,8 @@ pub struct ComplexLiteral { range: TextRange, } -impl Format> for ComplexLiteral { - fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { +impl Format for ComplexLiteral { + fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { let contents = f.context().contents(); let content = &contents[self.range]; diff --git a/crates/ruff_python_formatter/src/format/operator.rs b/crates/ruff_python_formatter/src/format/operator.rs index 2ebfad9093..baede7c6cc 100644 --- a/crates/ruff_python_formatter/src/format/operator.rs +++ b/crates/ruff_python_formatter/src/format/operator.rs @@ -10,7 +10,7 @@ pub struct FormatOperator<'a> { item: &'a Operator, } -impl AsFormat> for Operator { +impl AsFormat for Operator { type Format<'a> = FormatOperator<'a>; fn format(&self) -> Self::Format<'_> { @@ -18,8 +18,8 @@ impl AsFormat> for Operator { } } -impl Format> for FormatOperator<'_> { - fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { +impl Format for FormatOperator<'_> { + fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { let operator = self.item; write!(f, [leading_comments(operator)])?; write!( diff --git a/crates/ruff_python_formatter/src/format/pattern.rs b/crates/ruff_python_formatter/src/format/pattern.rs index f17921bbbf..99051ba8d9 100644 --- a/crates/ruff_python_formatter/src/format/pattern.rs +++ b/crates/ruff_python_formatter/src/format/pattern.rs @@ -12,7 +12,7 @@ pub struct FormatPattern<'a> { item: &'a Pattern, } -impl AsFormat> for Pattern { +impl AsFormat for Pattern { type Format<'a> = FormatPattern<'a>; fn format(&self) -> Self::Format<'_> { @@ -20,7 +20,7 @@ impl AsFormat> for Pattern { } } -impl Format> for FormatPattern<'_> { +impl Format for FormatPattern<'_> { fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { let pattern = self.item; diff --git a/crates/ruff_python_formatter/src/format/stmt.rs b/crates/ruff_python_formatter/src/format/stmt.rs index b3f6fdb655..5eb7036d80 100644 --- a/crates/ruff_python_formatter/src/format/stmt.rs +++ b/crates/ruff_python_formatter/src/format/stmt.rs @@ -14,26 +14,26 @@ use crate::format::comments::{end_of_line_comments, leading_comments, trailing_c use crate::format::helpers::is_self_closing; use crate::shared_traits::AsFormat; -fn format_break(f: &mut Formatter>, stmt: &Stmt) -> FormatResult<()> { +fn format_break(f: &mut Formatter, stmt: &Stmt) -> FormatResult<()> { write!(f, [text("break")])?; write!(f, [end_of_line_comments(stmt)])?; Ok(()) } -fn format_pass(f: &mut Formatter>, stmt: &Stmt) -> FormatResult<()> { +fn format_pass(f: &mut Formatter, stmt: &Stmt) -> FormatResult<()> { write!(f, [text("pass")])?; write!(f, [end_of_line_comments(stmt)])?; Ok(()) } -fn format_continue(f: &mut Formatter>, stmt: &Stmt) -> FormatResult<()> { +fn format_continue(f: &mut Formatter, stmt: &Stmt) -> FormatResult<()> { write!(f, [text("continue")])?; write!(f, [end_of_line_comments(stmt)])?; Ok(()) } fn format_global( - f: &mut Formatter>, + f: &mut Formatter, stmt: &Stmt, names: &[String], ) -> FormatResult<()> { @@ -46,7 +46,7 @@ fn format_global( } fn format_nonlocal( - f: &mut Formatter>, + f: &mut Formatter, stmt: &Stmt, names: &[String], ) -> FormatResult<()> { @@ -59,7 +59,7 @@ fn format_nonlocal( } fn format_delete( - f: &mut Formatter>, + f: &mut Formatter, stmt: &Stmt, targets: &[Expr], ) -> FormatResult<()> { @@ -97,7 +97,7 @@ fn format_delete( } fn format_class_def( - f: &mut Formatter>, + f: &mut Formatter, name: &str, bases: &[Expr], keywords: &[Keyword], @@ -157,7 +157,7 @@ fn format_class_def( } fn format_func_def( - f: &mut Formatter>, + f: &mut Formatter, stmt: &Stmt, name: &str, args: &Arguments, @@ -204,7 +204,7 @@ fn format_func_def( } fn format_assign( - f: &mut Formatter>, + f: &mut Formatter, stmt: &Stmt, targets: &[Expr], value: &Expr, @@ -236,7 +236,7 @@ fn format_assign( } fn format_aug_assign( - f: &mut Formatter>, + f: &mut Formatter, stmt: &Stmt, target: &Expr, op: &Operator, @@ -264,7 +264,7 @@ fn format_aug_assign( } fn format_ann_assign( - f: &mut Formatter>, + f: &mut Formatter, stmt: &Stmt, target: &Expr, annotation: &Expr, @@ -301,7 +301,7 @@ fn format_ann_assign( } fn format_for( - f: &mut Formatter>, + f: &mut Formatter, stmt: &Stmt, target: &Expr, iter: &Expr, @@ -342,7 +342,7 @@ fn format_for( } fn format_while( - f: &mut Formatter>, + f: &mut Formatter, stmt: &Stmt, test: &Expr, body: &Body, @@ -383,7 +383,7 @@ fn format_while( } fn format_if( - f: &mut Formatter>, + f: &mut Formatter, test: &Expr, body: &Body, orelse: Option<&Body>, @@ -449,7 +449,7 @@ fn format_if( } fn format_match( - f: &mut Formatter>, + f: &mut Formatter, stmt: &Stmt, subject: &Expr, cases: &[MatchCase], @@ -471,7 +471,7 @@ fn format_match( } fn format_raise( - f: &mut Formatter>, + f: &mut Formatter, stmt: &Stmt, exc: Option<&Expr>, cause: Option<&Expr>, @@ -487,7 +487,7 @@ fn format_raise( } fn format_return( - f: &mut Formatter>, + f: &mut Formatter, stmt: &Stmt, value: Option<&Expr>, ) -> FormatResult<()> { @@ -502,7 +502,7 @@ fn format_return( } fn format_try( - f: &mut Formatter>, + f: &mut Formatter, stmt: &Stmt, body: &Body, handlers: &[Excepthandler], @@ -534,7 +534,7 @@ fn format_try( } fn format_try_star( - f: &mut Formatter>, + f: &mut Formatter, stmt: &Stmt, body: &Body, handlers: &[Excepthandler], @@ -577,7 +577,7 @@ fn format_try_star( } fn format_assert( - f: &mut Formatter>, + f: &mut Formatter, stmt: &Stmt, test: &Expr, msg: Option<&Expr>, @@ -609,7 +609,7 @@ fn format_assert( } fn format_import( - f: &mut Formatter>, + f: &mut Formatter, stmt: &Stmt, names: &[Alias], ) -> FormatResult<()> { @@ -638,7 +638,7 @@ fn format_import( } fn format_import_from( - f: &mut Formatter>, + f: &mut Formatter, stmt: &Stmt, module: Option<&str>, names: &[Alias], @@ -693,11 +693,7 @@ fn format_import_from( Ok(()) } -fn format_expr( - f: &mut Formatter>, - stmt: &Stmt, - expr: &Expr, -) -> FormatResult<()> { +fn format_expr(f: &mut Formatter, stmt: &Stmt, expr: &Expr) -> FormatResult<()> { if stmt.parentheses.is_always() { write!( f, @@ -726,7 +722,7 @@ fn format_expr( } fn format_with_( - f: &mut Formatter>, + f: &mut Formatter, stmt: &Stmt, items: &[Withitem], body: &Body, @@ -768,8 +764,8 @@ pub struct FormatStmt<'a> { item: &'a Stmt, } -impl Format> for FormatStmt<'_> { - fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { +impl Format for FormatStmt<'_> { + fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { write!(f, [leading_comments(self.item)])?; match &self.item.node { @@ -951,7 +947,7 @@ impl Format> for FormatStmt<'_> { } } -impl AsFormat> for Stmt { +impl AsFormat for Stmt { type Format<'a> = FormatStmt<'a>; fn format(&self) -> Self::Format<'_> { diff --git a/crates/ruff_python_formatter/src/format/strings.rs b/crates/ruff_python_formatter/src/format/strings.rs index 299ee23ade..1b9be52c31 100644 --- a/crates/ruff_python_formatter/src/format/strings.rs +++ b/crates/ruff_python_formatter/src/format/strings.rs @@ -13,8 +13,8 @@ pub struct StringLiteralPart { range: TextRange, } -impl Format> for StringLiteralPart { - fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { +impl Format for StringLiteralPart { + fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { let contents = f.context().contents(); // Extract leading and trailing quotes. @@ -119,8 +119,8 @@ pub struct StringLiteral<'a> { expr: &'a Expr, } -impl Format> for StringLiteral<'_> { - fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { +impl Format for StringLiteral<'_> { + fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { let expr = self.expr; // TODO(charlie): This tokenization needs to happen earlier, so that we can attach diff --git a/crates/ruff_python_formatter/src/format/unary_op.rs b/crates/ruff_python_formatter/src/format/unary_op.rs index 5e51d251c2..39a16f52b5 100644 --- a/crates/ruff_python_formatter/src/format/unary_op.rs +++ b/crates/ruff_python_formatter/src/format/unary_op.rs @@ -9,7 +9,7 @@ pub struct FormatUnaryOp<'a> { item: &'a UnaryOp, } -impl AsFormat> for UnaryOp { +impl AsFormat for UnaryOp { type Format<'a> = FormatUnaryOp<'a>; fn format(&self) -> Self::Format<'_> { @@ -17,7 +17,7 @@ impl AsFormat> for UnaryOp { } } -impl Format> for FormatUnaryOp<'_> { +impl Format for FormatUnaryOp<'_> { fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { let unary_op = self.item; write!( diff --git a/crates/ruff_python_formatter/src/format/withitem.rs b/crates/ruff_python_formatter/src/format/withitem.rs index 50c309f86d..0a4b2e3852 100644 --- a/crates/ruff_python_formatter/src/format/withitem.rs +++ b/crates/ruff_python_formatter/src/format/withitem.rs @@ -9,7 +9,7 @@ pub struct FormatWithitem<'a> { item: &'a Withitem, } -impl AsFormat> for Withitem { +impl AsFormat for Withitem { type Format<'a> = FormatWithitem<'a>; fn format(&self) -> Self::Format<'_> { @@ -17,7 +17,7 @@ impl AsFormat> for Withitem { } } -impl Format> for FormatWithitem<'_> { +impl Format for FormatWithitem<'_> { fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { let withitem = self.item; diff --git a/crates/ruff_python_formatter/src/lib.rs b/crates/ruff_python_formatter/src/lib.rs index 90ceed1c1a..bc579f2c2b 100644 --- a/crates/ruff_python_formatter/src/lib.rs +++ b/crates/ruff_python_formatter/src/lib.rs @@ -50,7 +50,7 @@ pub fn fmt(contents: &str) -> Result> { indent_style: IndentStyle::Space(4), line_width: 88.try_into().unwrap(), }, - locator, + locator.contents(), ), [format::builders::statements(&python_cst)] )