mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-29 23:04:49 +00:00
Use Collection in Expr::TagUnion
This commit is contained in:
parent
d63405d824
commit
1fabc64fdf
6 changed files with 21 additions and 49 deletions
|
@ -428,7 +428,7 @@ pub fn to_type2<'a>(
|
||||||
Type2::Record(field_types, ext_type)
|
Type2::Record(field_types, ext_type)
|
||||||
}
|
}
|
||||||
TagUnion { tags, ext, .. } => {
|
TagUnion { tags, ext, .. } => {
|
||||||
let tag_types_vec = can_tags(env, scope, references, tags, region);
|
let tag_types_vec = can_tags(env, scope, references, tags.items, region);
|
||||||
|
|
||||||
let tag_types = PoolVec::with_capacity(tag_types_vec.len() as u32, env.pool);
|
let tag_types = PoolVec::with_capacity(tag_types_vec.len() as u32, env.pool);
|
||||||
|
|
||||||
|
|
|
@ -417,7 +417,7 @@ fn can_annotation_help(
|
||||||
TagUnion { tags, ext, .. } => {
|
TagUnion { tags, ext, .. } => {
|
||||||
let tag_types = can_tags(
|
let tag_types = can_tags(
|
||||||
env,
|
env,
|
||||||
tags,
|
tags.items,
|
||||||
region,
|
region,
|
||||||
scope,
|
scope,
|
||||||
var_store,
|
var_store,
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::spaces::{fmt_comments_only, fmt_spaces, newline, NewlineAt, INDENT};
|
use crate::spaces::{fmt_comments_only, fmt_spaces, newline, NewlineAt, INDENT};
|
||||||
use bumpalo::collections::String;
|
use bumpalo::collections::String;
|
||||||
use roc_parse::ast::{AssignedField, Collection, Expr, Tag, TypeAnnotation};
|
use roc_parse::ast::{AssignedField, Expr, Tag, TypeAnnotation};
|
||||||
use roc_region::all::Located;
|
use roc_region::all::Located;
|
||||||
|
|
||||||
/// Does an AST node need parens around it?
|
/// Does an AST node need parens around it?
|
||||||
|
@ -81,9 +81,9 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! format_sequence {
|
macro_rules! format_sequence {
|
||||||
($buf: expr, $indent:expr, $start:expr, $end:expr, $items:expr, $final_comments:expr, $newline:expr, $t:ident) => {
|
($buf: expr, $indent:expr, $start:expr, $end:expr, $items:expr, $newline:expr, $t:ident) => {
|
||||||
let is_multiline =
|
let is_multiline = $items.iter().any(|item| item.value.is_multiline())
|
||||||
$items.iter().any(|item| item.value.is_multiline()) || !$final_comments.is_empty();
|
|| !$items.final_comments.is_empty();
|
||||||
|
|
||||||
if is_multiline {
|
if is_multiline {
|
||||||
let braces_indent = $indent + INDENT;
|
let braces_indent = $indent + INDENT;
|
||||||
|
@ -138,7 +138,12 @@ macro_rules! format_sequence {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt_comments_only($buf, $final_comments.iter(), NewlineAt::Top, item_indent);
|
fmt_comments_only(
|
||||||
|
$buf,
|
||||||
|
$items.final_comments.iter(),
|
||||||
|
NewlineAt::Top,
|
||||||
|
item_indent,
|
||||||
|
);
|
||||||
newline($buf, braces_indent);
|
newline($buf, braces_indent);
|
||||||
$buf.push($end);
|
$buf.push($end);
|
||||||
} else {
|
} else {
|
||||||
|
@ -192,11 +197,7 @@ impl<'a> Formattable<'a> for TypeAnnotation<'a> {
|
||||||
fields.items.iter().any(|field| field.value.is_multiline())
|
fields.items.iter().any(|field| field.value.is_multiline())
|
||||||
}
|
}
|
||||||
|
|
||||||
TagUnion {
|
TagUnion { tags, ext } => {
|
||||||
tags,
|
|
||||||
ext,
|
|
||||||
final_comments: _,
|
|
||||||
} => {
|
|
||||||
match ext {
|
match ext {
|
||||||
Some(ann) if ann.value.is_multiline() => return true,
|
Some(ann) if ann.value.is_multiline() => return true,
|
||||||
_ => {}
|
_ => {}
|
||||||
|
@ -279,36 +280,16 @@ impl<'a> Formattable<'a> for TypeAnnotation<'a> {
|
||||||
BoundVariable(v) => buf.push_str(v),
|
BoundVariable(v) => buf.push_str(v),
|
||||||
Wildcard => buf.push('*'),
|
Wildcard => buf.push('*'),
|
||||||
|
|
||||||
TagUnion {
|
TagUnion { tags, ext } => {
|
||||||
tags,
|
format_sequence!(buf, indent, '[', ']', tags, newlines, Tag);
|
||||||
ext,
|
|
||||||
final_comments,
|
|
||||||
} => {
|
|
||||||
format_sequence!(buf, indent, '[', ']', tags, final_comments, newlines, Tag);
|
|
||||||
|
|
||||||
if let Some(loc_ext_ann) = *ext {
|
if let Some(loc_ext_ann) = *ext {
|
||||||
loc_ext_ann.value.format(buf, indent);
|
loc_ext_ann.value.format(buf, indent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Record {
|
Record { fields, ext } => {
|
||||||
fields:
|
format_sequence!(buf, indent, '{', '}', fields, newlines, AssignedField);
|
||||||
Collection {
|
|
||||||
items,
|
|
||||||
final_comments,
|
|
||||||
},
|
|
||||||
ext,
|
|
||||||
} => {
|
|
||||||
format_sequence!(
|
|
||||||
buf,
|
|
||||||
indent,
|
|
||||||
'{',
|
|
||||||
'}',
|
|
||||||
items,
|
|
||||||
final_comments,
|
|
||||||
newlines,
|
|
||||||
AssignedField
|
|
||||||
);
|
|
||||||
|
|
||||||
if let Some(loc_ext_ann) = *ext {
|
if let Some(loc_ext_ann) = *ext {
|
||||||
loc_ext_ann.value.format(buf, indent);
|
loc_ext_ann.value.format(buf, indent);
|
||||||
|
|
|
@ -235,16 +235,12 @@ fn generate_entry_doc<'a>(
|
||||||
|
|
||||||
fn type_to_docs(in_func_type_ann: bool, type_annotation: ast::TypeAnnotation) -> TypeAnnotation {
|
fn type_to_docs(in_func_type_ann: bool, type_annotation: ast::TypeAnnotation) -> TypeAnnotation {
|
||||||
match type_annotation {
|
match type_annotation {
|
||||||
ast::TypeAnnotation::TagUnion {
|
ast::TypeAnnotation::TagUnion { tags, ext } => {
|
||||||
tags,
|
|
||||||
ext,
|
|
||||||
final_comments: _,
|
|
||||||
} => {
|
|
||||||
let mut tags_to_render: Vec<Tag> = Vec::new();
|
let mut tags_to_render: Vec<Tag> = Vec::new();
|
||||||
|
|
||||||
let mut any_tags_are_private = false;
|
let mut any_tags_are_private = false;
|
||||||
|
|
||||||
for tag in tags {
|
for tag in tags.iter() {
|
||||||
match tag_to_doc(in_func_type_ann, tag.value) {
|
match tag_to_doc(in_func_type_ann, tag.value) {
|
||||||
None => {
|
None => {
|
||||||
any_tags_are_private = true;
|
any_tags_are_private = true;
|
||||||
|
|
|
@ -266,11 +266,10 @@ pub enum TypeAnnotation<'a> {
|
||||||
|
|
||||||
/// A tag union, e.g. `[
|
/// A tag union, e.g. `[
|
||||||
TagUnion {
|
TagUnion {
|
||||||
tags: &'a [Loc<Tag<'a>>],
|
|
||||||
/// The row type variable in an open tag union, e.g. the `a` in `[ Foo, Bar ]a`.
|
/// The row type variable in an open tag union, e.g. the `a` in `[ Foo, Bar ]a`.
|
||||||
/// This is None if it's a closed tag union like `[ Foo, Bar]`.
|
/// This is None if it's a closed tag union like `[ Foo, Bar]`.
|
||||||
ext: Option<&'a Loc<TypeAnnotation<'a>>>,
|
ext: Option<&'a Loc<TypeAnnotation<'a>>>,
|
||||||
final_comments: &'a [CommentOrNewline<'a>],
|
tags: Collection<'a, Loc<Tag<'a>>>,
|
||||||
},
|
},
|
||||||
|
|
||||||
/// The `*` type variable, e.g. in (List *)
|
/// The `*` type variable, e.g. in (List *)
|
||||||
|
|
|
@ -40,11 +40,7 @@ fn tag_union_type<'a>(min_indent: u16) -> impl Parser<'a, TypeAnnotation<'a>, ET
|
||||||
)))
|
)))
|
||||||
.parse(arena, state)?;
|
.parse(arena, state)?;
|
||||||
|
|
||||||
let result = TypeAnnotation::TagUnion {
|
let result = TypeAnnotation::TagUnion { tags, ext };
|
||||||
tags: tags.items,
|
|
||||||
ext,
|
|
||||||
final_comments: tags.final_comments,
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok((MadeProgress, result, state))
|
Ok((MadeProgress, result, state))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue