mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-02 22:55:08 +00:00
Make BoolOp its own located token (#3265)
This commit is contained in:
parent
470e1c1754
commit
061495a9eb
19 changed files with 355 additions and 301 deletions
38
crates/ruff_python_formatter/src/format/bool_op.rs
Normal file
38
crates/ruff_python_formatter/src/format/bool_op.rs
Normal file
|
@ -0,0 +1,38 @@
|
|||
use ruff_formatter::prelude::*;
|
||||
use ruff_formatter::write;
|
||||
|
||||
use crate::context::ASTFormatContext;
|
||||
use crate::cst::{BoolOp, BoolOpKind};
|
||||
use crate::format::comments::{end_of_line_comments, leading_comments, trailing_comments};
|
||||
use crate::shared_traits::AsFormat;
|
||||
|
||||
pub struct FormatBoolOp<'a> {
|
||||
item: &'a BoolOp,
|
||||
}
|
||||
|
||||
impl AsFormat<ASTFormatContext<'_>> for BoolOp {
|
||||
type Format<'a> = FormatBoolOp<'a>;
|
||||
|
||||
fn format(&self) -> Self::Format<'_> {
|
||||
FormatBoolOp { item: self }
|
||||
}
|
||||
}
|
||||
|
||||
impl Format<ASTFormatContext<'_>> for FormatBoolOp<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<ASTFormatContext>) -> FormatResult<()> {
|
||||
let boolop = self.item;
|
||||
|
||||
write!(f, [leading_comments(boolop)])?;
|
||||
write!(
|
||||
f,
|
||||
[text(match boolop.node {
|
||||
BoolOpKind::And => "and",
|
||||
BoolOpKind::Or => "or",
|
||||
})]
|
||||
)?;
|
||||
write!(f, [end_of_line_comments(boolop)])?;
|
||||
write!(f, [trailing_comments(boolop)])?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
use ruff_formatter::prelude::*;
|
||||
use ruff_formatter::write;
|
||||
|
||||
use crate::context::ASTFormatContext;
|
||||
use crate::cst::Boolop;
|
||||
use crate::shared_traits::AsFormat;
|
||||
|
||||
pub struct FormatBoolop<'a> {
|
||||
item: &'a Boolop,
|
||||
}
|
||||
|
||||
impl AsFormat<ASTFormatContext<'_>> for Boolop {
|
||||
type Format<'a> = FormatBoolop<'a>;
|
||||
|
||||
fn format(&self) -> Self::Format<'_> {
|
||||
FormatBoolop { item: self }
|
||||
}
|
||||
}
|
||||
|
||||
impl Format<ASTFormatContext<'_>> for FormatBoolop<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<ASTFormatContext>) -> FormatResult<()> {
|
||||
let boolop = self.item;
|
||||
write!(
|
||||
f,
|
||||
[text(match boolop {
|
||||
Boolop::And => "and",
|
||||
Boolop::Or => "or",
|
||||
})]
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
|
@ -9,7 +9,7 @@ use ruff_text_size::TextSize;
|
|||
use crate::context::ASTFormatContext;
|
||||
use crate::core::types::Range;
|
||||
use crate::cst::{
|
||||
Arguments, Boolop, Cmpop, Comprehension, Expr, ExprKind, Keyword, Operator, SliceIndex,
|
||||
Arguments, BoolOp, Cmpop, Comprehension, Expr, ExprKind, Keyword, Operator, SliceIndex,
|
||||
SliceIndexKind, Unaryop,
|
||||
};
|
||||
use crate::format::builders::literal;
|
||||
|
@ -338,38 +338,10 @@ fn format_call(
|
|||
if args.is_empty() && keywords.is_empty() {
|
||||
write!(f, [text("(")])?;
|
||||
write!(f, [text(")")])?;
|
||||
|
||||
// Format any end-of-line comments.
|
||||
let mut first = true;
|
||||
for range in expr.trivia.iter().filter_map(|trivia| {
|
||||
if trivia.relationship.is_trailing() {
|
||||
trivia.kind.end_of_line_comment()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}) {
|
||||
if std::mem::take(&mut first) {
|
||||
write!(f, [line_suffix(&text(" "))])?;
|
||||
}
|
||||
write!(f, [line_suffix(&literal(range))])?;
|
||||
}
|
||||
write!(f, [end_of_line_comments(expr)])?;
|
||||
} else {
|
||||
write!(f, [text("(")])?;
|
||||
|
||||
// Format any end-of-line comments.
|
||||
let mut first = true;
|
||||
for range in expr.trivia.iter().filter_map(|trivia| {
|
||||
if trivia.relationship.is_trailing() {
|
||||
trivia.kind.end_of_line_comment()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}) {
|
||||
if std::mem::take(&mut first) {
|
||||
write!(f, [line_suffix(&text(" "))])?;
|
||||
}
|
||||
write!(f, [line_suffix(&literal(range))])?;
|
||||
}
|
||||
write!(f, [end_of_line_comments(expr)])?;
|
||||
|
||||
let magic_trailing_comma = expr.trivia.iter().any(|c| c.kind.is_magic_trailing_comma());
|
||||
write!(
|
||||
|
@ -394,14 +366,7 @@ fn format_call(
|
|||
write!(
|
||||
f,
|
||||
[group(&format_args![&format_with(|f| {
|
||||
if let Some(arg) = &keyword.node.arg {
|
||||
write!(f, [dynamic_text(arg, TextSize::default())])?;
|
||||
write!(f, [text("=")])?;
|
||||
write!(f, [keyword.node.value.format()])?;
|
||||
} else {
|
||||
write!(f, [text("**")])?;
|
||||
write!(f, [keyword.node.value.format()])?;
|
||||
}
|
||||
write!(f, [keyword.format()])?;
|
||||
Ok(())
|
||||
})])]
|
||||
)?;
|
||||
|
@ -736,19 +701,15 @@ fn format_named_expr(
|
|||
fn format_bool_op(
|
||||
f: &mut Formatter<ASTFormatContext<'_>>,
|
||||
expr: &Expr,
|
||||
op: &Boolop,
|
||||
ops: &[BoolOp],
|
||||
values: &[Expr],
|
||||
) -> FormatResult<()> {
|
||||
let mut first = true;
|
||||
for value in values {
|
||||
if std::mem::take(&mut first) {
|
||||
write!(f, [group(&format_args![value.format()])])?;
|
||||
} else {
|
||||
write!(f, [soft_line_break_or_space()])?;
|
||||
write!(f, [op.format()])?;
|
||||
write!(f, [space()])?;
|
||||
write!(f, [group(&format_args![value.format()])])?;
|
||||
}
|
||||
write!(f, [group(&format_args![values[0].format()])])?;
|
||||
for (op, value) in ops.iter().zip(&values[1..]) {
|
||||
write!(f, [soft_line_break_or_space()])?;
|
||||
write!(f, [op.format()])?;
|
||||
write!(f, [space()])?;
|
||||
write!(f, [group(&format_args![value.format()])])?;
|
||||
}
|
||||
write!(f, [end_of_line_comments(expr)])?;
|
||||
Ok(())
|
||||
|
@ -851,7 +812,7 @@ impl Format<ASTFormatContext<'_>> for FormatExpr<'_> {
|
|||
write!(f, [leading_comments(self.item)])?;
|
||||
|
||||
match &self.item.node {
|
||||
ExprKind::BoolOp { op, values } => format_bool_op(f, self.item, op, values),
|
||||
ExprKind::BoolOp { ops, values } => format_bool_op(f, self.item, ops, values),
|
||||
ExprKind::NamedExpr { target, value } => format_named_expr(f, self.item, target, value),
|
||||
ExprKind::BinOp { left, op, right } => format_bin_op(f, self.item, left, op, right),
|
||||
ExprKind::UnaryOp { op, operand } => format_unary_op(f, self.item, op, operand),
|
||||
|
|
40
crates/ruff_python_formatter/src/format/keyword.rs
Normal file
40
crates/ruff_python_formatter/src/format/keyword.rs
Normal file
|
@ -0,0 +1,40 @@
|
|||
use ruff_formatter::prelude::*;
|
||||
use ruff_formatter::write;
|
||||
use ruff_text_size::TextSize;
|
||||
|
||||
use crate::context::ASTFormatContext;
|
||||
use crate::cst::Keyword;
|
||||
use crate::format::comments::{end_of_line_comments, leading_comments, trailing_comments};
|
||||
use crate::shared_traits::AsFormat;
|
||||
|
||||
pub struct FormatKeyword<'a> {
|
||||
item: &'a Keyword,
|
||||
}
|
||||
|
||||
impl AsFormat<ASTFormatContext<'_>> for Keyword {
|
||||
type Format<'a> = FormatKeyword<'a>;
|
||||
|
||||
fn format(&self) -> Self::Format<'_> {
|
||||
FormatKeyword { item: self }
|
||||
}
|
||||
}
|
||||
|
||||
impl Format<ASTFormatContext<'_>> for FormatKeyword<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<ASTFormatContext>) -> FormatResult<()> {
|
||||
let keyword = self.item;
|
||||
|
||||
write!(f, [leading_comments(keyword)])?;
|
||||
if let Some(arg) = &keyword.node.arg {
|
||||
write!(f, [dynamic_text(arg, TextSize::default())])?;
|
||||
write!(f, [text("=")])?;
|
||||
write!(f, [keyword.node.value.format()])?;
|
||||
} else {
|
||||
write!(f, [text("**")])?;
|
||||
write!(f, [keyword.node.value.format()])?;
|
||||
}
|
||||
write!(f, [end_of_line_comments(keyword)])?;
|
||||
write!(f, [trailing_comments(keyword)])?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
mod alias;
|
||||
mod arg;
|
||||
mod arguments;
|
||||
mod boolop;
|
||||
mod bool_op;
|
||||
pub mod builders;
|
||||
mod cmpop;
|
||||
mod comments;
|
||||
|
@ -9,6 +9,7 @@ mod comprehension;
|
|||
mod excepthandler;
|
||||
mod expr;
|
||||
mod helpers;
|
||||
mod keyword;
|
||||
mod match_case;
|
||||
mod numbers;
|
||||
mod operator;
|
||||
|
|
|
@ -131,18 +131,7 @@ fn format_class_def(
|
|||
}
|
||||
|
||||
for (i, keyword) in keywords.iter().enumerate() {
|
||||
if let Some(arg) = &keyword.node.arg {
|
||||
write!(
|
||||
f,
|
||||
[
|
||||
dynamic_text(arg, TextSize::default()),
|
||||
text("="),
|
||||
keyword.node.value.format()
|
||||
]
|
||||
)?;
|
||||
} else {
|
||||
write!(f, [text("**"), keyword.node.value.format()])?;
|
||||
}
|
||||
write!(f, [keyword.format()])?;
|
||||
if i < keywords.len() - 1 {
|
||||
write!(f, [text(","), soft_line_break_or_space()])?;
|
||||
} else {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue