mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-27 05:49:08 +00:00
format binops
This commit is contained in:
parent
dd5e13ae25
commit
6122b0650b
1 changed files with 56 additions and 23 deletions
|
@ -303,7 +303,7 @@ impl<'a> Formattable<'a> for Expr<'a> {
|
|||
parens,
|
||||
indent,
|
||||
),
|
||||
BinOps(_, _) => todo!(),
|
||||
BinOps(lefts, right) => fmt_bin_ops(buf, lefts, right, false, parens, indent),
|
||||
UnaryOp(sub_expr, unary_op) => {
|
||||
match &unary_op.value {
|
||||
operator::UnaryOp::Negate => {
|
||||
|
@ -365,28 +365,8 @@ fn format_str_segment<'a>(seg: &StrSegment<'a>, buf: &mut String<'a>, indent: u1
|
|||
}
|
||||
}
|
||||
|
||||
fn fmt_bin_op<'a>(
|
||||
buf: &mut String<'a>,
|
||||
loc_left_side: &'a Located<Expr<'a>>,
|
||||
loc_bin_op: &'a Located<BinOp>,
|
||||
loc_right_side: &'a Located<Expr<'a>>,
|
||||
part_of_multi_line_bin_ops: bool,
|
||||
apply_needs_parens: Parens,
|
||||
indent: u16,
|
||||
) {
|
||||
loc_left_side.format_with_options(buf, apply_needs_parens, Newlines::No, indent);
|
||||
|
||||
let is_multiline = (&loc_right_side.value).is_multiline()
|
||||
|| (&loc_left_side.value).is_multiline()
|
||||
|| part_of_multi_line_bin_ops;
|
||||
|
||||
if is_multiline {
|
||||
newline(buf, indent + INDENT)
|
||||
} else {
|
||||
buf.push(' ');
|
||||
}
|
||||
|
||||
match &loc_bin_op.value {
|
||||
fn push_op(buf: &mut String, op: BinOp) {
|
||||
match op {
|
||||
operator::BinOp::Caret => buf.push('^'),
|
||||
operator::BinOp::Star => buf.push('*'),
|
||||
operator::BinOp::Slash => buf.push('/'),
|
||||
|
@ -408,6 +388,30 @@ fn fmt_bin_op<'a>(
|
|||
operator::BinOp::HasType => unreachable!(),
|
||||
operator::BinOp::Backpassing => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn fmt_bin_op<'a>(
|
||||
buf: &mut String<'a>,
|
||||
loc_left_side: &'a Located<Expr<'a>>,
|
||||
loc_bin_op: &'a Located<BinOp>,
|
||||
loc_right_side: &'a Located<Expr<'a>>,
|
||||
part_of_multi_line_bin_ops: bool,
|
||||
apply_needs_parens: Parens,
|
||||
indent: u16,
|
||||
) {
|
||||
loc_left_side.format_with_options(buf, apply_needs_parens, Newlines::No, indent);
|
||||
|
||||
let is_multiline = (&loc_right_side.value).is_multiline()
|
||||
|| (&loc_left_side.value).is_multiline()
|
||||
|| part_of_multi_line_bin_ops;
|
||||
|
||||
if is_multiline {
|
||||
newline(buf, indent + INDENT)
|
||||
} else {
|
||||
buf.push(' ');
|
||||
}
|
||||
|
||||
push_op(buf, loc_bin_op.value);
|
||||
|
||||
buf.push(' ');
|
||||
|
||||
|
@ -430,6 +434,35 @@ fn fmt_bin_op<'a>(
|
|||
}
|
||||
}
|
||||
|
||||
fn fmt_bin_ops<'a>(
|
||||
buf: &mut String<'a>,
|
||||
lefts: &'a [(Located<Expr<'a>>, Located<BinOp>)],
|
||||
loc_right_side: &'a Located<Expr<'a>>,
|
||||
part_of_multi_line_bin_ops: bool,
|
||||
apply_needs_parens: Parens,
|
||||
indent: u16,
|
||||
) {
|
||||
let is_multiline = part_of_multi_line_bin_ops
|
||||
|| (&loc_right_side.value).is_multiline()
|
||||
|| lefts.iter().any(|(expr, _)| expr.value.is_multiline());
|
||||
|
||||
for (loc_left_side, loc_bin_op) in lefts {
|
||||
loc_left_side.format_with_options(buf, apply_needs_parens, Newlines::No, indent);
|
||||
|
||||
if is_multiline {
|
||||
newline(buf, indent + INDENT)
|
||||
} else {
|
||||
buf.push(' ');
|
||||
}
|
||||
|
||||
push_op(buf, loc_bin_op.value);
|
||||
|
||||
buf.push(' ');
|
||||
}
|
||||
|
||||
loc_right_side.format_with_options(buf, apply_needs_parens, Newlines::Yes, indent);
|
||||
}
|
||||
|
||||
fn fmt_list<'a>(
|
||||
buf: &mut String<'a>,
|
||||
loc_items: &[&Located<Expr<'a>>],
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue