Add tags.

This commit is contained in:
Miles Johnson 2023-04-08 16:21:38 -07:00
parent 597458ff9c
commit fb045a2d9c
3 changed files with 13 additions and 27 deletions

View File

@ -25,9 +25,6 @@ impl Default for RgbColors {
} }
} }
/// Function to format a message before rendering.
pub type MessageFormatter = dyn Fn(String) -> String;
/** /**
Create a custom [`MietteHandler`] from options. Create a custom [`MietteHandler`] from options.
@ -58,7 +55,6 @@ pub struct MietteHandlerOpts {
pub(crate) context_lines: Option<usize>, pub(crate) context_lines: Option<usize>,
pub(crate) tab_width: Option<usize>, pub(crate) tab_width: Option<usize>,
pub(crate) with_cause_chain: Option<bool>, pub(crate) with_cause_chain: Option<bool>,
pub(crate) message_formatter: Option<Box<MessageFormatter>>,
} }
impl MietteHandlerOpts { impl MietteHandlerOpts {
@ -168,11 +164,6 @@ impl MietteHandlerOpts {
self self
} }
pub fn message_formatter<F: MessageFormatter>(mut self, formatter: F) -> Self {
self.message_formatter = Some(Box::new(formatter));
self
}
/// Builds a [`MietteHandler`] from this builder. /// Builds a [`MietteHandler`] from this builder.
pub fn build(self) -> MietteHandler { pub fn build(self) -> MietteHandler {
let graphical = self.is_graphical(); let graphical = self.is_graphical();
@ -221,7 +212,11 @@ impl MietteHandlerOpts {
} else { } else {
ThemeStyles::none() ThemeStyles::none()
}; };
let theme = self.theme.unwrap_or(GraphicalTheme { characters, styles }); let theme = self.theme.unwrap_or(GraphicalTheme {
characters,
styles,
tags: std::collections::HashMap::new(),
});
let mut handler = GraphicalReportHandler::new() let mut handler = GraphicalReportHandler::new()
.with_width(width) .with_width(width)
.with_links(linkify) .with_links(linkify)
@ -242,9 +237,6 @@ impl MietteHandlerOpts {
if let Some(w) = self.tab_width { if let Some(w) = self.tab_width {
handler = handler.tab_width(w); handler = handler.tab_width(w);
} }
if let Some(formatter) = self.message_formatter {
handler = handler.with_message_formatter(formatter);
}
MietteHandler { MietteHandler {
inner: Box::new(handler), inner: Box::new(handler),
} }

View File

@ -6,9 +6,7 @@ use unicode_width::UnicodeWidthChar;
use crate::diagnostic_chain::DiagnosticChain; use crate::diagnostic_chain::DiagnosticChain;
use crate::handlers::theme::*; use crate::handlers::theme::*;
use crate::protocol::{Diagnostic, Severity}; use crate::protocol::{Diagnostic, Severity};
use crate::{ use crate::{LabeledSpan, MietteError, ReportHandler, SourceCode, SourceSpan, SpanContents};
LabeledSpan, MessageFormatter, MietteError, ReportHandler, SourceCode, SourceSpan, SpanContents,
};
/** /**
A [`ReportHandler`] that displays a given [`Report`](crate::Report) in a A [`ReportHandler`] that displays a given [`Report`](crate::Report) in a
@ -32,7 +30,6 @@ pub struct GraphicalReportHandler {
pub(crate) context_lines: usize, pub(crate) context_lines: usize,
pub(crate) tab_width: usize, pub(crate) tab_width: usize,
pub(crate) with_cause_chain: bool, pub(crate) with_cause_chain: bool,
pub(crate) message_formatter: Option<Box<MessageFormatter>>,
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
@ -54,7 +51,6 @@ impl GraphicalReportHandler {
context_lines: 1, context_lines: 1,
tab_width: 4, tab_width: 4,
with_cause_chain: true, with_cause_chain: true,
message_formatter: None,
} }
} }
@ -68,7 +64,6 @@ impl GraphicalReportHandler {
context_lines: 1, context_lines: 1,
tab_width: 4, tab_width: 4,
with_cause_chain: true, with_cause_chain: true,
message_formatter: None,
} }
} }
@ -138,11 +133,6 @@ impl GraphicalReportHandler {
self.context_lines = lines; self.context_lines = lines;
self self
} }
pub fn with_message_formatter<F: MessageFormatter>(mut self, formatter: F) -> Self {
self.message_formatter = Some(Box::new(formatter));
self
}
} }
impl Default for GraphicalReportHandler { impl Default for GraphicalReportHandler {
@ -226,9 +216,6 @@ impl GraphicalReportHandler {
.subsequent_indent(&rest_indent); .subsequent_indent(&rest_indent);
let mut message = diagnostic.to_string(); let mut message = diagnostic.to_string();
if let Some(formatter) = self.message_formatter {
message = formatter(message);
}
writeln!(f, "{}", textwrap::fill(&message, opts))?; writeln!(f, "{}", textwrap::fill(&message, opts))?;

View File

@ -1,5 +1,6 @@
use is_terminal::IsTerminal; use is_terminal::IsTerminal;
use owo_colors::Style; use owo_colors::Style;
use std::collections::HashMap;
/** /**
Theme used by [`GraphicalReportHandler`](crate::GraphicalReportHandler) to Theme used by [`GraphicalReportHandler`](crate::GraphicalReportHandler) to
@ -18,6 +19,8 @@ pub struct GraphicalTheme {
pub characters: ThemeCharacters, pub characters: ThemeCharacters,
/// Styles to be used for painting. /// Styles to be used for painting.
pub styles: ThemeStyles, pub styles: ThemeStyles,
/// Tags to be used for styling.
pub tags: HashMap<String, Style>,
} }
impl GraphicalTheme { impl GraphicalTheme {
@ -26,6 +29,7 @@ impl GraphicalTheme {
Self { Self {
characters: ThemeCharacters::ascii(), characters: ThemeCharacters::ascii(),
styles: ThemeStyles::ansi(), styles: ThemeStyles::ansi(),
tags: HashMap::new(),
} }
} }
@ -41,6 +45,7 @@ impl GraphicalTheme {
Self { Self {
characters: ThemeCharacters::unicode(), characters: ThemeCharacters::unicode(),
styles: ThemeStyles::ansi(), styles: ThemeStyles::ansi(),
tags: HashMap::new(),
} }
} }
@ -50,6 +55,7 @@ impl GraphicalTheme {
Self { Self {
characters: ThemeCharacters::unicode(), characters: ThemeCharacters::unicode(),
styles: ThemeStyles::none(), styles: ThemeStyles::none(),
tags: HashMap::new(),
} }
} }
@ -62,6 +68,7 @@ impl GraphicalTheme {
Self { Self {
characters: ThemeCharacters::ascii(), characters: ThemeCharacters::ascii(),
styles: ThemeStyles::none(), styles: ThemeStyles::none(),
tags: HashMap::new(),
} }
} }
} }