Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion codespan-reporting/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ impl<FileId> Label<FileId> {
}

/// Add a message to the diagnostic.
#[must_use]
pub fn with_message(mut self, message: impl Display) -> Label<FileId> {
self.message = message.to_string();
self
Expand Down Expand Up @@ -137,63 +138,73 @@ impl<FileId> Diagnostic<FileId> {
/// Create a new diagnostic with a severity of [`Severity::Bug`].
///
/// [`Severity::Bug`]: Severity::Bug
#[must_use]
pub fn bug() -> Diagnostic<FileId> {
Diagnostic::new(Severity::Bug)
}

/// Create a new diagnostic with a severity of [`Severity::Error`].
///
/// [`Severity::Error`]: Severity::Error
#[must_use]
pub fn error() -> Diagnostic<FileId> {
Diagnostic::new(Severity::Error)
}

/// Create a new diagnostic with a severity of [`Severity::Warning`].
///
/// [`Severity::Warning`]: Severity::Warning
#[must_use]
pub fn warning() -> Diagnostic<FileId> {
Diagnostic::new(Severity::Warning)
}

/// Create a new diagnostic with a severity of [`Severity::Note`].
///
/// [`Severity::Note`]: Severity::Note
#[must_use]
pub fn note() -> Diagnostic<FileId> {
Diagnostic::new(Severity::Note)
}

/// Create a new diagnostic with a severity of [`Severity::Help`].
///
/// [`Severity::Help`]: Severity::Help
#[must_use]
pub fn help() -> Diagnostic<FileId> {
Diagnostic::new(Severity::Help)
}

/// Set the error code of the diagnostic.
#[must_use]
pub fn with_code(mut self, code: impl Display) -> Diagnostic<FileId> {
self.code = Some(code.to_string());
self
}

/// Set the message of the diagnostic.
#[must_use]
pub fn with_message(mut self, message: impl Display) -> Diagnostic<FileId> {
self.message = message.to_string();
self
}

/// Add a label to the diagnostic.
#[must_use]
pub fn with_label(mut self, label: Label<FileId>) -> Diagnostic<FileId> {
self.labels.push(label);
self
}

/// Add some labels to the diagnostic.
#[must_use]
pub fn with_labels(mut self, mut labels: Vec<Label<FileId>>) -> Diagnostic<FileId> {
self.labels.append(&mut labels);
self
}

/// Add some labels to the diagnostic.
#[must_use]
pub fn with_labels_iter(
mut self,
labels: impl IntoIterator<Item = Label<FileId>>,
Expand All @@ -203,18 +214,22 @@ impl<FileId> Diagnostic<FileId> {
}

/// Add a note to the diagnostic.
pub fn with_note(mut self, note: &impl ToString) -> Diagnostic<FileId> {
#[allow(clippy::needless_pass_by_value)]
#[must_use]
pub fn with_note(mut self, note: impl ToString) -> Diagnostic<FileId> {
self.notes.push(note.to_string());
self
}

/// Add some notes to the diagnostic.
#[must_use]
pub fn with_notes(mut self, mut notes: Vec<String>) -> Diagnostic<FileId> {
self.notes.append(&mut notes);
self
}

/// Add some notes to the diagnostic.
#[must_use]
pub fn with_notes_iter(
mut self,
notes: impl IntoIterator<Item = String>,
Expand Down
2 changes: 2 additions & 0 deletions codespan-reporting/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ pub struct Location {
/// assert_eq!(files::column_index(source, 2..13, 2 + 11), 3);
/// assert_eq!(files::column_index(source, 2..13, 2 + 12), 3);
/// ```
#[must_use]
pub fn column_index(source: &str, line_range: Range<usize>, byte_index: usize) -> usize {
let end_index = core::cmp::min(byte_index, core::cmp::min(line_range.end, source.len()));

Expand Down Expand Up @@ -383,6 +384,7 @@ where
Source: AsRef<str>,
{
/// Create a new files database.
#[must_use]
pub fn new() -> SimpleFiles<Name, Source> {
SimpleFiles { files: Vec::new() }
}
Expand Down
9 changes: 9 additions & 0 deletions codespan-reporting/src/term/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ pub mod styles {

impl Styles {
/// The style used to mark a header at a given severity.
#[must_use]
pub fn header(&self, severity: Severity) -> &ColorSpec {
match severity {
Severity::Bug => &self.header_bug,
Expand All @@ -162,23 +163,28 @@ pub mod styles {
}
}

#[must_use]
pub fn header_message(&self) -> &ColorSpec {
&self.header_message
}

#[must_use]
pub fn line_number(&self) -> &ColorSpec {
&self.line_number
}

#[must_use]
pub fn note_bullet(&self) -> &ColorSpec {
&self.note_bullet
}

#[must_use]
pub fn source_border(&self) -> &ColorSpec {
&self.source_border
}

/// The style used to mark a primary or secondary label at a given severity.
#[must_use]
pub fn label(&self, severity: Severity, label_style: LabelStyle) -> &ColorSpec {
match (label_style, severity) {
(LabelStyle::Primary, Severity::Bug) => &self.primary_label_bug,
Expand All @@ -192,6 +198,7 @@ pub mod styles {
}

impl Styles {
#[must_use]
pub fn no_color() -> Styles {
Styles {
header_bug: ColorSpec::new(),
Expand Down Expand Up @@ -416,6 +423,7 @@ impl Default for Chars {

impl Chars {
/// A character set that uses Unicode box drawing characters.
#[must_use]
pub fn box_drawing() -> Chars {
Chars {
snippet_start: "┌─".into(),
Expand Down Expand Up @@ -446,6 +454,7 @@ impl Chars {
/// This is useful if your terminal's font does not support box drawing
/// characters well and results in output that looks similar to rustc's
/// diagnostic output.
#[must_use]
pub fn ascii() -> Chars {
Chars {
snippet_start: "-->".into(),
Expand Down
5 changes: 3 additions & 2 deletions codespan-reporting/src/term/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ impl<'writer, 'config> Renderer<'writer, 'config> {
let current_label_style = single_labels
.iter()
.filter(|(_, range, _)| is_overlapping(range, &column_range))
.map(|(label_style, _, _)| label_style.clone())
.map(|(label_style, _, _)| *label_style)
.max_by_key(label_priority_key);

// Update writer style if necessary
Expand Down Expand Up @@ -925,7 +925,7 @@ impl<'writer, 'config> Renderer<'writer, 'config> {
let column_range = metrics.byte_index..(metrics.byte_index + ch.len_utf8());
let label_style = hanging_labels(single_labels, trailing_label)
.filter(|(_, range, _)| column_range.contains(&range.start))
.map(|(label_style, _, _)| label_style.clone())
.map(|(label_style, _, _)| *label_style)
.max_by_key(label_priority_key);

let mut spaces = match label_style {
Expand Down Expand Up @@ -1199,6 +1199,7 @@ fn is_overlapping(range0: &Range<usize>, range1: &Range<usize>) -> bool {
}

/// For prioritizing primary labels over secondary labels when rendering carets.
#[allow(clippy::trivially_copy_pass_by_ref)]
fn label_priority_key(label_style: &LabelStyle) -> u8 {
match label_style {
LabelStyle::Secondary => 0,
Expand Down
1 change: 1 addition & 0 deletions codespan-reporting/src/term/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl<'diagnostic, 'config, FileId> RichDiagnostic<'diagnostic, 'config, FileId>
where
FileId: Copy + PartialEq,
{
#[must_use]
pub fn new(
diagnostic: &'diagnostic Diagnostic<FileId>,
config: &'config Config,
Expand Down