Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow adding labels to notes #346

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions codespan-reporting/examples/custom_files.rs
Expand Up @@ -9,7 +9,7 @@
//! cargo run --example custom_files
//! ```

use codespan_reporting::diagnostic::{Diagnostic, Label};
use codespan_reporting::diagnostic::{Diagnostic, Label, Note};
use codespan_reporting::term;
use codespan_reporting::term::termcolor::{ColorChoice, StandardStream};
use std::ops::Range;
Expand Down Expand Up @@ -177,8 +177,8 @@ impl Message {
.collect(),
)
.with_notes(vec![
"found greetings!".to_owned(),
"pleas no greetings :(".to_owned(),
Note::new("found greetings!".to_owned()),
Note::new("pleas no greetings :(".to_owned()),
]),
Message::OverTheTopExclamations { exclamations } => Diagnostic::error()
.with_message("over-the-top exclamations")
Expand All @@ -190,7 +190,7 @@ impl Message {
})
.collect(),
)
.with_notes(vec!["ridiculous!".to_owned()]),
.with_notes(vec![Note::new("ridiculous!".to_owned())]),
}
}
}
4 changes: 2 additions & 2 deletions codespan-reporting/examples/peg_calculator.rs
Expand Up @@ -7,7 +7,7 @@
//! cargo run --example peg_calculator
//! ```

use codespan_reporting::diagnostic::{Diagnostic, Label};
use codespan_reporting::diagnostic::{Diagnostic, Label, Note};
use codespan_reporting::files::SimpleFile;
use codespan_reporting::term;
use codespan_reporting::term::termcolor::{ColorChoice, StandardStream};
Expand Down Expand Up @@ -59,7 +59,7 @@ fn main() -> anyhow::Result<()> {
.with_labels(vec![
Label::primary((), start..start).with_message("parse error")
])
.with_notes(vec![format!("expected: {}", error.expected)]);
.with_notes(vec![Note::new(format!("expected: {}", error.expected))]);

term::emit(&mut writer.lock(), &config, &file, &diagnostic)?;
}
Expand Down
6 changes: 3 additions & 3 deletions codespan-reporting/examples/readme_preview.rs
Expand Up @@ -7,7 +7,7 @@
//! cargo run --example readme_preview svg > codespan-reporting/assets/readme_preview.svg
//! ```

use codespan_reporting::diagnostic::{Diagnostic, Label};
use codespan_reporting::diagnostic::{Diagnostic, Label, Note};
use codespan_reporting::files::SimpleFile;
use codespan_reporting::term::termcolor::{Color, ColorSpec, StandardStream, WriteColor};
use codespan_reporting::term::{self, ColorArg};
Expand Down Expand Up @@ -69,12 +69,12 @@ fn main() -> anyhow::Result<()> {
Label::secondary((), 306..312).with_message("this is found to be of type `String`"),
Label::secondary((), 186..192).with_message("expected type `String` found here"),
])
.with_notes(vec![unindent::unindent(
.with_notes(vec![Note::new(unindent::unindent(
"
expected type `String`
found type `Nat`
",
)])];
))])];

// let mut files = SimpleFiles::new();
match Opts::from_args() {
Expand Down
18 changes: 9 additions & 9 deletions codespan-reporting/examples/term.rs
Expand Up @@ -5,7 +5,7 @@
//! cargo run --example term
//! ```

use codespan_reporting::diagnostic::{Diagnostic, Label};
use codespan_reporting::diagnostic::{Diagnostic, Label, Note};
use codespan_reporting::files::SimpleFiles;
use codespan_reporting::term::termcolor::StandardStream;
use codespan_reporting::term::{self, ColorArg};
Expand Down Expand Up @@ -99,15 +99,15 @@ fn main() -> anyhow::Result<()> {
Label::primary(file_id1, 96..102).with_message("unknown builtin")
])
.with_notes(vec![
"there is a builtin with a similar name: `NATURAL`".to_owned()
Note::new("there is a builtin with a similar name: `NATURAL`".to_owned())
]),
// Unused parameter warning
Diagnostic::warning()
.with_message("unused parameter pattern: `n₂`")
.with_labels(vec![
Label::primary(file_id1, 285..289).with_message("unused parameter")
])
.with_notes(vec!["consider using a wildcard pattern: `_`".to_owned()]),
.with_notes(vec![Note::new("consider using a wildcard pattern: `_`".to_owned())]),
// Unexpected type error
Diagnostic::error()
.with_message("unexpected type in application of `_+_`")
Expand All @@ -117,12 +117,12 @@ fn main() -> anyhow::Result<()> {
Label::secondary(file_id1, 130..155)
.with_message("based on the definition of `_+_`"),
])
.with_notes(vec![unindent::unindent(
.with_notes(vec![Note::new(unindent::unindent(
"
expected type `Nat`
found type `String`
",
)]),
))]),
// Incompatible match clause error
Diagnostic::error()
.with_message("`case` clauses have incompatible types")
Expand All @@ -134,12 +134,12 @@ fn main() -> anyhow::Result<()> {
Label::secondary(file_id3, 41..47)
.with_message("expected type `String` found here"),
])
.with_notes(vec![unindent::unindent(
.with_notes(vec![Note::new(unindent::unindent(
"
expected type `String`
found type `Nat`
",
)]),
))]),
// Incompatible match clause error
Diagnostic::error()
.with_message("`case` clauses have incompatible types")
Expand All @@ -157,12 +157,12 @@ fn main() -> anyhow::Result<()> {
Label::secondary(file_id3, 186..192)
.with_message("expected type `String` found here"),
])
.with_notes(vec![unindent::unindent(
.with_notes(vec![Note::new(unindent::unindent(
"
expected type `String`
found type `Nat`
",
)]),
))]),
];

let writer = StandardStream::stderr(opts.color.into());
Expand Down
31 changes: 29 additions & 2 deletions codespan-reporting/src/diagnostic.rs
Expand Up @@ -92,6 +92,33 @@ impl<FileId> Label<FileId> {
}
}

/// A note that is associated with the primary cause of the diagnostic.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
pub struct Note<FileId> {
/// A message that provides additional information about the diagnostic.
/// This can include line breaks for improved formatting.
pub message: String,
/// An optional label showing the cause of the note.
pub labels: Vec<Label<FileId>>,
}

impl<FileId> Note<FileId> {
/// Create a new note without a label.
pub fn new(message: String) -> Note<FileId> {
Note {
message,
labels: Vec::new(),
}
}

/// Add a label to the note.
pub fn with_labels(mut self, mut labels: Vec<Label<FileId>>) -> Note<FileId> {
self.labels.append(&mut labels);
self
}
}

/// Represents a diagnostic message that can provide information like errors and
/// warnings to the user.
///
Expand All @@ -115,7 +142,7 @@ pub struct Diagnostic<FileId> {
pub labels: Vec<Label<FileId>>,
/// Notes that are associated with the primary cause of the diagnostic.
/// These can include line breaks for improved formatting.
pub notes: Vec<String>,
pub notes: Vec<Note<FileId>>,
}

impl<FileId> Diagnostic<FileId> {
Expand Down Expand Up @@ -184,7 +211,7 @@ impl<FileId> Diagnostic<FileId> {
}

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