mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-02 16:21:11 +00:00
Make sure patterns continue to parse as such
This commit is contained in:
parent
ea1ecb9e68
commit
64164eb1f4
6 changed files with 91 additions and 4 deletions
|
@ -454,7 +454,15 @@ impl<'a> Formattable for TypeDef<'a> {
|
|||
|
||||
let make_multiline = ann.is_multiline() || has_abilities_multiline;
|
||||
|
||||
fmt_general_def(header, buf, indent, ":=", &ann.value, newlines);
|
||||
fmt_general_def(
|
||||
header,
|
||||
Parens::NotNeeded,
|
||||
buf,
|
||||
indent,
|
||||
":=",
|
||||
&ann.value,
|
||||
newlines,
|
||||
);
|
||||
|
||||
if let Some(has_abilities) = has_abilities {
|
||||
buf.spaces(1);
|
||||
|
@ -824,8 +832,14 @@ impl<'a> Formattable for ValueDef<'a> {
|
|||
use roc_parse::ast::ValueDef::*;
|
||||
match self {
|
||||
Annotation(loc_pattern, loc_annotation) => {
|
||||
let pat_parens = if ann_pattern_needs_parens(&loc_pattern.value) {
|
||||
Parens::InApply
|
||||
} else {
|
||||
Parens::NotNeeded
|
||||
};
|
||||
fmt_general_def(
|
||||
loc_pattern,
|
||||
pat_parens,
|
||||
buf,
|
||||
indent,
|
||||
":",
|
||||
|
@ -845,7 +859,20 @@ impl<'a> Formattable for ValueDef<'a> {
|
|||
body_pattern,
|
||||
body_expr,
|
||||
} => {
|
||||
fmt_general_def(ann_pattern, buf, indent, ":", &ann_type.value, newlines);
|
||||
let pat_parens = if ann_pattern_needs_parens(&ann_pattern.value) {
|
||||
Parens::InApply
|
||||
} else {
|
||||
Parens::NotNeeded
|
||||
};
|
||||
fmt_general_def(
|
||||
ann_pattern,
|
||||
pat_parens,
|
||||
buf,
|
||||
indent,
|
||||
":",
|
||||
&ann_type.value,
|
||||
newlines,
|
||||
);
|
||||
|
||||
fmt_annotated_body_comment(buf, indent, lines_between);
|
||||
|
||||
|
@ -860,8 +887,19 @@ impl<'a> Formattable for ValueDef<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
fn ann_pattern_needs_parens(value: &Pattern<'_>) -> bool {
|
||||
match value.extract_spaces().item {
|
||||
Pattern::Tag(_) => true,
|
||||
Pattern::Apply(func, _args) if matches!(func.extract_spaces().item, Pattern::Tag(..)) => {
|
||||
true
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn fmt_general_def<L: Formattable>(
|
||||
lhs: L,
|
||||
lhs_parens: Parens,
|
||||
buf: &mut Buf,
|
||||
|
||||
indent: u16,
|
||||
|
@ -869,7 +907,7 @@ fn fmt_general_def<L: Formattable>(
|
|||
rhs: &TypeAnnotation,
|
||||
newlines: Newlines,
|
||||
) {
|
||||
lhs.format(buf, indent);
|
||||
lhs.format_with_options(buf, lhs_parens, Newlines::Yes, indent);
|
||||
buf.indent(indent);
|
||||
|
||||
if rhs.is_multiline() {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
UserId x : [UserId I64]
|
||||
(UserId x) : [UserId I64]
|
||||
(UserId x) = UserId 42
|
||||
|
||||
x
|
|
@ -0,0 +1,2 @@
|
|||
(J x) : i
|
||||
i
|
|
@ -0,0 +1,44 @@
|
|||
Defs(
|
||||
Defs {
|
||||
tags: [
|
||||
EitherIndex(2147483648),
|
||||
],
|
||||
regions: [
|
||||
@0-8,
|
||||
],
|
||||
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: [
|
||||
Annotation(
|
||||
@1-5 Apply(
|
||||
@2-3 Tag(
|
||||
"J",
|
||||
),
|
||||
[
|
||||
@4-5 Identifier {
|
||||
ident: "x",
|
||||
},
|
||||
],
|
||||
),
|
||||
@7-8 BoundVariable(
|
||||
"i",
|
||||
),
|
||||
),
|
||||
],
|
||||
},
|
||||
@9-10 SpaceBefore(
|
||||
Var {
|
||||
module_name: "",
|
||||
ident: "i",
|
||||
},
|
||||
[
|
||||
Newline,
|
||||
],
|
||||
),
|
||||
)
|
|
@ -0,0 +1,2 @@
|
|||
((J)x):i
|
||||
i
|
|
@ -480,6 +480,7 @@ mod test_snapshots {
|
|||
pass/nested_def_annotation.moduledefs,
|
||||
pass/nested_if.expr,
|
||||
pass/nested_list_comment_in_closure_arg.expr,
|
||||
pass/nested_parens_in_pattern.expr,
|
||||
pass/newline_after_equals.expr, // Regression test for https://github.com/roc-lang/roc/issues/51
|
||||
pass/newline_after_mul.expr,
|
||||
pass/newline_after_opt_field.expr,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue