PNC for Patterns, stabilize formatting

This commit is contained in:
Anthony Bullard 2024-12-31 08:25:11 -06:00
parent bac165fd99
commit 3b0db07fa1
No known key found for this signature in database
78 changed files with 789 additions and 332 deletions

View file

@ -21,6 +21,7 @@
"E",
),
[],
Whitespace,
),
ann_type: @2-3 Apply(
"",

View file

@ -26,6 +26,7 @@
ident: "x",
},
],
Whitespace,
),
ann_type: @11-25 TagUnion {
ext: None,
@ -54,6 +55,7 @@
ident: "x",
},
],
Whitespace,
),
body_expr: @37-46 Apply(
@37-43 Tag(

View file

@ -0,0 +1,16 @@
@0-4 SpaceAfter(
Apply(
@0-2 Tag(
"Ok",
),
[
@3-4 Identifier {
ident: "a",
},
],
ParensAndCommas,
),
[
Newline,
],
)

View file

@ -0,0 +1,16 @@
@0-4 SpaceAfter(
Apply(
@0-2 Tag(
"Ok",
),
[
@3-4 Identifier {
ident: "a",
},
],
Whitespace,
),
[
Newline,
],
)

View file

@ -27,6 +27,7 @@
ident: "inner",
},
],
Whitespace,
),
PatternAs {
spaces_before: [],

View file

@ -38,6 +38,7 @@
},
),
],
Whitespace,
),
],
@17-18 SpaceBefore(

View file

@ -20,6 +20,7 @@
],
),
],
ParensAndCommas,
),
],
@11-13 Num(

View file

@ -16,6 +16,7 @@
],
),
],
Whitespace,
),
],
@8-10 UnaryOp(

View file

@ -19,6 +19,7 @@
],
),
],
Whitespace,
),
],
@10-12 Var {

View file

@ -16,7 +16,7 @@
type_defs: [],
value_defs: [
Annotation(
@0-1 Apply(
@0-10 Apply(
@0-1 NumLiteral(
"1",
),
@ -35,6 +35,7 @@
],
),
],
ParensAndCommas,
),
@11-13 BoundVariable(
"gi",

View file

@ -1,5 +1,5 @@
1
(0 #
0)
1(
0( #
0,),)
f : f
t

View file

@ -17,37 +17,44 @@
type_defs: [],
value_defs: [
Annotation(
@0-1 Apply(
@0-1 NumLiteral(
"1",
),
[
@2-11 SpaceAfter(
Apply(
@3-4 NumLiteral(
"0",
@0-12 Apply(
@0-12 Apply(
@0-1 NumLiteral(
"1",
),
[
@2-11 SpaceAfter(
Apply(
@3-4 NumLiteral(
"0",
),
[
@7-8 SpaceBefore(
NumLiteral(
"0",
),
[
LineComment(
"",
),
],
),
],
ParensAndCommas,
),
[
@7-8 SpaceBefore(
NumLiteral(
"0",
),
[
LineComment(
"",
),
],
),
Newline,
],
),
[
Newline,
],
),
],
ParensAndCommas,
),
[
@12-13 Identifier {
ident: "f",
},
],
Whitespace,
),
@14-15 BoundVariable(
"f",

View file

@ -16,39 +16,46 @@
type_defs: [],
value_defs: [
Annotation(
@0-1 Apply(
@0-1 NumLiteral(
"1",
),
[
@2-13 SpaceAfter(
Apply(
@2-4 Identifier {
ident: "ts",
},
[
@5-9 SpaceAfter(
NumLiteral(
"0",
),
[
Newline,
Newline,
LineComment(
"",
@0-15 Apply(
@0-15 Apply(
@0-1 NumLiteral(
"1",
),
[
@2-13 SpaceAfter(
Apply(
@2-4 Identifier {
ident: "ts",
},
[
@5-9 SpaceAfter(
NumLiteral(
"0",
),
],
),
[
Newline,
Newline,
LineComment(
"",
),
],
),
],
ParensAndCommas,
),
[
Newline,
],
),
[
Newline,
],
),
],
ParensAndCommas,
),
[
@15-16 Identifier {
ident: "f",
},
],
Whitespace,
),
@17-20 BoundVariable(
"i7f",

View file

@ -34,6 +34,7 @@
ident: "f",
},
],
Whitespace,
),
@14-15 BoundVariable(
"f",

View file

@ -26,6 +26,7 @@
ident: "str",
},
],
Whitespace,
),
@12-36 Apply(
@12-17 Tag(

View file

@ -35,6 +35,7 @@
ident: "e",
},
],
Whitespace,
),
@8-14 Apply(
@9-10 SpaceAfter(
@ -52,6 +53,7 @@
ident: "p",
},
],
Whitespace,
),
],
),

View file

@ -1,11 +1,11 @@
@0-22 SpaceAfter(
@0-23 SpaceAfter(
Defs(
Defs {
tags: [
EitherIndex(0),
],
regions: [
@0-20,
@0-21,
],
space_before: [
Slice<roc_parse::ast::CommentOrNewline> { start: 0, length: 0 },
@ -19,23 +19,23 @@
header: TypeHeader {
name: @0-1 "C",
vars: [
@2-3 SpaceAfter(
NumLiteral(
"4",
),
[
LineComment(
"",
),
],
@2-3 NumLiteral(
"4",
),
],
},
loc_implements: @6-16 Implements,
loc_implements: @7-17 SpaceBefore(
Implements,
[
LineComment(
"",
),
],
),
members: [
AbilityMember {
name: @17-18 "e",
typ: @19-20 BoundVariable(
name: @18-19 "e",
typ: @20-21 BoundVariable(
"m",
),
},
@ -44,7 +44,7 @@
],
value_defs: [],
},
@21-22 SpaceBefore(
@22-23 SpaceBefore(
Tag(
"C",
),

View file

@ -0,0 +1,3 @@
C 4 #
implements e:m
C

View file

@ -33,6 +33,7 @@
],
),
],
Whitespace,
),
@15-16 BoundVariable(
"s",

View file

@ -1,6 +1,6 @@
1
(0 #
f)
1(
0 #
f,)
(0 #
f) : f
e

View file

@ -17,28 +17,34 @@
type_defs: [],
value_defs: [
Annotation(
@0-1 Apply(
@0-1 NumLiteral(
"1",
),
[
@2-8 Apply(
@3-4 SpaceAfter(
NumLiteral(
"0",
@0-9 Apply(
@0-9 Apply(
@0-1 NumLiteral(
"1",
),
[
@2-8 Apply(
@3-4 SpaceAfter(
NumLiteral(
"0",
),
[
LineComment(
"",
),
],
),
[
LineComment(
"",
),
@7-8 Identifier {
ident: "f",
},
],
Whitespace,
),
[
@7-8 Identifier {
ident: "f",
},
],
),
],
ParensAndCommas,
),
[
@10-16 Apply(
@11-12 SpaceAfter(
NumLiteral(
@ -55,8 +61,10 @@
ident: "f",
},
],
Whitespace,
),
],
Whitespace,
),
@18-19 BoundVariable(
"f",

View file

@ -34,6 +34,7 @@
),
),
],
Whitespace,
),
@12-13 Apply(
"",

View file

@ -42,12 +42,14 @@
),
),
],
Whitespace,
),
[
@12-13 Identifier {
ident: "f",
},
],
Whitespace,
),
@14-15 Apply(
"",

View file

@ -33,8 +33,10 @@
"0",
),
],
Whitespace,
),
],
Whitespace,
),
@15-16 BoundVariable(
"f",

View file

@ -36,6 +36,7 @@
"2",
),
],
Whitespace,
),
@12-13 Apply(
"",

View file

@ -5,44 +5,42 @@
"0",
),
[
@4-7 ParensAround(
Defs(
Defs {
tags: [
EitherIndex(2147483648),
],
regions: [
@4-5,
],
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(
@4-5 Num(
"1",
),
@4-7 Defs(
Defs {
tags: [
EitherIndex(2147483648),
],
regions: [
@4-5,
],
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(
@4-5 Num(
"1",
),
],
),
],
},
@6-7 SpaceBefore(
Var {
module_name: "",
ident: "d",
},
@6-7 SpaceBefore(
Var {
module_name: "",
ident: "d",
},
[
Newline,
],
),
[
Newline,
],
),
),
],
Space,
ParensAndCommas,
),
),
@0-1 Negate,

View file

@ -32,6 +32,7 @@
],
),
],
Whitespace,
),
],
@14-15 Var {

View file

@ -25,6 +25,7 @@
ident: "x",
},
],
Whitespace,
),
@7-8 BoundVariable(
"i",

View file

@ -23,6 +23,7 @@
],
),
],
Whitespace,
),
[
Newline,

View file

@ -26,6 +26,7 @@
ident: "it",
},
],
Whitespace,
),
@12-22 Apply(
@12-14 Var {

View file

@ -20,6 +20,7 @@
ident: "m",
},
],
Whitespace,
),
[
Newline,

View file

@ -28,6 +28,7 @@
ident: "a",
},
],
Whitespace,
),
],
},

View file

@ -1,4 +1,3 @@
1
(i, p #
) : f
1(i, p) #
: f
n

View file

@ -16,29 +16,26 @@
type_defs: [],
value_defs: [
Annotation(
@0-1 Apply(
@0-8 Apply(
@0-1 NumLiteral(
"1",
),
[
@1-8 Tuple(
@2-3 Identifier {
ident: "i",
},
@4-5 SpaceAfter(
Identifier {
ident: "p",
},
[
@2-3 Identifier {
ident: "i",
},
@4-5 SpaceAfter(
Identifier {
ident: "p",
},
[
LineComment(
"",
),
],
LineComment(
"",
),
],
),
],
ParensAndCommas,
),
@9-10 BoundVariable(
"f",

View file

@ -37,6 +37,7 @@
],
),
],
Whitespace,
),
@7-8 BoundVariable(
"s",

View file

@ -23,6 +23,7 @@
},
),
],
Whitespace,
),
[
Newline,

View file

@ -43,11 +43,13 @@
ident: "ry",
},
],
Whitespace,
),
@47-48 Underscore(
"",
),
],
Whitespace,
),
[
Newline,

View file

@ -32,6 +32,7 @@
),
),
],
Whitespace,
),
],
},

View file

@ -37,6 +37,7 @@
ident: "a",
},
],
Whitespace,
),
@10-11 BoundVariable(
"t",

View file

@ -45,6 +45,7 @@
"K",
),
],
Whitespace,
),
[
LineComment(

View file

@ -29,6 +29,7 @@
ident: "code",
},
],
Whitespace,
),
@30-33 Var {
module_name: "",

View file

@ -29,6 +29,7 @@
ident: "code",
},
],
Whitespace,
),
@29-32 Var {
module_name: "",

View file

@ -1,2 +1,2 @@
1 (0 0) f : f
1(0(0)) f : f
i

View file

@ -17,30 +17,37 @@
type_defs: [],
value_defs: [
Annotation(
@0-1 Apply(
@0-1 NumLiteral(
"1",
@0-10 Apply(
@0-10 Apply(
@0-1 NumLiteral(
"1",
),
[
@2-9 Apply(
@3-4 NumLiteral(
"0",
),
[
@5-6 SpaceAfter(
NumLiteral(
"0",
),
[
Newline,
],
),
],
ParensAndCommas,
),
],
ParensAndCommas,
),
[
@2-9 Apply(
@3-4 NumLiteral(
"0",
),
[
@5-6 SpaceAfter(
NumLiteral(
"0",
),
[
Newline,
],
),
],
),
@10-11 Identifier {
ident: "f",
},
],
Whitespace,
),
@12-13 BoundVariable(
"f",

View file

@ -28,6 +28,7 @@
),
),
],
Whitespace,
),
@8-15 Apply(
@8-9 Var {

View file

@ -50,6 +50,7 @@
"",
),
],
Whitespace,
),
@11-19 Apply(
@11-15 Tag(
@ -79,6 +80,7 @@
ident: "y",
},
],
Whitespace,
),
@31-39 Apply(
@31-35 Tag(
@ -108,6 +110,7 @@
"",
),
],
Whitespace,
),
@51-59 Apply(
@51-55 Tag(
@ -161,6 +164,7 @@
"",
),
],
Whitespace,
),
@90-98 Apply(
@90-94 Tag(
@ -174,8 +178,10 @@
ident: "y",
},
],
Whitespace,
),
],
Whitespace,
),
@102-128 Apply(
@102-106 Tag(

View file

@ -23,6 +23,7 @@
[],
),
],
Whitespace,
),
[
Newline,

View file

@ -37,6 +37,14 @@ mod test_fmt {
)
}
fn pattern_formats_same(input: &str) {
Input::Pattern(input.trim()).check_invariants(
check_formatting(input.trim()),
true,
Some(false),
);
}
fn fmt_module_and_defs<'a>(
arena: &Bump,
src: &str,
@ -3497,6 +3505,33 @@ mod test_fmt {
));
}
#[test]
fn zero_arg_application_with_parens() {
expr_formats_same(indoc!(
r"
a()
"
));
}
#[test]
fn try_then_application_with_parens() {
expr_formats_same(indoc!(
r"
try something!(arg)
"
));
}
#[test]
fn dbg_then_application_with_parens() {
expr_formats_same(indoc!(
r"
dbg something!(arg)
"
));
}
#[test]
fn single_line_application_with_parens() {
expr_formats_same(indoc!(
@ -3885,6 +3920,40 @@ mod test_fmt {
));
}
#[test]
fn multi_line_when_condition_2_pnc() {
expr_formats_same(indoc!(
r"
when
# this is quite complicated
complexFunction(a, b, c)
# Watch out
is
Complex(x, y) ->
simplify(x, y)
Simple(z) ->
z
"
));
}
#[test]
fn anthony_testing() {
expr_formats_same(indoc!(
r"
when alter (Ok value) is
Ok newValue ->
bucket = listGetUnsafe buckets bucketIndex
newData = List.set data (Num.toU64 bucket.dataIndex) (key, newValue)
@Dict { buckets, data: newData, maxBucketCapacity, maxLoadFactor, shifts }
Err Missing ->
removeBucket (@Dict { buckets, data, maxBucketCapacity, maxLoadFactor, shifts }) bucketIndex
"
));
}
#[test]
fn multi_line_when_condition_3() {
expr_formats_to(
@ -6238,4 +6307,24 @@ mod test_fmt {
// "
// ));
// }
#[test]
fn pattern_tag_apply_with_whitespace_single_arg() {
pattern_formats_same(indoc!("Ok a"));
}
#[test]
fn pattern_tag_apply_with_pnc_single_arg() {
pattern_formats_same(indoc!("Ok(a)"));
}
#[test]
fn pattern_tag_apply_with_whitespace_multi_arg() {
pattern_formats_same(indoc!("Ok a b"));
}
#[test]
fn pattern_tag_apply_with_pnc_multi_arg() {
pattern_formats_same(indoc!("Ok(a, b)"));
}
}

View file

@ -33,6 +33,9 @@ mod test_snapshots {
(full => $input:expr) => {
Input::Full($input)
};
(pattern => $input:expr) => {
Input::Pattern($input)
};
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@ -324,6 +327,8 @@ mod test_snapshots {
pass/apply_record_parens_newline_field.expr,
pass/apply_tag.expr,
pass/apply_tag_pnc.expr,
pass/apply_tag_single_arg_pnc.pattern,
pass/apply_tag_single_arg_whitespace.pattern,
pass/apply_three_args.expr,
pass/apply_tuple_ext_parens_ty.expr,
pass/apply_two_args.expr,
@ -450,7 +455,7 @@ mod test_snapshots {
pass/if_in_record_field_opt_pat.expr,
pass/if_newline_then_negate_else_recordupdater.expr,
pass/if_then_weird_indent.expr,
pass/implements_after_parens_comment.expr,
pass/implements_after_comment_with_newline.expr,
pass/implements_annotation_comment.expr,
pass/implements_in_pat_after_comment.expr,
pass/implements_newline_in_fn_ty.expr,
@ -859,8 +864,6 @@ mod test_snapshots {
Err(err) => Err(format!("{err:?}")),
};
println!("{:?}", result);
if expect == TestExpectation::Pass {
let tokens = roc_parse::highlight::highlight(&source);
for token in tokens {