mirror of
https://github.com/roc-lang/roc.git
synced 2025-11-02 22:01:20 +00:00
Merge pull request #7536 from joshuawarner32/remove-indented-else
Remove indented-else syntax from the formatter
This commit is contained in:
commit
5b4c8e70d8
8 changed files with 121 additions and 50 deletions
|
|
@ -233,16 +233,9 @@ fn format_expr_only(
|
||||||
Expr::If {
|
Expr::If {
|
||||||
if_thens: branches,
|
if_thens: branches,
|
||||||
final_else,
|
final_else,
|
||||||
indented_else,
|
indented_else: _,
|
||||||
} => {
|
} => {
|
||||||
fmt_if(
|
fmt_if(buf, branches, final_else, item.is_multiline(), indent);
|
||||||
buf,
|
|
||||||
branches,
|
|
||||||
final_else,
|
|
||||||
item.is_multiline(),
|
|
||||||
*indented_else,
|
|
||||||
indent,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
Expr::When(loc_condition, branches) => fmt_when(buf, loc_condition, branches, indent),
|
Expr::When(loc_condition, branches) => fmt_when(buf, loc_condition, branches, indent),
|
||||||
Expr::Tuple(items) => fmt_expr_collection(buf, indent, Braces::Round, *items, Newlines::No),
|
Expr::Tuple(items) => fmt_expr_collection(buf, indent, Braces::Round, *items, Newlines::No),
|
||||||
|
|
@ -1927,8 +1920,6 @@ fn fmt_if<'a>(
|
||||||
branches: &'a [(Loc<Expr<'a>>, Loc<Expr<'a>>)],
|
branches: &'a [(Loc<Expr<'a>>, Loc<Expr<'a>>)],
|
||||||
final_else: &'a Loc<Expr<'a>>,
|
final_else: &'a Loc<Expr<'a>>,
|
||||||
is_multiline: bool,
|
is_multiline: bool,
|
||||||
indented_else: bool,
|
|
||||||
|
|
||||||
indent: u16,
|
indent: u16,
|
||||||
) {
|
) {
|
||||||
// let is_multiline_then = loc_then.is_multiline();
|
// let is_multiline_then = loc_then.is_multiline();
|
||||||
|
|
@ -1972,29 +1963,20 @@ fn fmt_if<'a>(
|
||||||
}
|
}
|
||||||
|
|
||||||
buf.ensure_ends_with_whitespace();
|
buf.ensure_ends_with_whitespace();
|
||||||
if indented_else {
|
buf.indent(indent);
|
||||||
buf.indent(indent + INDENT);
|
buf.push_str("else");
|
||||||
buf.push_str("else");
|
if is_multiline {
|
||||||
buf.newline();
|
|
||||||
buf.newline();
|
|
||||||
} else if is_multiline {
|
|
||||||
buf.indent(indent);
|
|
||||||
buf.push_str("else");
|
|
||||||
buf.newline();
|
buf.newline();
|
||||||
} else {
|
} else {
|
||||||
buf.indent(indent);
|
|
||||||
buf.push_str("else");
|
|
||||||
buf.spaces(1);
|
buf.spaces(1);
|
||||||
}
|
}
|
||||||
let indent = if indented_else { indent } else { return_indent };
|
final_else.format(buf, return_indent);
|
||||||
final_else.format(buf, indent);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fmt_closure<'a>(
|
fn fmt_closure<'a>(
|
||||||
buf: &mut Buf,
|
buf: &mut Buf,
|
||||||
loc_patterns: &'a [Loc<Pattern<'a>>],
|
loc_patterns: &'a [Loc<Pattern<'a>>],
|
||||||
loc_ret: &'a Loc<Expr<'a>>,
|
loc_ret: &'a Loc<Expr<'a>>,
|
||||||
|
|
||||||
indent: u16,
|
indent: u16,
|
||||||
) {
|
) {
|
||||||
use self::Expr::*;
|
use self::Expr::*;
|
||||||
|
|
|
||||||
|
|
@ -778,11 +778,11 @@ impl<'a> Normalize<'a> for Expr<'a> {
|
||||||
Expr::If {
|
Expr::If {
|
||||||
if_thens,
|
if_thens,
|
||||||
final_else,
|
final_else,
|
||||||
indented_else,
|
indented_else: _,
|
||||||
} => Expr::If {
|
} => Expr::If {
|
||||||
if_thens: if_thens.normalize(arena),
|
if_thens: if_thens.normalize(arena),
|
||||||
final_else: arena.alloc(final_else.normalize(arena)),
|
final_else: arena.alloc(final_else.normalize(arena)),
|
||||||
indented_else,
|
indented_else: false,
|
||||||
},
|
},
|
||||||
Expr::When(a, b) => Expr::When(arena.alloc(a.normalize(arena)), b.normalize(arena)),
|
Expr::When(a, b) => Expr::When(arena.alloc(a.normalize(arena)), b.normalize(arena)),
|
||||||
Expr::ParensAround(a) => {
|
Expr::ParensAround(a) => {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
if !a! then
|
||||||
|
t
|
||||||
|
else
|
||||||
|
l
|
||||||
|
5
|
||||||
|
|
@ -0,0 +1,72 @@
|
||||||
|
@1-24 SpaceAfter(
|
||||||
|
Defs(
|
||||||
|
Defs {
|
||||||
|
tags: [
|
||||||
|
EitherIndex(2147483648),
|
||||||
|
],
|
||||||
|
regions: [
|
||||||
|
@1-22,
|
||||||
|
],
|
||||||
|
space_before: [
|
||||||
|
Slice<roc_parse::ast::CommentOrNewline> { start: 0, length: 0 },
|
||||||
|
],
|
||||||
|
space_after: [
|
||||||
|
Slice<roc_parse::ast::CommentOrNewline> { start: 0, length: 0 },
|
||||||
|
],
|
||||||
|
spaces: [],
|
||||||
|
type_defs: [],
|
||||||
|
value_defs: [
|
||||||
|
Stmt(
|
||||||
|
@1-22 If {
|
||||||
|
if_thens: [
|
||||||
|
(
|
||||||
|
@3-6 UnaryOp(
|
||||||
|
@4-6 Var {
|
||||||
|
module_name: "",
|
||||||
|
ident: "a!",
|
||||||
|
},
|
||||||
|
@3-4 Not,
|
||||||
|
),
|
||||||
|
@11-12 SpaceBefore(
|
||||||
|
SpaceAfter(
|
||||||
|
Var {
|
||||||
|
module_name: "",
|
||||||
|
ident: "t",
|
||||||
|
},
|
||||||
|
[
|
||||||
|
Newline,
|
||||||
|
],
|
||||||
|
),
|
||||||
|
[
|
||||||
|
Newline,
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
final_else: @21-22 SpaceBefore(
|
||||||
|
Var {
|
||||||
|
module_name: "",
|
||||||
|
ident: "l",
|
||||||
|
},
|
||||||
|
[
|
||||||
|
Newline,
|
||||||
|
],
|
||||||
|
),
|
||||||
|
indented_else: true,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
@23-24 SpaceBefore(
|
||||||
|
Num(
|
||||||
|
"5",
|
||||||
|
),
|
||||||
|
[
|
||||||
|
Newline,
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
[
|
||||||
|
Newline,
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
if!a!then
|
||||||
|
t
|
||||||
|
else
|
||||||
|
l
|
||||||
|
5
|
||||||
|
|
@ -2,7 +2,6 @@ if
|
||||||
k
|
k
|
||||||
then
|
then
|
||||||
A
|
A
|
||||||
else
|
else
|
||||||
|
e
|
||||||
e
|
r
|
||||||
r
|
|
||||||
|
|
@ -3061,7 +3061,7 @@ mod test_fmt {
|
||||||
x,
|
x,
|
||||||
y
|
y
|
||||||
}| a
|
}| a
|
||||||
|
|
||||||
identity 43
|
identity 43
|
||||||
"
|
"
|
||||||
),
|
),
|
||||||
|
|
@ -3069,7 +3069,7 @@ mod test_fmt {
|
||||||
r"
|
r"
|
||||||
identity =
|
identity =
|
||||||
|{ x, y }| a
|
|{ x, y }| a
|
||||||
|
|
||||||
identity 43
|
identity 43
|
||||||
"
|
"
|
||||||
),
|
),
|
||||||
|
|
@ -3083,7 +3083,7 @@ mod test_fmt {
|
||||||
x,
|
x,
|
||||||
y
|
y
|
||||||
} -> a
|
} -> a
|
||||||
|
|
||||||
identity 43
|
identity 43
|
||||||
"
|
"
|
||||||
),
|
),
|
||||||
|
|
@ -3091,7 +3091,7 @@ mod test_fmt {
|
||||||
r"
|
r"
|
||||||
identity =
|
identity =
|
||||||
|{ x, y }| a
|
|{ x, y }| a
|
||||||
|
|
||||||
identity 43
|
identity 43
|
||||||
"
|
"
|
||||||
),
|
),
|
||||||
|
|
@ -3996,15 +3996,25 @@ mod test_fmt {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn early_return_else() {
|
fn early_return_else() {
|
||||||
expr_formats_same(indoc!(
|
expr_formats_to(
|
||||||
r"
|
indoc!(
|
||||||
if foo then
|
r"
|
||||||
bar
|
if foo then
|
||||||
else
|
bar
|
||||||
|
else
|
||||||
|
|
||||||
baz
|
baz
|
||||||
"
|
"
|
||||||
));
|
),
|
||||||
|
indoc!(
|
||||||
|
r"
|
||||||
|
if foo then
|
||||||
|
bar
|
||||||
|
else
|
||||||
|
baz
|
||||||
|
"
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
expr_formats_to(
|
expr_formats_to(
|
||||||
indoc!(
|
indoc!(
|
||||||
|
|
@ -4019,9 +4029,8 @@ mod test_fmt {
|
||||||
r"
|
r"
|
||||||
if thing then
|
if thing then
|
||||||
whatever
|
whatever
|
||||||
else
|
else
|
||||||
|
too close
|
||||||
too close
|
|
||||||
"
|
"
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
@ -4031,8 +4040,7 @@ mod test_fmt {
|
||||||
r"
|
r"
|
||||||
if isGrowing plant then
|
if isGrowing plant then
|
||||||
LetBe
|
LetBe
|
||||||
else
|
else
|
||||||
|
|
||||||
Water
|
Water
|
||||||
"
|
"
|
||||||
),
|
),
|
||||||
|
|
@ -4040,9 +4048,8 @@ mod test_fmt {
|
||||||
r"
|
r"
|
||||||
if isGrowing plant then
|
if isGrowing plant then
|
||||||
LetBe
|
LetBe
|
||||||
else
|
else
|
||||||
|
Water
|
||||||
Water
|
|
||||||
"
|
"
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -472,6 +472,7 @@ mod test_snapshots {
|
||||||
pass/highest_float.expr,
|
pass/highest_float.expr,
|
||||||
pass/highest_int.expr,
|
pass/highest_int.expr,
|
||||||
pass/i_over_not_g.expr,
|
pass/i_over_not_g.expr,
|
||||||
|
pass/if_bang_then_bang_indented_else.expr,
|
||||||
pass/if_bang_then_else_one_line.expr,
|
pass/if_bang_then_else_one_line.expr,
|
||||||
pass/if_def.expr,
|
pass/if_def.expr,
|
||||||
pass/if_newline_then_negate_else_recordupdater.expr,
|
pass/if_newline_then_negate_else_recordupdater.expr,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue