mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-04 04:08:19 +00:00
Merge pull request #5016 from joshuawarner32/fix-collection-newlines
Fix formatting of initial newlines in collections
This commit is contained in:
commit
57c3840926
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,63 +204,7 @@ impl<'a> Formattable for ValueDef<'a> {
|
|||
use roc_parse::ast::ValueDef::*;
|
||||
match self {
|
||||
Annotation(loc_pattern, loc_annotation) => {
|
||||
loc_pattern.format(buf, indent);
|
||||
buf.indent(indent);
|
||||
|
||||
if loc_annotation.is_multiline() {
|
||||
buf.push_str(" :");
|
||||
buf.spaces(1);
|
||||
|
||||
let should_outdent = match loc_annotation.value {
|
||||
TypeAnnotation::SpaceBefore(sub_def, spaces) => match sub_def {
|
||||
TypeAnnotation::Record { .. } | TypeAnnotation::TagUnion { .. } => {
|
||||
let is_only_newlines = spaces.iter().all(|s| s.is_newline());
|
||||
is_only_newlines && sub_def.is_multiline()
|
||||
}
|
||||
_ => false,
|
||||
},
|
||||
TypeAnnotation::Record { .. } | TypeAnnotation::TagUnion { .. } => true,
|
||||
_ => false,
|
||||
};
|
||||
|
||||
if should_outdent {
|
||||
match loc_annotation.value {
|
||||
TypeAnnotation::SpaceBefore(sub_def, _) => {
|
||||
sub_def.format_with_options(
|
||||
buf,
|
||||
Parens::NotNeeded,
|
||||
Newlines::No,
|
||||
indent,
|
||||
);
|
||||
}
|
||||
_ => {
|
||||
loc_annotation.format_with_options(
|
||||
buf,
|
||||
Parens::NotNeeded,
|
||||
Newlines::No,
|
||||
indent,
|
||||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
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,
|
||||
);
|
||||
}
|
||||
fmt_annotation(loc_pattern, buf, indent, loc_annotation, newlines);
|
||||
}
|
||||
Body(loc_pattern, loc_expr) => {
|
||||
fmt_body(buf, &loc_pattern.value, &loc_expr.value, indent);
|
||||
|
@ -277,34 +221,7 @@ impl<'a> Formattable for ValueDef<'a> {
|
|||
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);
|
||||
}
|
||||
fmt_annotation(ann_pattern, buf, indent, ann_type, newlines);
|
||||
|
||||
if let Some(comment_str) = comment {
|
||||
buf.push_str(" #");
|
||||
|
@ -319,6 +236,57 @@ impl<'a> Formattable for ValueDef<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if loc_annotation.is_multiline() {
|
||||
buf.push_str(" :");
|
||||
buf.spaces(1);
|
||||
|
||||
let should_outdent = match loc_annotation.value {
|
||||
TypeAnnotation::SpaceBefore(sub_def, spaces) => match sub_def {
|
||||
TypeAnnotation::Record { .. } | TypeAnnotation::TagUnion { .. } => {
|
||||
let is_only_newlines = spaces.iter().all(|s| s.is_newline());
|
||||
is_only_newlines && sub_def.is_multiline()
|
||||
}
|
||||
_ => false,
|
||||
},
|
||||
TypeAnnotation::Record { .. } | TypeAnnotation::TagUnion { .. } => true,
|
||||
_ => false,
|
||||
};
|
||||
|
||||
if should_outdent {
|
||||
match loc_annotation.value {
|
||||
TypeAnnotation::SpaceBefore(sub_def, _) => {
|
||||
sub_def.format_with_options(buf, Parens::NotNeeded, Newlines::No, indent);
|
||||
}
|
||||
_ => {
|
||||
loc_annotation.format_with_options(
|
||||
buf,
|
||||
Parens::NotNeeded,
|
||||
Newlines::No,
|
||||
indent,
|
||||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
fn fmt_dbg_in_def<'a, 'buf>(
|
||||
buf: &mut Buf<'buf>,
|
||||
condition: &'a Loc<Expr<'a>>,
|
||||
|
|
|
@ -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