mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 06:14:46 +00:00
Do not add parens around exprs if they are not needed in formatting
This commit is contained in:
parent
8a4975ee8a
commit
2ca9dad156
3 changed files with 33 additions and 4 deletions
|
@ -18,7 +18,7 @@ use roc_region::all::Located;
|
||||||
/// Just (Just a)
|
/// Just (Just a)
|
||||||
/// List (List a)
|
/// List (List a)
|
||||||
/// reverse (reverse l)
|
/// reverse (reverse l)
|
||||||
#[derive(PartialEq, Eq, Clone, Copy)]
|
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
|
||||||
pub enum Parens {
|
pub enum Parens {
|
||||||
NotNeeded,
|
NotNeeded,
|
||||||
InFunctionType,
|
InFunctionType,
|
||||||
|
|
|
@ -133,10 +133,14 @@ impl<'a> Formattable<'a> for Expr<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ParensAround(sub_expr) => {
|
ParensAround(sub_expr) => {
|
||||||
|
if parens == Parens::NotNeeded {
|
||||||
|
sub_expr.format_with_options(buf, Parens::NotNeeded, Newlines::Yes, indent);
|
||||||
|
} else {
|
||||||
buf.push('(');
|
buf.push('(');
|
||||||
sub_expr.format_with_options(buf, Parens::NotNeeded, Newlines::Yes, indent);
|
sub_expr.format_with_options(buf, Parens::NotNeeded, Newlines::Yes, indent);
|
||||||
buf.push(')');
|
buf.push(')');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Str(literal) => {
|
Str(literal) => {
|
||||||
use roc_parse::ast::StrLiteral::*;
|
use roc_parse::ast::StrLiteral::*;
|
||||||
|
|
||||||
|
|
|
@ -362,6 +362,31 @@ mod test_fmt {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn excess_parens() {
|
||||||
|
expr_formats_to(
|
||||||
|
indoc!(
|
||||||
|
r#"
|
||||||
|
x = (5)
|
||||||
|
|
||||||
|
|
||||||
|
y = ((10))
|
||||||
|
|
||||||
|
42
|
||||||
|
"#
|
||||||
|
),
|
||||||
|
indoc!(
|
||||||
|
r#"
|
||||||
|
x = 5
|
||||||
|
|
||||||
|
y = 10
|
||||||
|
|
||||||
|
42
|
||||||
|
"#
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// #[test]
|
// #[test]
|
||||||
// fn defs_with_defs() {
|
// fn defs_with_defs() {
|
||||||
// expr_formats_to(
|
// expr_formats_to(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue