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

View File

@ -864,3 +864,26 @@ fn hex_and_numeric_underscore() {
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);
}