mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 12:29:28 +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
|
@ -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) => "'''",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue