diff --git a/codespan-lsp/src/lib.rs b/codespan-lsp/src/lib.rs index be97066..7b2a81a 100644 --- a/codespan-lsp/src/lib.rs +++ b/codespan-lsp/src/lib.rs @@ -180,8 +180,6 @@ pub fn make_lsp_diagnostic( where F: FnMut(&str) -> Result, { - use codespan_reporting::LabelStyle; - let find_file = |index| { code_map .find_file(index) @@ -189,37 +187,22 @@ where }; // We need a position for the primary error so take the span from the first primary label - let (primary_file_map, primary_label_range) = { - let first_primary_label = diagnostic - .labels - .iter() - .find(|label| label.style == LabelStyle::Primary); - - match first_primary_label { - Some(label) => { - let file_map = find_file(label.span.start())?; - (Some(file_map), byte_span_to_range(&file_map, label.span)?) - }, - None => (None, UNKNOWN_RANGE), - } - }; + let primary_file_map = find_file(diagnostic.primary_label.span.start())?; + let primary_label_range = byte_span_to_range(&primary_file_map, diagnostic.primary_label.span)?; let related_information = diagnostic - .labels + .secondary_labels .into_iter() .map(|label| { - let (file_map, range) = match primary_file_map { - // If the label's span does not point anywhere, assume it comes from the same file - // as the primary label - Some(file_map) if label.span.start() == ByteIndex::none() => { - (file_map, UNKNOWN_RANGE) - }, - Some(_) | None => { - let file_map = find_file(label.span.start())?; - let range = byte_span_to_range(file_map, label.span)?; + // If the label's span does not point anywhere, assume it comes from the same file + // as the primary label + let (file_map, range) = if label.span.start() == ByteIndex::none() { + (primary_file_map, UNKNOWN_RANGE) + } else { + let file_map = find_file(label.span.start())?; + let range = byte_span_to_range(file_map, label.span)?; - (file_map, range) - }, + (file_map, range) }; let uri = codespan_name_to_file(file_map.name()) diff --git a/codespan-reporting/examples/emit.rs b/codespan-reporting/examples/emit.rs index e0e53c7..8f48e78 100644 --- a/codespan-reporting/examples/emit.rs +++ b/codespan-reporting/examples/emit.rs @@ -2,7 +2,7 @@ use structopt::StructOpt; use codespan::{CodeMap, Span}; use codespan_reporting::termcolor::StandardStream; -use codespan_reporting::{emit, ColorArg, Diagnostic, Label, Severity}; +use codespan_reporting::{emit, ColorArg, Diagnostic, Label}; #[derive(Debug, StructOpt)] #[structopt(name = "emit")] @@ -31,25 +31,23 @@ fn main() { let file_map = code_map.add_filemap("test".into(), source.to_string()); let str_start = file_map.byte_index(3.into(), 6.into()).unwrap(); - let error = Diagnostic::new(Severity::Error, "Unexpected type in `+` application") - .with_label( - Label::new_primary(Span::from_offset(str_start, 2.into())) - .with_message("Expected integer but got string"), - ) - .with_label( - Label::new_secondary(Span::from_offset(str_start, 2.into())) - .with_message("Expected integer but got string"), - ) - .with_code("E0001"); + let error = Diagnostic::new_error( + "Unexpected type in `+` application", + Label::new( + Span::from_offset(str_start, 2.into()), + "Expected integer but got string", + ), + ) + .with_code("E0001") + .with_secondary_labels(vec![Label::new( + Span::from_offset(str_start, 2.into()), + "Expected integer but got string", + )]); let line_start = file_map.byte_index(2.into(), 3.into()).unwrap(); - let warning = Diagnostic::new( - Severity::Warning, + let warning = Diagnostic::new_warning( "`+` function has no effect unless its result is used", - ) - .with_label( - Label::new_primary(Span::from_offset(line_start, 27.into())) - .with_message("Value discarded"), + Label::new(Span::from_offset(line_start, 27.into()), "Value discarded"), ); let diagnostics = [error, warning]; diff --git a/codespan-reporting/src/diagnostic.rs b/codespan-reporting/src/diagnostic.rs index 4262b06..23c2d22 100644 --- a/codespan-reporting/src/diagnostic.rs +++ b/codespan-reporting/src/diagnostic.rs @@ -6,18 +6,7 @@ use serde::{Deserialize, Serialize}; use crate::Severity; -/// A style for the label -#[derive(Copy, Clone, PartialEq, Debug)] -#[cfg_attr(feature = "memory_usage", derive(heapsize_derive::HeapSizeOf))] -#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] -pub enum LabelStyle { - /// The main focus of the diagnostic - Primary, - /// Supporting labels that may help to isolate the cause of the diagnostic - Secondary, -} - -/// A label describing an underlined region of code associated with a diagnostic +/// A label describing an underlined region of code associated with a diagnostic. #[derive(Clone, Debug)] #[cfg_attr(feature = "memory_usage", derive(heapsize_derive::HeapSizeOf))] #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] @@ -26,34 +15,19 @@ pub struct Label { pub span: ByteSpan, /// A message to provide some additional information for the underlined code. pub message: String, - /// The style to use for the label. - pub style: LabelStyle, } impl Label { - pub fn new(span: ByteSpan, style: LabelStyle) -> Label { + pub fn new(span: ByteSpan, message: impl Into) -> Label { Label { span, - message: String::new(), - style, + message: message.into(), } } - - pub fn new_primary(span: ByteSpan) -> Label { - Label::new(span, LabelStyle::Primary) - } - - pub fn new_secondary(span: ByteSpan) -> Label { - Label::new(span, LabelStyle::Secondary) - } - - pub fn with_message(mut self, message: impl Into) -> Label { - self.message = message.into(); - self - } } -/// Represents a diagnostic message and associated child messages. +/// Represents a diagnostic message that can provide information like errors and +/// warnings to the user. #[derive(Clone, Debug)] #[cfg_attr(feature = "memory_usage", derive(heapsize_derive::HeapSizeOf))] #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] @@ -62,41 +36,43 @@ pub struct Diagnostic { pub severity: Severity, /// An optional code that identifies this diagnostic. pub code: Option, - /// The main message associated with this diagnostic + /// The main message associated with this diagnostic. pub message: String, - /// The labelled spans marking the regions of code that cause this - /// diagnostic to be raised - pub labels: Vec