mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-22 04:25:11 +00:00
Replace Formatter<PyFormatContext<'_>>
with PyFormatter
(#6330)
This is a refactoring to use the type alias in more places. In the process, I had to fix and run generate.py. There are no functional changes.
This commit is contained in:
parent
8a5bc93fdd
commit
a48d16e025
23 changed files with 212 additions and 424 deletions
|
@ -80,7 +80,7 @@ pub(crate) struct FormatLeadingAlternateBranchComments<'a> {
|
|||
}
|
||||
|
||||
impl Format<PyFormatContext<'_>> for FormatLeadingAlternateBranchComments<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
if let Some(first_leading) = self.comments.first() {
|
||||
// Leading comments only preserves the lines after the comment but not before.
|
||||
// Insert the necessary lines.
|
||||
|
@ -121,7 +121,7 @@ pub(crate) enum FormatTrailingComments<'a> {
|
|||
}
|
||||
|
||||
impl Format<PyFormatContext<'_>> for FormatTrailingComments<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let comments = f.context().comments().clone();
|
||||
|
||||
let trailing_comments = match self {
|
||||
|
@ -243,7 +243,7 @@ struct FormatComment<'a> {
|
|||
}
|
||||
|
||||
impl Format<PyFormatContext<'_>> for FormatComment<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let slice = self.comment.slice();
|
||||
let comment_text = slice.text(SourceCode::new(f.context().source()));
|
||||
|
||||
|
|
|
@ -255,7 +255,7 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for Operator {
|
|||
}
|
||||
|
||||
impl FormatRule<Operator, PyFormatContext<'_>> for FormatOperator {
|
||||
fn fmt(&self, item: &Operator, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
|
||||
fn fmt(&self, item: &Operator, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let operator = match item {
|
||||
Operator::Add => "+",
|
||||
Operator::Sub => "-",
|
||||
|
|
|
@ -98,7 +98,7 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for BoolOp {
|
|||
}
|
||||
|
||||
impl FormatRule<BoolOp, PyFormatContext<'_>> for FormatBoolOp {
|
||||
fn fmt(&self, item: &BoolOp, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
|
||||
fn fmt(&self, item: &BoolOp, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let operator = match item {
|
||||
BoolOp::And => "and",
|
||||
BoolOp::Or => "or",
|
||||
|
|
|
@ -101,7 +101,7 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for CmpOp {
|
|||
}
|
||||
|
||||
impl FormatRule<CmpOp, PyFormatContext<'_>> for FormatCmpOp {
|
||||
fn fmt(&self, item: &CmpOp, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
|
||||
fn fmt(&self, item: &CmpOp, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let operator = match item {
|
||||
CmpOp::Eq => "==",
|
||||
CmpOp::NotEq => "!=",
|
||||
|
|
|
@ -31,7 +31,7 @@ impl Ranged for KeyValuePair<'_> {
|
|||
}
|
||||
|
||||
impl Format<PyFormatContext<'_>> for KeyValuePair<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
if let Some(key) = self.key {
|
||||
write!(
|
||||
f,
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
|
||||
use ruff_formatter::{write, Buffer, FormatResult};
|
||||
use ruff_python_ast::ExprLineMagic;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatExprLineMagic;
|
||||
|
||||
impl FormatNodeRule<ExprLineMagic> for FormatExprLineMagic {
|
||||
fn fmt_fields(&self, item: &ExprLineMagic, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
write!(f, [verbatim_text(item)])
|
||||
}
|
||||
}
|
|
@ -185,7 +185,7 @@ impl<'a> ExprSequence<'a> {
|
|||
}
|
||||
|
||||
impl Format<PyFormatContext<'_>> for ExprSequence<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
f.join_comma_separated(self.tuple.end())
|
||||
.nodes(&self.tuple.elts)
|
||||
.finish()
|
||||
|
|
|
@ -77,7 +77,7 @@ impl<'a> From<&AnyExpressionYield<'a>> for AnyNodeRef<'a> {
|
|||
}
|
||||
|
||||
impl Format<PyFormatContext<'_>> for AnyExpressionYield<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let keyword = if self.is_yield_from() {
|
||||
"yield from"
|
||||
} else {
|
||||
|
|
|
@ -31,6 +31,7 @@ pub(crate) mod expr_generator_exp;
|
|||
pub(crate) mod expr_if_exp;
|
||||
pub(crate) mod expr_joined_str;
|
||||
pub(crate) mod expr_lambda;
|
||||
pub(crate) mod expr_line_magic;
|
||||
pub(crate) mod expr_list;
|
||||
pub(crate) mod expr_list_comp;
|
||||
pub(crate) mod expr_name;
|
||||
|
@ -147,7 +148,7 @@ pub(crate) struct MaybeParenthesizeExpression<'a> {
|
|||
}
|
||||
|
||||
impl Format<PyFormatContext<'_>> for MaybeParenthesizeExpression<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let MaybeParenthesizeExpression {
|
||||
expression,
|
||||
parent,
|
||||
|
|
|
@ -17,7 +17,7 @@ impl<'a> FormatInt<'a> {
|
|||
}
|
||||
|
||||
impl Format<PyFormatContext<'_>> for FormatInt<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let range = self.constant.range();
|
||||
let content = f.context().locator().slice(range);
|
||||
|
||||
|
@ -42,7 +42,7 @@ impl<'a> FormatFloat<'a> {
|
|||
}
|
||||
|
||||
impl Format<PyFormatContext<'_>> for FormatFloat<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let range = self.constant.range();
|
||||
let content = f.context().locator().slice(range);
|
||||
|
||||
|
@ -67,7 +67,7 @@ impl<'a> FormatComplex<'a> {
|
|||
}
|
||||
|
||||
impl Format<PyFormatContext<'_>> for FormatComplex<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let range = self.constant.range();
|
||||
let content = f.context().locator().slice(range);
|
||||
|
||||
|
|
|
@ -100,7 +100,7 @@ impl<'a> FormatString<'a> {
|
|||
}
|
||||
|
||||
impl<'a> Format<PyFormatContext<'_>> for FormatString<'a> {
|
||||
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
match self.layout {
|
||||
StringLayout::Default => {
|
||||
let string_range = self.string.range();
|
||||
|
@ -134,7 +134,7 @@ impl<'a> FormatStringContinuation<'a> {
|
|||
}
|
||||
|
||||
impl Format<PyFormatContext<'_>> for FormatStringContinuation<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let comments = f.context().comments().clone();
|
||||
let locator = f.context().locator();
|
||||
let mut dangling_comments = comments.dangling_comments(self.string);
|
||||
|
@ -249,7 +249,7 @@ impl FormatStringPart {
|
|||
}
|
||||
|
||||
impl Format<PyFormatContext<'_>> for FormatStringPart {
|
||||
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let string_content = f.context().locator().slice(self.part_range);
|
||||
|
||||
let prefix = StringPrefix::parse(string_content);
|
||||
|
@ -344,7 +344,7 @@ impl StringPrefix {
|
|||
}
|
||||
|
||||
impl Format<PyFormatContext<'_>> for StringPrefix {
|
||||
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
// Retain the casing for the raw prefix:
|
||||
// https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#r-strings-and-r-strings
|
||||
if self.contains(StringPrefix::RAW) {
|
||||
|
@ -550,7 +550,7 @@ impl StringQuotes {
|
|||
}
|
||||
|
||||
impl Format<PyFormatContext<'_>> for StringQuotes {
|
||||
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let quotes = match (self.style, self.triple) {
|
||||
(QuoteStyle::Single, false) => "'",
|
||||
(QuoteStyle::Single, true) => "'''",
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -109,7 +109,7 @@ impl FormatNodeRule<Comprehension> for FormatComprehension {
|
|||
struct ExprTupleWithoutParentheses<'a>(&'a Expr);
|
||||
|
||||
impl Format<PyFormatContext<'_>> for ExprTupleWithoutParentheses<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
match self.0 {
|
||||
Expr::Tuple(expr_tuple) => expr_tuple
|
||||
.format()
|
||||
|
|
|
@ -225,7 +225,7 @@ struct CommentsAroundText<'a> {
|
|||
}
|
||||
|
||||
impl Format<PyFormatContext<'_>> for CommentsAroundText<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
if self.comments.is_empty() {
|
||||
text(self.text).fmt(f)
|
||||
} else {
|
||||
|
|
|
@ -20,6 +20,7 @@ pub(crate) mod stmt_global;
|
|||
pub(crate) mod stmt_if;
|
||||
pub(crate) mod stmt_import;
|
||||
pub(crate) mod stmt_import_from;
|
||||
pub(crate) mod stmt_line_magic;
|
||||
pub(crate) mod stmt_match;
|
||||
pub(crate) mod stmt_nonlocal;
|
||||
pub(crate) mod stmt_pass;
|
||||
|
|
|
@ -50,7 +50,7 @@ struct FormatTargets<'a> {
|
|||
}
|
||||
|
||||
impl Format<PyFormatContext<'_>> for FormatTargets<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
if let Some((first, rest)) = self.targets.split_first() {
|
||||
let can_omit_parentheses = has_own_parentheses(first);
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ use ruff_text_size::TextRange;
|
|||
struct ExprTupleWithoutParentheses<'a>(&'a Expr);
|
||||
|
||||
impl Format<PyFormatContext<'_>> for ExprTupleWithoutParentheses<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
match self.0 {
|
||||
Expr::Tuple(expr_tuple) => expr_tuple
|
||||
.format()
|
||||
|
@ -98,7 +98,7 @@ impl<'a> From<&AnyStatementFor<'a>> for AnyNodeRef<'a> {
|
|||
}
|
||||
|
||||
impl Format<PyFormatContext<'_>> for AnyStatementFor<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let target = self.target();
|
||||
let iter = self.iter();
|
||||
let body = self.body();
|
||||
|
|
|
@ -33,11 +33,7 @@ impl FormatNodeRule<StmtFunctionDef> for FormatStmtFunctionDef {
|
|||
pub struct FormatAnyFunctionDef;
|
||||
|
||||
impl FormatRule<AnyFunctionDefinition<'_>, PyFormatContext<'_>> for FormatAnyFunctionDef {
|
||||
fn fmt(
|
||||
&self,
|
||||
item: &AnyFunctionDefinition<'_>,
|
||||
f: &mut Formatter<PyFormatContext<'_>>,
|
||||
) -> FormatResult<()> {
|
||||
fn fmt(&self, item: &AnyFunctionDefinition<'_>, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let comments = f.context().comments().clone();
|
||||
|
||||
let dangling_comments = comments.dangling_comments(item);
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
|
||||
use ruff_formatter::{write, Buffer, FormatResult};
|
||||
use ruff_python_ast::StmtLineMagic;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatStmtLineMagic;
|
||||
|
||||
impl FormatNodeRule<StmtLineMagic> for FormatStmtLineMagic {
|
||||
fn fmt_fields(&self, item: &StmtLineMagic, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
write!(f, [verbatim_text(item)])
|
||||
}
|
||||
}
|
|
@ -100,11 +100,7 @@ impl FormatRuleWithOptions<ExceptHandler, PyFormatContext<'_>> for FormatExceptH
|
|||
}
|
||||
|
||||
impl FormatRule<ExceptHandler, PyFormatContext<'_>> for FormatExceptHandler {
|
||||
fn fmt(
|
||||
&self,
|
||||
item: &ExceptHandler,
|
||||
f: &mut Formatter<PyFormatContext<'_>>,
|
||||
) -> FormatResult<()> {
|
||||
fn fmt(&self, item: &ExceptHandler, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
match item {
|
||||
ExceptHandler::ExceptHandler(x) => {
|
||||
x.format().with_options(self.except_handler_kind).fmt(f)
|
||||
|
@ -126,7 +122,7 @@ impl<'ast> AsFormat<PyFormatContext<'ast>> for ExceptHandler {
|
|||
}
|
||||
}
|
||||
impl Format<PyFormatContext<'_>> for AnyStatementTry<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let comments_info = f.context().comments().clone();
|
||||
let mut dangling_comments = comments_info.dangling_comments(self);
|
||||
let body = self.body();
|
||||
|
|
|
@ -68,7 +68,7 @@ impl<'a> From<&AnyStatementWith<'a>> for AnyNodeRef<'a> {
|
|||
}
|
||||
|
||||
impl Format<PyFormatContext<'_>> for AnyStatementWith<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let comments = f.context().comments().clone();
|
||||
let dangling_comments = comments.dangling_comments(self);
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ pub(crate) struct FormatTypeParamsClause<'a> {
|
|||
|
||||
/// Formats a sequence of [`TypeParam`] nodes.
|
||||
impl Format<PyFormatContext<'_>> for FormatTypeParamsClause<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
if self.type_params.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue