Skip to content

Commit

Permalink
Refactor write to store char spans in LabelInfo
Browse files Browse the repository at this point in the history
This way LabelInfo doesn't need to depend on the particular impl of Span. This also removes the redundancy in LabelInfo, because labels of the same file are stored in the same SourceGroup anyway.

This is in preparation for switching over to byte spans.
  • Loading branch information
VonTum authored and zesterer committed Mar 7, 2024
1 parent e3ff5e2 commit 3ad939a
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 112 deletions.
32 changes: 20 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,22 @@ impl<Id: fmt::Debug + Hash + PartialEq + Eq + ToOwned> Span for (Id, Range<usize
fn end(&self) -> usize { self.1.end }
}

/// A type that represents a labelled section of source code.
/// A type that represents the way a label should be displayed.
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct Label<S = Range<usize>> {
span: S,
struct LabelDisplay {
msg: Option<String>,
color: Option<Color>,
order: i32,
priority: i32,
}

/// A type that represents a labelled section of source code.
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct Label<S = Range<usize>> {
span: S,
display_info: LabelDisplay,
}

impl<S: Span> Label<S> {
/// Create a new [`Label`].
/// If the span is specified as a `Range<usize>` the numbers have to be zero-indexed character offsets.
Expand All @@ -93,22 +99,24 @@ impl<S: Span> Label<S> {

Self {
span,
msg: None,
color: None,
order: 0,
priority: 0,
display_info: LabelDisplay{
msg: None,
color: None,
order: 0,
priority: 0,
}
}
}

/// Give this label a message.
pub fn with_message<M: ToString>(mut self, msg: M) -> Self {
self.msg = Some(msg.to_string());
self.display_info.msg = Some(msg.to_string());
self
}

/// Give this label a highlight colour.
pub fn with_color(mut self, color: Color) -> Self {
self.color = Some(color);
self.display_info.color = Some(color);
self
}

Expand All @@ -123,7 +131,7 @@ impl<S: Span> Label<S> {
/// Additionally, multi-line labels are ordered before inline labels. You can use this function to override this
/// behaviour.
pub fn with_order(mut self, order: i32) -> Self {
self.order = order;
self.display_info.order = order;
self
}

Expand All @@ -137,7 +145,7 @@ impl<S: Span> Label<S> {
/// purposes such as highlighting. By default, spans with a smaller length get a higher priority. You can use this
/// function to override this behaviour.
pub fn with_priority(mut self, priority: i32) -> Self {
self.priority = priority;
self.display_info.priority = priority;
self
}
}
Expand Down Expand Up @@ -281,7 +289,7 @@ impl<'a, S: Span> ReportBuilder<'a, S> {
/// Add multiple labels to the report.
pub fn add_labels<L: IntoIterator<Item = Label<S>>>(&mut self, labels: L) {
let config = &self.config; // This would not be necessary in Rust 2021 edition
self.labels.extend(labels.into_iter().map(|mut label| { label.color = config.filter_color(label.color); label }));
self.labels.extend(labels.into_iter().map(|mut label| { label.display_info.color = config.filter_color(label.display_info.color); label }));
}

/// Add a label to the report.
Expand Down
Loading

0 comments on commit 3ad939a

Please sign in to comment.