Move from language-reporting to codespan (#1825)

This commit is contained in:
Jonathan Turner
2020-05-18 11:44:27 -07:00
committed by GitHub
parent 5f1136dcb0
commit 0743b69ad5
12 changed files with 116 additions and 320 deletions

View File

@ -15,7 +15,7 @@ nu-source = { path = "../nu-source", version = "0.14.1" }
ansi_term = "0.12.1"
bigdecimal = { version = "0.1.2", features = ["serde"] }
derive-new = "0.5.8"
language-reporting = "0.4.0"
codespan-reporting = { version = "0.9.4", features = ["serialization"]}
num-bigint = { version = "0.2.6", features = ["serde"] }
num-traits = "0.2.11"
serde = { version = "1.0.110", features = ["derive"] }

View File

@ -1,8 +1,8 @@
use ansi_term::Color;
use bigdecimal::BigDecimal;
use codespan_reporting::diagnostic::{Diagnostic, Label};
use derive_new::new;
use getset::Getters;
use language_reporting::{Diagnostic, Label, Severity};
use nu_source::{b, DebugDocBuilder, HasFallibleSpan, PrettyDebug, Span, Spanned, SpannedItem};
use num_bigint::BigInt;
use num_traits::ToPrimitive;
@ -414,7 +414,7 @@ impl ShellError {
.start()
}
pub fn diagnostic(diagnostic: Diagnostic<Span>) -> ShellError {
pub fn diagnostic(diagnostic: Diagnostic<usize>) -> ShellError {
ProximateShellError::Diagnostic(ShellDiagnostic { diagnostic }).start()
}
@ -422,16 +422,13 @@ impl ShellError {
ProximateShellError::ExternalPlaceholderError.start()
}
pub fn into_diagnostic(self) -> Option<Diagnostic<Span>> {
pub fn into_diagnostic(self) -> Option<Diagnostic<usize>> {
match self.error {
ProximateShellError::MissingValue { span, reason } => {
let mut d = Diagnostic::new(
Severity::Bug,
format!("Internal Error (missing value) :: {}", reason),
);
let mut d = Diagnostic::bug().with_message(format!("Internal Error (missing value) :: {}", reason));
if let Some(span) = span {
d = d.with_label(Label::new_primary(span));
d = d.with_labels(vec![Label::primary(0, span)]);
}
Some(d)
@ -440,57 +437,49 @@ impl ShellError {
command,
error,
} => Some(match error {
ArgumentError::InvalidExternalWord => Diagnostic::new(
Severity::Error,
"Invalid bare word for Nu command (did you intend to invoke an external command?)".to_string())
.with_label(Label::new_primary(command.span)),
ArgumentError::UnexpectedArgument(argument) => Diagnostic::new(
Severity::Error,
ArgumentError::InvalidExternalWord => Diagnostic::error().with_message("Invalid bare word for Nu command (did you intend to invoke an external command?)")
.with_labels(vec![Label::primary(0, command.span)]),
ArgumentError::UnexpectedArgument(argument) => Diagnostic::error().with_message(
format!(
"{} unexpected {}",
Color::Cyan.paint(&command.item),
Color::Green.bold().paint(&argument.item)
),
)
)
.with_label(
Label::new_primary(argument.span).with_message(
format!("unexpected argument (try {} -h)", &command.item))
.with_labels(
vec![Label::primary(0, argument.span).with_message(
format!("unexpected argument (try {} -h)", &command.item))]
),
ArgumentError::UnexpectedFlag(flag) => Diagnostic::new(
Severity::Error,
ArgumentError::UnexpectedFlag(flag) => Diagnostic::error().with_message(
format!(
"{} unexpected {}",
Color::Cyan.paint(&command.item),
Color::Green.bold().paint(&flag.item)
),
)
.with_label(
Label::new_primary(flag.span).with_message(
.with_labels(vec![
Label::primary(0, flag.span).with_message(
format!("unexpected flag (try {} -h)", &command.item))
),
ArgumentError::MissingMandatoryFlag(name) => Diagnostic::new(
Severity::Error,
format!(
]),
ArgumentError::MissingMandatoryFlag(name) => Diagnostic::error().with_message( format!(
"{} requires {}{}",
Color::Cyan.paint(&command.item),
Color::Green.bold().paint("--"),
Color::Green.bold().paint(name)
),
)
.with_label(Label::new_primary(command.span)),
ArgumentError::MissingMandatoryPositional(name) => Diagnostic::new(
Severity::Error,
.with_labels(vec![Label::primary(0, command.span)]),
ArgumentError::MissingMandatoryPositional(name) => Diagnostic::error().with_message(
format!(
"{} requires {} parameter",
Color::Cyan.paint(&command.item),
Color::Green.bold().paint(name.clone())
),
)
.with_label(
Label::new_primary(command.span).with_message(format!("requires {} parameter", name)),
.with_labels(
vec![Label::primary(0, command.span).with_message(format!("requires {} parameter", name))],
),
ArgumentError::MissingValueForName(name) => Diagnostic::new(
Severity::Error,
ArgumentError::MissingValueForName(name) => Diagnostic::error().with_message(
format!(
"{} is missing value for flag {}{}",
Color::Cyan.paint(&command.item),
@ -498,7 +487,7 @@ impl ShellError {
Color::Green.bold().paint(name)
),
)
.with_label(Label::new_primary(command.span)),
.with_labels(vec![Label::primary(0, command.span)]),
}),
ProximateShellError::TypeError {
expected,
@ -507,9 +496,9 @@ impl ShellError {
item: Some(actual),
span,
},
} => Some(Diagnostic::new(Severity::Error, "Type Error").with_label(
Label::new_primary(span)
.with_message(format!("Expected {}, found {}", expected, actual))),
} => Some(Diagnostic::error().with_message("Type Error").with_labels(
vec![Label::primary(0, span)
.with_message(format!("Expected {}, found {}", expected, actual))]),
),
ProximateShellError::TypeError {
expected,
@ -518,13 +507,13 @@ impl ShellError {
item: None,
span
},
} => Some(Diagnostic::new(Severity::Error, "Type Error")
.with_label(Label::new_primary(span).with_message(expected))),
} => Some(Diagnostic::error().with_message("Type Error")
.with_labels(vec![Label::primary(0, span).with_message(expected)])),
ProximateShellError::UnexpectedEof {
expected, span
} => Some(Diagnostic::new(Severity::Error, "Unexpected end of input".to_string())
.with_label(Label::new_primary(span).with_message(format!("Expected {}", expected)))),
} => Some(Diagnostic::error().with_message("Unexpected end of input")
.with_labels(vec![Label::primary(0, span).with_message(format!("Expected {}", expected))])),
ProximateShellError::RangeError {
kind,
@ -534,13 +523,13 @@ impl ShellError {
item,
span
},
} => Some(Diagnostic::new(Severity::Error, "Range Error").with_label(
Label::new_primary(span).with_message(format!(
} => Some(Diagnostic::error().with_message("Range Error").with_labels(
vec![Label::primary(0, span).with_message(format!(
"Expected to convert {} to {} while {}, but it was out of range",
item,
kind.display(),
operation
))),
))]),
),
ProximateShellError::SyntaxError {
@ -549,52 +538,55 @@ impl ShellError {
span,
item
},
} => Some(Diagnostic::new(Severity::Error, "Syntax Error")
.with_label(Label::new_primary(span).with_message(item))),
} => Some(Diagnostic::error().with_message("Syntax Error")
.with_labels(vec![Label::primary(0, span).with_message(item)])),
ProximateShellError::MissingProperty { subpath, expr, .. } => {
let mut diag = Diagnostic::new(Severity::Error, "Missing property");
let mut diag = Diagnostic::error().with_message("Missing property");
if subpath.span == Span::unknown() {
diag.message = format!("Missing property (for {})", subpath.item);
} else {
let subpath = Label::new_primary(subpath.span).with_message(subpath.item);
diag = diag.with_label(subpath);
let subpath = Label::primary(0, subpath.span).with_message(subpath.item);
let mut labels = vec![];
labels.push(subpath);
if expr.span != Span::unknown() {
let expr = Label::new_primary(expr.span).with_message(expr.item);
diag = diag.with_label(expr)
let expr = Label::primary(0, expr.span).with_message(expr.item);
labels.push(expr);
}
diag = diag.with_labels(labels);
}
Some(diag)
}
ProximateShellError::InvalidIntegerIndex { subpath,integer } => {
let mut diag = Diagnostic::new(Severity::Error, "Invalid integer property");
let mut diag = Diagnostic::error().with_message("Invalid integer property");
let mut labels = vec![];
if subpath.span == Span::unknown() {
diag.message = format!("Invalid integer property (for {})", subpath.item)
} else {
let label = Label::new_primary(subpath.span).with_message(subpath.item);
diag = diag.with_label(label)
let label = Label::primary(0, subpath.span).with_message(subpath.item);
labels.push(label);
}
diag = diag.with_label(Label::new_secondary(integer).with_message("integer"));
labels.push(Label::secondary(0, integer).with_message("integer"));
diag = diag.with_labels(labels);
Some(diag)
}
ProximateShellError::Diagnostic(diag) => Some(diag.diagnostic),
ProximateShellError::CoerceError { left, right } => {
Some(Diagnostic::new(Severity::Error, "Coercion error")
.with_label(Label::new_primary(left.span).with_message(left.item))
.with_label(Label::new_secondary(right.span).with_message(right.item)))
Some(Diagnostic::error().with_message("Coercion error")
.with_labels(vec![Label::primary(0, left.span).with_message(left.item),
Label::secondary(0, right.span).with_message(right.item)]))
}
ProximateShellError::UntaggedRuntimeError { reason } => Some(Diagnostic::new(Severity::Error, format!("Error: {}", reason))),
ProximateShellError::UntaggedRuntimeError { reason } => Some(Diagnostic::error().with_message(format!("Error: {}", reason))),
ProximateShellError::ExternalPlaceholderError => None,
}
}
@ -605,8 +597,11 @@ impl ShellError {
span: impl Into<Span>,
) -> ShellError {
ShellError::diagnostic(
Diagnostic::new(Severity::Error, msg.into())
.with_label(Label::new_primary(span.into()).with_message(label.into())),
Diagnostic::error()
.with_message(msg.into())
.with_labels(vec![
Label::primary(0, span.into()).with_message(label.into())
]),
)
}
@ -618,14 +613,12 @@ impl ShellError {
secondary_span: impl Into<Span>,
) -> ShellError {
ShellError::diagnostic(
Diagnostic::new_error(msg.into())
.with_label(
Label::new_primary(primary_span.into()).with_message(primary_label.into()),
)
.with_label(
Label::new_secondary(secondary_span.into())
.with_message(secondary_label.into()),
),
Diagnostic::error()
.with_message(msg.into())
.with_labels(vec![
Label::primary(0, primary_span.into()).with_message(primary_label.into()),
Label::secondary(0, secondary_span.into()).with_message(secondary_label.into()),
]),
)
}
@ -780,7 +773,7 @@ impl HasFallibleSpan for ProximateShellError {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ShellDiagnostic {
pub(crate) diagnostic: Diagnostic<Span>,
pub(crate) diagnostic: Diagnostic<usize>,
}
impl std::hash::Hash for ShellDiagnostic {
@ -790,11 +783,11 @@ impl std::hash::Hash for ShellDiagnostic {
self.diagnostic.message.hash(state);
for label in &self.diagnostic.labels {
label.span.hash(state);
label.range.hash(state);
label.message.hash(state);
match label.style {
language_reporting::LabelStyle::Primary => 0.hash(state),
language_reporting::LabelStyle::Secondary => 1.hash(state),
codespan_reporting::diagnostic::LabelStyle::Primary => 0.hash(state),
codespan_reporting::diagnostic::LabelStyle::Secondary => 1.hash(state),
}
}
}