mirror of
https://github.com/roc-lang/roc.git
synced 2025-12-04 00:55:00 +00:00
parent
f98a75fc51
commit
2541a09273
6 changed files with 198 additions and 86 deletions
|
|
@ -38,7 +38,7 @@ pub fn fmt_collection<'a, 'buf, T: ExtractSpaces<'a> + Formattable>(
|
|||
let braces_indent = indent;
|
||||
let item_indent = braces_indent + INDENT;
|
||||
if newline == Newlines::Yes {
|
||||
buf.newline();
|
||||
buf.ensure_ends_with_newline();
|
||||
}
|
||||
buf.indent(braces_indent);
|
||||
buf.push(start);
|
||||
|
|
|
|||
|
|
@ -204,6 +204,45 @@ impl<'a> Formattable for ValueDef<'a> {
|
|||
use roc_parse::ast::ValueDef::*;
|
||||
match self {
|
||||
Annotation(loc_pattern, loc_annotation) => {
|
||||
fmt_annotation(loc_pattern, buf, indent, loc_annotation, newlines);
|
||||
}
|
||||
Body(loc_pattern, loc_expr) => {
|
||||
fmt_body(buf, &loc_pattern.value, &loc_expr.value, indent);
|
||||
}
|
||||
Dbg { condition, .. } => fmt_dbg_in_def(buf, condition, self.is_multiline(), indent),
|
||||
Expect { condition, .. } => fmt_expect(buf, condition, self.is_multiline(), indent),
|
||||
ExpectFx { condition, .. } => {
|
||||
fmt_expect_fx(buf, condition, self.is_multiline(), indent)
|
||||
}
|
||||
AnnotatedBody {
|
||||
ann_pattern,
|
||||
ann_type,
|
||||
comment,
|
||||
body_pattern,
|
||||
body_expr,
|
||||
} => {
|
||||
fmt_annotation(ann_pattern, buf, indent, ann_type, newlines);
|
||||
|
||||
if let Some(comment_str) = comment {
|
||||
buf.push_str(" #");
|
||||
buf.spaces(1);
|
||||
buf.push_str(comment_str.trim());
|
||||
}
|
||||
|
||||
buf.newline();
|
||||
fmt_body(buf, &body_pattern.value, &body_expr.value, indent);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn fmt_annotation(
|
||||
loc_pattern: &Loc<Pattern>,
|
||||
buf: &mut Buf,
|
||||
indent: u16,
|
||||
loc_annotation: &Loc<TypeAnnotation>,
|
||||
newlines: Newlines,
|
||||
) {
|
||||
loc_pattern.format(buf, indent);
|
||||
buf.indent(indent);
|
||||
|
||||
|
|
@ -226,12 +265,7 @@ impl<'a> Formattable for ValueDef<'a> {
|
|||
if should_outdent {
|
||||
match loc_annotation.value {
|
||||
TypeAnnotation::SpaceBefore(sub_def, _) => {
|
||||
sub_def.format_with_options(
|
||||
buf,
|
||||
Parens::NotNeeded,
|
||||
Newlines::No,
|
||||
indent,
|
||||
);
|
||||
sub_def.format_with_options(buf, Parens::NotNeeded, Newlines::No, indent);
|
||||
}
|
||||
_ => {
|
||||
loc_annotation.format_with_options(
|
||||
|
|
@ -243,79 +277,13 @@ impl<'a> Formattable for ValueDef<'a> {
|
|||
}
|
||||
}
|
||||
} else {
|
||||
loc_annotation.format_with_options(
|
||||
buf,
|
||||
Parens::NotNeeded,
|
||||
newlines,
|
||||
indent + INDENT,
|
||||
);
|
||||
loc_annotation.format_with_options(buf, Parens::NotNeeded, newlines, indent + INDENT);
|
||||
}
|
||||
} else {
|
||||
buf.spaces(1);
|
||||
buf.push(':');
|
||||
buf.spaces(1);
|
||||
loc_annotation.format_with_options(
|
||||
buf,
|
||||
Parens::NotNeeded,
|
||||
Newlines::No,
|
||||
indent,
|
||||
);
|
||||
}
|
||||
}
|
||||
Body(loc_pattern, loc_expr) => {
|
||||
fmt_body(buf, &loc_pattern.value, &loc_expr.value, indent);
|
||||
}
|
||||
Dbg { condition, .. } => fmt_dbg_in_def(buf, condition, self.is_multiline(), indent),
|
||||
Expect { condition, .. } => fmt_expect(buf, condition, self.is_multiline(), indent),
|
||||
ExpectFx { condition, .. } => {
|
||||
fmt_expect_fx(buf, condition, self.is_multiline(), indent)
|
||||
}
|
||||
AnnotatedBody {
|
||||
ann_pattern,
|
||||
ann_type,
|
||||
comment,
|
||||
body_pattern,
|
||||
body_expr,
|
||||
} => {
|
||||
let is_type_multiline = ann_type.is_multiline();
|
||||
let is_type_function = matches!(
|
||||
ann_type.value,
|
||||
TypeAnnotation::Function(..)
|
||||
| TypeAnnotation::SpaceBefore(TypeAnnotation::Function(..), ..)
|
||||
| TypeAnnotation::SpaceAfter(TypeAnnotation::Function(..), ..)
|
||||
);
|
||||
|
||||
let next_indent = if is_type_multiline {
|
||||
indent + INDENT
|
||||
} else {
|
||||
indent
|
||||
};
|
||||
|
||||
ann_pattern.format(buf, indent);
|
||||
buf.push_str(" :");
|
||||
|
||||
if is_type_multiline && is_type_function {
|
||||
ann_type.format_with_options(
|
||||
buf,
|
||||
Parens::NotNeeded,
|
||||
Newlines::Yes,
|
||||
next_indent,
|
||||
);
|
||||
} else {
|
||||
buf.spaces(1);
|
||||
ann_type.format(buf, indent);
|
||||
}
|
||||
|
||||
if let Some(comment_str) = comment {
|
||||
buf.push_str(" #");
|
||||
buf.spaces(1);
|
||||
buf.push_str(comment_str.trim());
|
||||
}
|
||||
|
||||
buf.newline();
|
||||
fmt_body(buf, &body_pattern.value, &body_expr.value, indent);
|
||||
}
|
||||
}
|
||||
loc_annotation.format_with_options(buf, Parens::NotNeeded, Newlines::No, indent);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,7 @@
|
|||
table :
|
||||
{
|
||||
height : Pixels,
|
||||
}
|
||||
-> Table
|
||||
table = \{ height } -> crash "not implemented"
|
||||
table
|
||||
|
|
@ -0,0 +1,131 @@
|
|||
Defs(
|
||||
Defs {
|
||||
tags: [
|
||||
Index(2147483649),
|
||||
],
|
||||
regions: [
|
||||
@0-89,
|
||||
],
|
||||
space_before: [
|
||||
Slice(start = 0, length = 0),
|
||||
],
|
||||
space_after: [
|
||||
Slice(start = 0, length = 0),
|
||||
],
|
||||
spaces: [],
|
||||
type_defs: [],
|
||||
value_defs: [
|
||||
Annotation(
|
||||
@0-5 Identifier(
|
||||
"table",
|
||||
),
|
||||
@8-44 Function(
|
||||
[
|
||||
@8-35 Record {
|
||||
fields: [
|
||||
@14-29 SpaceBefore(
|
||||
SpaceAfter(
|
||||
RequiredValue(
|
||||
@14-20 "height",
|
||||
[],
|
||||
@23-29 Apply(
|
||||
"",
|
||||
"Pixels",
|
||||
[],
|
||||
),
|
||||
),
|
||||
[
|
||||
Newline,
|
||||
],
|
||||
),
|
||||
[
|
||||
Newline,
|
||||
],
|
||||
),
|
||||
],
|
||||
ext: None,
|
||||
},
|
||||
],
|
||||
@39-44 Apply(
|
||||
"",
|
||||
"Table",
|
||||
[],
|
||||
),
|
||||
),
|
||||
),
|
||||
AnnotatedBody {
|
||||
ann_pattern: @0-5 Identifier(
|
||||
"table",
|
||||
),
|
||||
ann_type: @8-44 Function(
|
||||
[
|
||||
@8-35 Record {
|
||||
fields: [
|
||||
@14-29 SpaceBefore(
|
||||
SpaceAfter(
|
||||
RequiredValue(
|
||||
@14-20 "height",
|
||||
[],
|
||||
@23-29 Apply(
|
||||
"",
|
||||
"Pixels",
|
||||
[],
|
||||
),
|
||||
),
|
||||
[
|
||||
Newline,
|
||||
],
|
||||
),
|
||||
[
|
||||
Newline,
|
||||
],
|
||||
),
|
||||
],
|
||||
ext: None,
|
||||
},
|
||||
],
|
||||
@39-44 Apply(
|
||||
"",
|
||||
"Table",
|
||||
[],
|
||||
),
|
||||
),
|
||||
comment: None,
|
||||
body_pattern: @45-50 Identifier(
|
||||
"table",
|
||||
),
|
||||
body_expr: @53-89 Closure(
|
||||
[
|
||||
@54-62 RecordDestructure(
|
||||
[
|
||||
@55-61 Identifier(
|
||||
"height",
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
@66-89 Apply(
|
||||
@66-71 Crash,
|
||||
[
|
||||
@72-89 Str(
|
||||
PlainLine(
|
||||
"not implemented",
|
||||
),
|
||||
),
|
||||
],
|
||||
Space,
|
||||
),
|
||||
),
|
||||
},
|
||||
],
|
||||
},
|
||||
@90-95 SpaceBefore(
|
||||
Var {
|
||||
module_name: "",
|
||||
ident: "table",
|
||||
},
|
||||
[
|
||||
Newline,
|
||||
],
|
||||
),
|
||||
)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
table : {
|
||||
height : Pixels
|
||||
} -> Table
|
||||
table = \{height} -> crash "not implemented"
|
||||
table
|
||||
|
|
@ -311,6 +311,7 @@ mod test_snapshots {
|
|||
pass/expect_fx.moduledefs,
|
||||
pass/extra_newline_in_parens.expr,
|
||||
pass/float_with_underscores.expr,
|
||||
pass/fn_with_record_arg.expr,
|
||||
pass/full_app_header.header,
|
||||
pass/full_app_header_trailing_commas.header,
|
||||
pass/function_effect_types.header,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue