tamer: diagnostic::report: Hoist gutter output into Section

The `Section` itself is now responsible for outputting the gutter, which
puts us in a position to be able to apply consistent formatting without
having to propagate width data to every line variant.
main
Mike Gerwitz 2022-04-28 22:59:13 -04:00
parent 4e03a367a5
commit 5744e08984
1 changed files with 25 additions and 12 deletions

View File

@ -351,13 +351,9 @@ impl<'d> Display for Section<'d> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, " {heading}\n", heading = self.heading)?;
for line in self.body.iter() {
// Let each line have control over its own newline so that it
// can fully suppress itself if it's not relevant.
line.fmt(f)?;
}
Ok(())
self.body.iter().try_for_each(|line| {
write!(f, " {delim}{line}\n", delim = line.gutter_delim())
})
}
}
@ -497,7 +493,7 @@ impl<'d> SpanLabel<'d> {
impl<'d> Display for SpanLabel<'d> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let Self(level, label) = self;
write!(f, " = {level}: {label}")
write!(f, " {level}: {label}")
}
}
@ -511,6 +507,23 @@ enum SectionLine<'d> {
}
impl<'d> SectionLine<'d> {
/// Delimiter used to separate the gutter from the body.
///
/// For example:
///
/// ```text
/// |
/// 12 | source line
/// | ^^^^^^
/// = note: notice the delim change for this footnote
/// ```
fn gutter_delim(&self) -> char {
match self {
Self::Footnote(..) => '=',
_ => '|',
}
}
fn into_footnote(self) -> Option<Self> {
match self {
Self::SourceLinePadding => None,
@ -527,10 +540,10 @@ impl<'d> SectionLine<'d> {
impl<'d> Display for SectionLine<'d> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::SourceLinePadding => write!(f, " |\n"),
Self::SourceLine(line) => write!(f, " |{line}\n"),
Self::SourceLineMark(mark) => write!(f, " |{mark}\n"),
Self::Footnote(label) => write!(f, "{label}\n"),
Self::SourceLinePadding => Ok(()),
Self::SourceLine(line) => line.fmt(f),
Self::SourceLineMark(mark) => mark.fmt(f),
Self::Footnote(label) => label.fmt(f),
}
}
}