mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-03 16:44:33 +00:00
Merge branch 'main' into fromutf-roc
This commit is contained in:
commit
063dfc37be
15 changed files with 273 additions and 70 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 + INDENT);
|
|
||||||
buf.push_str("else");
|
|
||||||
buf.newline();
|
|
||||||
buf.newline();
|
|
||||||
} else if is_multiline {
|
|
||||||
buf.indent(indent);
|
buf.indent(indent);
|
||||||
buf.push_str("else");
|
buf.push_str("else");
|
||||||
|
if is_multiline {
|
||||||
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::*;
|
||||||
|
|
|
@ -3390,7 +3390,7 @@ fn starts_with_spaces_conservative(value: &Pattern<'_>) -> bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn type_header_equivalent_to_pat<'a>(header: &TypeHeader<'a>, pat: &Pattern<'a>) -> bool {
|
fn type_header_equivalent_to_pat<'a>(header: &TypeHeader<'a>, pat: &Pattern<'a>) -> bool {
|
||||||
match pat {
|
match pat.without_spaces() {
|
||||||
Pattern::Apply(func, args) => {
|
Pattern::Apply(func, args) => {
|
||||||
if !matches!(func.value, Pattern::Tag(tag) if header.name.value == tag) {
|
if !matches!(func.value, Pattern::Tag(tag) if header.name.value == tag) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -3399,7 +3399,7 @@ fn type_header_equivalent_to_pat<'a>(header: &TypeHeader<'a>, pat: &Pattern<'a>)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
for (arg, var) in (*args).iter().zip(header.vars) {
|
for (arg, var) in (*args).iter().zip(header.vars) {
|
||||||
match (arg.value, var.value) {
|
match (arg.value.without_spaces(), var.value.without_spaces()) {
|
||||||
(Pattern::Identifier { ident: left }, TypeVar::Identifier(right)) => {
|
(Pattern::Identifier { ident: left }, TypeVar::Identifier(right)) => {
|
||||||
if left != right {
|
if left != right {
|
||||||
return false;
|
return false;
|
||||||
|
@ -3410,7 +3410,7 @@ fn type_header_equivalent_to_pat<'a>(header: &TypeHeader<'a>, pat: &Pattern<'a>)
|
||||||
}
|
}
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
Pattern::Tag(tag) => header.vars.is_empty() && header.name.value == *tag,
|
Pattern::Tag(tag) => header.vars.is_empty() && header.name.value == tag,
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3991,9 +3991,10 @@ enum OperatorOrDef {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bin_op<'a>(check_for_defs: bool) -> impl Parser<'a, BinOp, EExpr<'a>> {
|
fn bin_op<'a>(check_for_defs: bool) -> impl Parser<'a, BinOp, EExpr<'a>> {
|
||||||
(move |_, state: State<'a>, min_indent| {
|
(move |arena: &'a Bump, state: State<'a>, min_indent| {
|
||||||
let start = state.pos();
|
let start = state.pos();
|
||||||
let (_, op, state) = operator_help(EExpr::Start, EExpr::BadOperator, state, min_indent)?;
|
let (_, op, state) =
|
||||||
|
operator_help(arena, EExpr::Start, EExpr::BadOperator, state, min_indent)?;
|
||||||
let err_progress = if check_for_defs {
|
let err_progress = if check_for_defs {
|
||||||
MadeProgress
|
MadeProgress
|
||||||
} else {
|
} else {
|
||||||
|
@ -4014,12 +4015,15 @@ fn bin_op<'a>(check_for_defs: bool) -> impl Parser<'a, BinOp, EExpr<'a>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn operator<'a>() -> impl Parser<'a, OperatorOrDef, EExpr<'a>> {
|
fn operator<'a>() -> impl Parser<'a, OperatorOrDef, EExpr<'a>> {
|
||||||
(move |_, state, min_indent| operator_help(EExpr::Start, EExpr::BadOperator, state, min_indent))
|
(move |arena: &'a Bump, state, min_indent| {
|
||||||
|
operator_help(arena, EExpr::Start, EExpr::BadOperator, state, min_indent)
|
||||||
|
})
|
||||||
.trace("operator")
|
.trace("operator")
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn operator_help<'a, F, G, E>(
|
fn operator_help<'a, F, G, E>(
|
||||||
|
arena: &'a Bump,
|
||||||
to_expectation: F,
|
to_expectation: F,
|
||||||
to_error: G,
|
to_error: G,
|
||||||
mut state: State<'a>,
|
mut state: State<'a>,
|
||||||
|
@ -4030,20 +4034,17 @@ where
|
||||||
G: Fn(&'a str, Position) -> E,
|
G: Fn(&'a str, Position) -> E,
|
||||||
E: 'a,
|
E: 'a,
|
||||||
{
|
{
|
||||||
match *state.bytes() {
|
let and_or = either(
|
||||||
[b'o', b'r', ..] => {
|
parser::keyword(keyword::AND, EExpr::End),
|
||||||
return Ok((
|
parser::keyword(keyword::OR, EExpr::End),
|
||||||
MadeProgress,
|
);
|
||||||
OperatorOrDef::BinOp(BinOp::Or),
|
|
||||||
state.advance(2),
|
match and_or.parse(arena, state.clone(), min_indent) {
|
||||||
))
|
Ok((MadeProgress, Either::First(_), state)) => {
|
||||||
|
return Ok((MadeProgress, OperatorOrDef::BinOp(BinOp::And), state))
|
||||||
}
|
}
|
||||||
[b'a', b'n', b'd', ..] => {
|
Ok((MadeProgress, Either::Second(_), state)) => {
|
||||||
return Ok((
|
return Ok((MadeProgress, OperatorOrDef::BinOp(BinOp::Or), state))
|
||||||
MadeProgress,
|
|
||||||
OperatorOrDef::BinOp(BinOp::And),
|
|
||||||
state.advance(3),
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
E : i
|
||||||
|
E = h
|
||||||
|
0
|
|
@ -0,0 +1,60 @@
|
||||||
|
@0-11 SpaceAfter(
|
||||||
|
Defs(
|
||||||
|
Defs {
|
||||||
|
tags: [
|
||||||
|
EitherIndex(2147483648),
|
||||||
|
],
|
||||||
|
regions: [
|
||||||
|
@0-9,
|
||||||
|
],
|
||||||
|
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: [
|
||||||
|
AnnotatedBody {
|
||||||
|
ann_pattern: Apply(
|
||||||
|
@0-1 Tag(
|
||||||
|
"E",
|
||||||
|
),
|
||||||
|
[],
|
||||||
|
),
|
||||||
|
ann_type: @2-3 BoundVariable(
|
||||||
|
"i",
|
||||||
|
),
|
||||||
|
lines_between: [
|
||||||
|
Newline,
|
||||||
|
Newline,
|
||||||
|
],
|
||||||
|
body_pattern: @5-6 SpaceAfter(
|
||||||
|
Tag(
|
||||||
|
"E",
|
||||||
|
),
|
||||||
|
[
|
||||||
|
Newline,
|
||||||
|
],
|
||||||
|
),
|
||||||
|
body_expr: @8-9 Var {
|
||||||
|
module_name: "",
|
||||||
|
ident: "h",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
@10-11 SpaceBefore(
|
||||||
|
Num(
|
||||||
|
"0",
|
||||||
|
),
|
||||||
|
[
|
||||||
|
Newline,
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
[
|
||||||
|
Newline,
|
||||||
|
],
|
||||||
|
)
|
|
@ -0,0 +1,5 @@
|
||||||
|
E:i
|
||||||
|
|
||||||
|
E
|
||||||
|
=h
|
||||||
|
0
|
|
@ -0,0 +1,3 @@
|
||||||
|
a
|
||||||
|
ands
|
||||||
|
d
|
|
@ -0,0 +1,55 @@
|
||||||
|
@0-11 SpaceAfter(
|
||||||
|
Defs(
|
||||||
|
Defs {
|
||||||
|
tags: [
|
||||||
|
EitherIndex(2147483648),
|
||||||
|
EitherIndex(2147483649),
|
||||||
|
],
|
||||||
|
regions: [
|
||||||
|
@0-1,
|
||||||
|
@2-9,
|
||||||
|
],
|
||||||
|
space_before: [
|
||||||
|
Slice<roc_parse::ast::CommentOrNewline> { start: 0, length: 0 },
|
||||||
|
Slice<roc_parse::ast::CommentOrNewline> { start: 0, length: 1 },
|
||||||
|
],
|
||||||
|
space_after: [
|
||||||
|
Slice<roc_parse::ast::CommentOrNewline> { start: 0, length: 0 },
|
||||||
|
Slice<roc_parse::ast::CommentOrNewline> { start: 1, length: 0 },
|
||||||
|
],
|
||||||
|
spaces: [
|
||||||
|
Newline,
|
||||||
|
],
|
||||||
|
type_defs: [],
|
||||||
|
value_defs: [
|
||||||
|
Stmt(
|
||||||
|
@0-1 Var {
|
||||||
|
module_name: "",
|
||||||
|
ident: "a",
|
||||||
|
},
|
||||||
|
),
|
||||||
|
Body(
|
||||||
|
@2-4 RecordDestructure(
|
||||||
|
[],
|
||||||
|
),
|
||||||
|
@5-9 Var {
|
||||||
|
module_name: "",
|
||||||
|
ident: "ands",
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
@10-11 SpaceBefore(
|
||||||
|
Var {
|
||||||
|
module_name: "",
|
||||||
|
ident: "d",
|
||||||
|
},
|
||||||
|
[
|
||||||
|
Newline,
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
[
|
||||||
|
Newline,
|
||||||
|
],
|
||||||
|
)
|
|
@ -0,0 +1,3 @@
|
||||||
|
a
|
||||||
|
{}=ands
|
||||||
|
d
|
|
@ -3996,7 +3996,8 @@ mod test_fmt {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn early_return_else() {
|
fn early_return_else() {
|
||||||
expr_formats_same(indoc!(
|
expr_formats_to(
|
||||||
|
indoc!(
|
||||||
r"
|
r"
|
||||||
if foo then
|
if foo then
|
||||||
bar
|
bar
|
||||||
|
@ -4004,7 +4005,16 @@ mod test_fmt {
|
||||||
|
|
||||||
baz
|
baz
|
||||||
"
|
"
|
||||||
));
|
),
|
||||||
|
indoc!(
|
||||||
|
r"
|
||||||
|
if foo then
|
||||||
|
bar
|
||||||
|
else
|
||||||
|
baz
|
||||||
|
"
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
expr_formats_to(
|
expr_formats_to(
|
||||||
indoc!(
|
indoc!(
|
||||||
|
@ -4020,7 +4030,6 @@ mod test_fmt {
|
||||||
if thing then
|
if thing then
|
||||||
whatever
|
whatever
|
||||||
else
|
else
|
||||||
|
|
||||||
too close
|
too close
|
||||||
"
|
"
|
||||||
),
|
),
|
||||||
|
@ -4032,7 +4041,6 @@ mod test_fmt {
|
||||||
if isGrowing plant then
|
if isGrowing plant then
|
||||||
LetBe
|
LetBe
|
||||||
else
|
else
|
||||||
|
|
||||||
Water
|
Water
|
||||||
"
|
"
|
||||||
),
|
),
|
||||||
|
@ -4041,7 +4049,6 @@ mod test_fmt {
|
||||||
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,
|
||||||
|
@ -697,12 +698,14 @@ mod test_snapshots {
|
||||||
pass/return_record_update_comment_empty_fields.expr,
|
pass/return_record_update_comment_empty_fields.expr,
|
||||||
pass/return_then_nested_parens.expr,
|
pass/return_then_nested_parens.expr,
|
||||||
pass/return_with_after.expr,
|
pass/return_with_after.expr,
|
||||||
|
pass/sep_annotation.expr,
|
||||||
pass/separate_defs.moduledefs,
|
pass/separate_defs.moduledefs,
|
||||||
pass/single_arg_closure.expr,
|
pass/single_arg_closure.expr,
|
||||||
pass/single_arg_with_underscore_closure.expr,
|
pass/single_arg_with_underscore_closure.expr,
|
||||||
pass/single_question_binop_closure.expr,
|
pass/single_question_binop_closure.expr,
|
||||||
pass/single_question_binop_tag.expr,
|
pass/single_question_binop_tag.expr,
|
||||||
pass/single_underscore_closure.expr,
|
pass/single_underscore_closure.expr,
|
||||||
|
pass/sneaky_and_expr.expr,
|
||||||
pass/sneaky_implements_in_opaque_fn_type.expr,
|
pass/sneaky_implements_in_opaque_fn_type.expr,
|
||||||
pass/space_after_opt_field_pat.expr,
|
pass/space_after_opt_field_pat.expr,
|
||||||
pass/space_before_colon.full,
|
pass/space_before_colon.full,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue