Implement formatter for anon binop.

This commit is contained in:
KtorZ 2023-06-17 08:44:59 +02:00
parent 91f03abb7b
commit 4252ee6373
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
2 changed files with 39 additions and 17 deletions

View File

@ -775,11 +775,8 @@ impl<'comments> Formatter<'comments> {
UntypedExpr::Fn { UntypedExpr::Fn {
fn_style: FnStyle::BinOp(op), fn_style: FnStyle::BinOp(op),
body,
.. ..
} => { } => op.to_doc(),
unimplemented!()
}
UntypedExpr::Fn { UntypedExpr::Fn {
fn_style: FnStyle::Plain, fn_style: FnStyle::Plain,
@ -1070,7 +1067,9 @@ impl<'comments> Formatter<'comments> {
let right = self.expr(right); let right = self.expr(right);
self.operator_side(left, precedence, left_precedence) self.operator_side(left, precedence, left_precedence)
.append(" ")
.append(name) .append(name)
.append(" ")
.append(self.operator_side(right, precedence, right_precedence - 1)) .append(self.operator_side(right, precedence, right_precedence - 1))
} }
@ -1726,19 +1725,19 @@ impl<'a> Documentable<'a> for &'a UnqualifiedImport {
impl<'a> Documentable<'a> for &'a BinOp { impl<'a> Documentable<'a> for &'a BinOp {
fn to_doc(self) -> Document<'a> { fn to_doc(self) -> Document<'a> {
match self { match self {
BinOp::And => " && ", BinOp::And => "&&",
BinOp::Or => " || ", BinOp::Or => "||",
BinOp::LtInt => " < ", BinOp::LtInt => "<",
BinOp::LtEqInt => " <= ", BinOp::LtEqInt => "<=",
BinOp::Eq => " == ", BinOp::Eq => "==",
BinOp::NotEq => " != ", BinOp::NotEq => "!=",
BinOp::GtEqInt => " >= ", BinOp::GtEqInt => ">=",
BinOp::GtInt => " > ", BinOp::GtInt => ">",
BinOp::AddInt => " + ", BinOp::AddInt => "+",
BinOp::SubInt => " - ", BinOp::SubInt => "-",
BinOp::MultInt => " * ", BinOp::MultInt => "*",
BinOp::DivInt => " / ", BinOp::DivInt => "/",
BinOp::ModInt => " % ", BinOp::ModInt => "%",
} }
.to_doc() .to_doc()
} }

View File

@ -864,3 +864,26 @@ fn hex_and_numeric_underscore() {
assert_fmt(src, src); assert_fmt(src, src);
} }
#[test]
fn first_class_binop() {
let src = indoc! { r#"
fn foo() {
compare_with(a, >, b)
compare_with(a, >=, b)
compare_with(a, <, b)
compare_with(a, <=, b)
compare_with(a, ==, b)
compare_with(a, !=, b)
combine_with(a, &&, b)
combine_with(a, ||, b)
compute_with(a, +, b)
compute_with(a, -, b)
compute_with(a, /, b)
compute_with(a, *, b)
compute_with(a, %, b)
}
"#};
assert_fmt(src, src);
}