diff --git a/crates/syntax/src/ast/expr_ext.rs b/crates/syntax/src/ast/expr_ext.rs index 01ab562c7ee..b615037b7e3 100644 --- a/crates/syntax/src/ast/expr_ext.rs +++ b/crates/syntax/src/ast/expr_ext.rs @@ -350,6 +350,42 @@ impl ast::BinExpr { } } +impl std::fmt::Display for BinOp { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + BinOp::BooleanOr => write!(f, "||"), + BinOp::BooleanAnd => write!(f, "&&"), + BinOp::EqualityTest => write!(f, "=="), + BinOp::NegatedEqualityTest => write!(f, "!="), + BinOp::LesserEqualTest => write!(f, "<="), + BinOp::GreaterEqualTest => write!(f, ">="), + BinOp::LesserTest => write!(f, "<"), + BinOp::GreaterTest => write!(f, ">"), + BinOp::Addition => write!(f, "+"), + BinOp::Multiplication => write!(f, "*"), + BinOp::Subtraction => write!(f, "-"), + BinOp::Division => write!(f, "/"), + BinOp::Remainder => write!(f, "%"), + BinOp::LeftShift => write!(f, "<<"), + BinOp::RightShift => write!(f, ">>"), + BinOp::BitwiseXor => write!(f, "^"), + BinOp::BitwiseOr => write!(f, "|"), + BinOp::BitwiseAnd => write!(f, "&"), + BinOp::Assignment => write!(f, "="), + BinOp::AddAssign => write!(f, "+="), + BinOp::DivAssign => write!(f, "/="), + BinOp::MulAssign => write!(f, "*="), + BinOp::RemAssign => write!(f, "%="), + BinOp::ShrAssign => write!(f, ">>="), + BinOp::ShlAssign => write!(f, "<<="), + BinOp::SubAssign => write!(f, "-"), + BinOp::BitOrAssign => write!(f, "|="), + BinOp::BitAndAssign => write!(f, "&="), + BinOp::BitXorAssign => write!(f, "^="), + } + } +} + #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] pub enum RangeOp { /// `..` diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index ec90be35a6e..74f2e5c314c 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs @@ -264,6 +264,9 @@ pub fn expr_path(path: ast::Path) -> ast::Expr { pub fn expr_continue() -> ast::Expr { expr_from_text("continue") } +pub fn expr_op(op: ast::BinOp, lhs: ast::Expr, rhs: ast::Expr) -> ast::Expr { + expr_from_text(&format!("{} {} {}", lhs, op, rhs)) +} pub fn expr_break(expr: Option) -> ast::Expr { match expr { Some(expr) => expr_from_text(&format!("break {}", expr)),