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:
konsti 2023-08-04 10:48:58 +02:00 committed by GitHub
parent 8a5bc93fdd
commit a48d16e025
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 212 additions and 424 deletions

View file

@ -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;

View file

@ -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);

View file

@ -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();

View file

@ -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);

View file

@ -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)])
}
}

View file

@ -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();

View file

@ -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);