fix formatting of ctor patterns in errors

This commit is contained in:
Folkert 2020-04-23 20:30:42 +02:00
parent b2a713915b
commit c54d61c854
2 changed files with 22 additions and 4 deletions

View file

@ -122,6 +122,14 @@ pub fn unhandled_patterns_to_doc_block<'b>(
fn pattern_to_doc<'b>( fn pattern_to_doc<'b>(
alloc: &'b RocDocAllocator<'b>, alloc: &'b RocDocAllocator<'b>,
pattern: roc_mono::pattern::Pattern, pattern: roc_mono::pattern::Pattern,
) -> RocDocBuilder<'b> {
pattern_to_doc_help(alloc, pattern, false)
}
fn pattern_to_doc_help<'b>(
alloc: &'b RocDocAllocator<'b>,
pattern: roc_mono::pattern::Pattern,
in_type_param: bool,
) -> RocDocBuilder<'b> { ) -> RocDocBuilder<'b> {
use roc_mono::pattern::Literal::*; use roc_mono::pattern::Literal::*;
use roc_mono::pattern::Pattern::*; use roc_mono::pattern::Pattern::*;
@ -137,7 +145,10 @@ fn pattern_to_doc<'b>(
Str(s) => alloc.string(s.into()), Str(s) => alloc.string(s.into()),
}, },
Ctor(union, tag_id, args) => { Ctor(union, tag_id, args) => {
let arg_docs = args.into_iter().map(|v| pattern_to_doc(alloc, v)); let has_args = !args.is_empty();
let arg_docs = args
.into_iter()
.map(|v| pattern_to_doc_help(alloc, v, true));
let tag = &union.alternatives[tag_id.0 as usize]; let tag = &union.alternatives[tag_id.0 as usize];
let tag_name = tag.name.clone(); let tag_name = tag.name.clone();
@ -147,7 +158,14 @@ fn pattern_to_doc<'b>(
let docs = std::iter::once(alloc.tag_name(tag_name)).chain(arg_docs); let docs = std::iter::once(alloc.tag_name(tag_name)).chain(arg_docs);
alloc.intersperse(docs, alloc.space()) if in_type_param && has_args {
alloc
.text("(")
.append(alloc.intersperse(docs, alloc.space()))
.append(")")
} else {
alloc.intersperse(docs, alloc.space())
}
} }
} }
} }

View file

@ -2299,7 +2299,7 @@ mod test_reporting {
Other possibilities include: Other possibilities include:
#Record Just _ _ #Record (Just _) _
I would have to crash if I saw one of those! Add branches for them! I would have to crash if I saw one of those! Add branches for them!
"# "#
@ -2329,7 +2329,7 @@ mod test_reporting {
Other possibilities include: Other possibilities include:
Record Just _ _ Record (Just _) _
I would have to crash if I saw one of those! Add branches for them! I would have to crash if I saw one of those! Add branches for them!
"# "#