mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-23 04:55:09 +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
|
@ -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);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue