Introduce dedicated CST tokens for other operator kinds (#3267)

This commit is contained in:
Charlie Marsh 2023-02-27 23:54:57 -05:00 committed by GitHub
parent 061495a9eb
commit f5f09b489b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 425 additions and 217 deletions

View file

@ -20,19 +20,17 @@ impl AsFormat<ASTFormatContext<'_>> for BoolOp {
impl Format<ASTFormatContext<'_>> for FormatBoolOp<'_> {
fn fmt(&self, f: &mut Formatter<ASTFormatContext>) -> FormatResult<()> {
let boolop = self.item;
write!(f, [leading_comments(boolop)])?;
let bool_op = self.item;
write!(f, [leading_comments(bool_op)])?;
write!(
f,
[text(match boolop.node {
[text(match bool_op.node {
BoolOpKind::And => "and",
BoolOpKind::Or => "or",
})]
)?;
write!(f, [end_of_line_comments(boolop)])?;
write!(f, [trailing_comments(boolop)])?;
write!(f, [end_of_line_comments(bool_op)])?;
write!(f, [trailing_comments(bool_op)])?;
Ok(())
}
}

View file

@ -0,0 +1,44 @@
use ruff_formatter::prelude::*;
use ruff_formatter::write;
use crate::context::ASTFormatContext;
use crate::cst::{CmpOp, CmpOpKind};
use crate::format::comments::{end_of_line_comments, leading_comments, trailing_comments};
use crate::shared_traits::AsFormat;
pub struct FormatCmpOp<'a> {
item: &'a CmpOp,
}
impl AsFormat<ASTFormatContext<'_>> for CmpOp {
type Format<'a> = FormatCmpOp<'a>;
fn format(&self) -> Self::Format<'_> {
FormatCmpOp { item: self }
}
}
impl Format<ASTFormatContext<'_>> for FormatCmpOp<'_> {
fn fmt(&self, f: &mut Formatter<ASTFormatContext>) -> FormatResult<()> {
let cmp_op = self.item;
write!(f, [leading_comments(cmp_op)])?;
write!(
f,
[text(match cmp_op.node {
CmpOpKind::Eq => "==",
CmpOpKind::NotEq => "!=",
CmpOpKind::Lt => "<",
CmpOpKind::LtE => "<=",
CmpOpKind::Gt => ">",
CmpOpKind::GtE => ">=",
CmpOpKind::Is => "is",
CmpOpKind::IsNot => "is not",
CmpOpKind::In => "in",
CmpOpKind::NotIn => "not in",
})]
)?;
write!(f, [end_of_line_comments(cmp_op)])?;
write!(f, [trailing_comments(cmp_op)])?;
Ok(())
}
}

View file

@ -1,40 +0,0 @@
use ruff_formatter::prelude::*;
use ruff_formatter::write;
use crate::context::ASTFormatContext;
use crate::cst::Cmpop;
use crate::shared_traits::AsFormat;
pub struct FormatCmpop<'a> {
item: &'a Cmpop,
}
impl AsFormat<ASTFormatContext<'_>> for Cmpop {
type Format<'a> = FormatCmpop<'a>;
fn format(&self) -> Self::Format<'_> {
FormatCmpop { item: self }
}
}
impl Format<ASTFormatContext<'_>> for FormatCmpop<'_> {
fn fmt(&self, f: &mut Formatter<ASTFormatContext>) -> FormatResult<()> {
let unaryop = self.item;
write!(
f,
[text(match unaryop {
Cmpop::Eq => "==",
Cmpop::NotEq => "!=",
Cmpop::Lt => "<",
Cmpop::LtE => "<=",
Cmpop::Gt => ">",
Cmpop::GtE => ">=",
Cmpop::Is => "is",
Cmpop::IsNot => "is not",
Cmpop::In => "in",
Cmpop::NotIn => "not in",
})]
)?;
Ok(())
}
}

View file

@ -9,8 +9,8 @@ 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,
SliceIndexKind, Unaryop,
Arguments, BoolOp, CmpOp, Comprehension, Expr, ExprKind, Keyword, Operator, OperatorKind,
SliceIndex, SliceIndexKind, UnaryOp, UnaryOpKind,
};
use crate::format::builders::literal;
use crate::format::comments::{dangling_comments, end_of_line_comments, leading_comments};
@ -556,7 +556,7 @@ fn format_compare(
f: &mut Formatter<ASTFormatContext<'_>>,
expr: &Expr,
left: &Expr,
ops: &[Cmpop],
ops: &[CmpOp],
comparators: &[Expr],
) -> FormatResult<()> {
write!(f, [group(&format_args![left.format()])])?;
@ -723,7 +723,8 @@ fn format_bin_op(
right: &Expr,
) -> FormatResult<()> {
// https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#line-breaks-binary-operators
let is_simple = matches!(op, Operator::Pow) && is_simple_power(left) && is_simple_power(right);
let is_simple =
matches!(op.node, OperatorKind::Pow) && is_simple_power(left) && is_simple_power(right);
write!(f, [left.format()])?;
if !is_simple {
write!(f, [soft_line_break_or_space()])?;
@ -740,12 +741,12 @@ fn format_bin_op(
fn format_unary_op(
f: &mut Formatter<ASTFormatContext<'_>>,
expr: &Expr,
op: &Unaryop,
op: &UnaryOp,
operand: &Expr,
) -> FormatResult<()> {
write!(f, [op.format()])?;
// TODO(charlie): Do this in the normalization pass.
if !matches!(op, Unaryop::Not)
if !matches!(op.node, UnaryOpKind::Not)
&& matches!(
operand.node,
ExprKind::BoolOp { .. } | ExprKind::Compare { .. } | ExprKind::BinOp { .. }

View file

@ -1,4 +1,4 @@
use crate::cst::{Expr, ExprKind, Unaryop};
use crate::cst::{Expr, ExprKind, UnaryOpKind};
pub fn is_self_closing(expr: &Expr) -> bool {
match &expr.node {
@ -56,7 +56,7 @@ pub fn is_self_closing(expr: &Expr) -> bool {
pub fn is_simple_slice(expr: &Expr) -> bool {
match &expr.node {
ExprKind::UnaryOp { op, operand } => {
if matches!(op, Unaryop::Not) {
if matches!(op.node, UnaryOpKind::Not) {
false
} else {
is_simple_slice(operand)
@ -73,7 +73,7 @@ pub fn is_simple_slice(expr: &Expr) -> bool {
pub fn is_simple_power(expr: &Expr) -> bool {
match &expr.node {
ExprKind::UnaryOp { op, operand } => {
if matches!(op, Unaryop::Not) {
if matches!(op.node, UnaryOpKind::Not) {
false
} else {
is_simple_slice(operand)

View file

@ -3,7 +3,7 @@ mod arg;
mod arguments;
mod bool_op;
pub mod builders;
mod cmpop;
mod cmp_op;
mod comments;
mod comprehension;
mod excepthandler;
@ -16,5 +16,5 @@ mod operator;
mod pattern;
mod stmt;
mod strings;
mod unaryop;
mod unary_op;
mod withitem;

View file

@ -2,7 +2,8 @@ use ruff_formatter::prelude::*;
use ruff_formatter::write;
use crate::context::ASTFormatContext;
use crate::cst::Operator;
use crate::cst::{Operator, OperatorKind};
use crate::format::comments::{end_of_line_comments, leading_comments, trailing_comments};
use crate::shared_traits::AsFormat;
pub struct FormatOperator<'a> {
@ -20,26 +21,27 @@ impl AsFormat<ASTFormatContext<'_>> for Operator {
impl Format<ASTFormatContext<'_>> for FormatOperator<'_> {
fn fmt(&self, f: &mut Formatter<ASTFormatContext<'_>>) -> FormatResult<()> {
let operator = self.item;
write!(f, [leading_comments(operator)])?;
write!(
f,
[text(match operator {
Operator::Add => "+",
Operator::Sub => "-",
Operator::Mult => "*",
Operator::MatMult => "@",
Operator::Div => "/",
Operator::Mod => "%",
Operator::Pow => "**",
Operator::LShift => "<<",
Operator::RShift => ">>",
Operator::BitOr => "|",
Operator::BitXor => "^",
Operator::BitAnd => "&",
Operator::FloorDiv => "//",
[text(match operator.node {
OperatorKind::Add => "+",
OperatorKind::Sub => "-",
OperatorKind::Mult => "*",
OperatorKind::MatMult => "@",
OperatorKind::Div => "/",
OperatorKind::Mod => "%",
OperatorKind::Pow => "**",
OperatorKind::LShift => "<<",
OperatorKind::RShift => ">>",
OperatorKind::BitOr => "|",
OperatorKind::BitXor => "^",
OperatorKind::BitAnd => "&",
OperatorKind::FloorDiv => "//",
})]
)?;
write!(f, [end_of_line_comments(operator)])?;
write!(f, [trailing_comments(operator)])?;
Ok(())
}
}

View file

@ -0,0 +1,37 @@
use ruff_formatter::prelude::*;
use ruff_formatter::write;
use crate::context::ASTFormatContext;
use crate::cst::{UnaryOp, UnaryOpKind};
use crate::shared_traits::AsFormat;
pub struct FormatUnaryOp<'a> {
item: &'a UnaryOp,
}
impl AsFormat<ASTFormatContext<'_>> for UnaryOp {
type Format<'a> = FormatUnaryOp<'a>;
fn format(&self) -> Self::Format<'_> {
FormatUnaryOp { item: self }
}
}
impl Format<ASTFormatContext<'_>> for FormatUnaryOp<'_> {
fn fmt(&self, f: &mut Formatter<ASTFormatContext>) -> FormatResult<()> {
let unary_op = self.item;
write!(
f,
[
text(match unary_op.node {
UnaryOpKind::Invert => "~",
UnaryOpKind::Not => "not",
UnaryOpKind::UAdd => "+",
UnaryOpKind::USub => "-",
}),
matches!(unary_op.node, UnaryOpKind::Not).then_some(space())
]
)?;
Ok(())
}
}

View file

@ -1,37 +0,0 @@
use ruff_formatter::prelude::*;
use ruff_formatter::write;
use crate::context::ASTFormatContext;
use crate::cst::Unaryop;
use crate::shared_traits::AsFormat;
pub struct FormatUnaryop<'a> {
item: &'a Unaryop,
}
impl AsFormat<ASTFormatContext<'_>> for Unaryop {
type Format<'a> = FormatUnaryop<'a>;
fn format(&self) -> Self::Format<'_> {
FormatUnaryop { item: self }
}
}
impl Format<ASTFormatContext<'_>> for FormatUnaryop<'_> {
fn fmt(&self, f: &mut Formatter<ASTFormatContext>) -> FormatResult<()> {
let unaryop = self.item;
write!(
f,
[
text(match unaryop {
Unaryop::Invert => "~",
Unaryop::Not => "not",
Unaryop::UAdd => "+",
Unaryop::USub => "-",
}),
matches!(unaryop, Unaryop::Not).then_some(space())
]
)?;
Ok(())
}
}