2024-04-04 07:13:25 +00:00
|
|
|
use std::ops::Deref;
|
|
|
|
|
2021-09-20 21:37:26 +00:00
|
|
|
use miette::SourceSpan;
|
2021-10-01 05:11:49 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-09-20 21:37:26 +00:00
|
|
|
|
2021-11-03 00:26:09 +00:00
|
|
|
/// A spanned area of interest, generic over what kind of thing is of interest
|
2024-04-04 07:13:25 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
|
2024-03-02 17:14:02 +00:00
|
|
|
pub struct Spanned<T> {
|
2021-10-01 21:53:13 +00:00
|
|
|
pub item: T,
|
|
|
|
pub span: Span,
|
|
|
|
}
|
|
|
|
|
2024-04-04 07:13:25 +00:00
|
|
|
impl<T> Spanned<T> {
|
|
|
|
/// Map to a spanned reference of the inner type, i.e. `Spanned<T> -> Spanned<&T>`.
|
|
|
|
pub fn as_ref(&self) -> Spanned<&T> {
|
|
|
|
Spanned {
|
|
|
|
item: &self.item,
|
|
|
|
span: self.span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Map to a mutable reference of the inner type, i.e. `Spanned<T> -> Spanned<&mut T>`.
|
|
|
|
pub fn as_mut(&mut self) -> Spanned<&mut T> {
|
|
|
|
Spanned {
|
|
|
|
item: &mut self.item,
|
|
|
|
span: self.span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Map to the result of [`.deref()`](std::ops::Deref::deref) on the inner type.
|
|
|
|
///
|
|
|
|
/// This can be used for example to turn `Spanned<Vec<T>>` into `Spanned<&[T]>`.
|
|
|
|
pub fn as_deref(&self) -> Spanned<&<T as Deref>::Target>
|
|
|
|
where
|
|
|
|
T: Deref,
|
|
|
|
{
|
|
|
|
Spanned {
|
|
|
|
item: self.item.deref(),
|
|
|
|
span: self.span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Map the spanned item with a function.
|
|
|
|
pub fn map<U>(self, f: impl FnOnce(T) -> U) -> Spanned<U> {
|
|
|
|
Spanned {
|
|
|
|
item: f(self.item),
|
|
|
|
span: self.span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-02 17:14:02 +00:00
|
|
|
/// Helper trait to create [`Spanned`] more ergonomically.
|
|
|
|
pub trait IntoSpanned: Sized {
|
|
|
|
/// Wrap items together with a span into [`Spanned`].
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use nu_protocol::{Span, IntoSpanned};
|
|
|
|
/// # let span = Span::test_data();
|
|
|
|
/// let spanned = "Hello, world!".into_spanned(span);
|
|
|
|
/// assert_eq!("Hello, world!", spanned.item);
|
|
|
|
/// assert_eq!(span, spanned.span);
|
|
|
|
/// ```
|
|
|
|
fn into_spanned(self, span: Span) -> Spanned<Self>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> IntoSpanned for T {
|
|
|
|
fn into_spanned(self, span: Span) -> Spanned<Self> {
|
|
|
|
Spanned { item: self, span }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-03 00:26:09 +00:00
|
|
|
/// Spans are a global offset across all seen files, which are cached in the engine's state. The start and
|
|
|
|
/// end offset together make the inclusive start/exclusive end pair for where to underline to highlight
|
|
|
|
/// a given point of interest.
|
2022-12-03 09:44:12 +00:00
|
|
|
#[non_exhaustive]
|
2021-10-13 17:53:27 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
|
2021-06-30 01:42:56 +00:00
|
|
|
pub struct Span {
|
|
|
|
pub start: usize,
|
|
|
|
pub end: usize,
|
|
|
|
}
|
|
|
|
|
2021-09-20 21:37:26 +00:00
|
|
|
impl From<Span> for SourceSpan {
|
|
|
|
fn from(s: Span) -> Self {
|
2024-02-08 01:26:18 +00:00
|
|
|
Self::new(s.start.into(), s.end - s.start)
|
2021-09-20 21:37:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-30 01:42:56 +00:00
|
|
|
impl Span {
|
2021-07-03 03:35:15 +00:00
|
|
|
pub fn new(start: usize, end: usize) -> Span {
|
2022-12-03 09:44:12 +00:00
|
|
|
debug_assert!(
|
|
|
|
end >= start,
|
2023-01-30 01:37:54 +00:00
|
|
|
"Can't create a Span whose end < start, start={start}, end={end}"
|
2022-12-03 09:44:12 +00:00
|
|
|
);
|
|
|
|
|
2021-07-03 03:35:15 +00:00
|
|
|
Span { start, end }
|
2021-06-30 01:42:56 +00:00
|
|
|
}
|
2021-07-01 00:01:04 +00:00
|
|
|
|
2022-12-03 09:44:12 +00:00
|
|
|
pub const fn unknown() -> Span {
|
|
|
|
Span { start: 0, end: 0 }
|
2022-05-01 03:32:30 +00:00
|
|
|
}
|
|
|
|
|
2022-01-23 22:32:02 +00:00
|
|
|
/// Note: Only use this for test data, *not* live data, as it will point into unknown source
|
|
|
|
/// when used in errors.
|
2022-12-03 09:44:12 +00:00
|
|
|
pub const fn test_data() -> Span {
|
|
|
|
Self::unknown()
|
2021-07-01 00:01:04 +00:00
|
|
|
}
|
2021-07-22 20:45:23 +00:00
|
|
|
|
|
|
|
pub fn offset(&self, offset: usize) -> Span {
|
2022-12-03 09:44:12 +00:00
|
|
|
Span::new(self.start - offset, self.end - offset)
|
2021-07-22 20:45:23 +00:00
|
|
|
}
|
2021-10-13 17:53:27 +00:00
|
|
|
|
|
|
|
pub fn contains(&self, pos: usize) -> bool {
|
|
|
|
pos >= self.start && pos < self.end
|
|
|
|
}
|
2022-01-03 23:14:33 +00:00
|
|
|
|
2022-02-15 02:09:21 +00:00
|
|
|
pub fn contains_span(&self, span: Span) -> bool {
|
|
|
|
span.start >= self.start && span.end <= self.end
|
|
|
|
}
|
|
|
|
|
2022-01-03 23:14:33 +00:00
|
|
|
/// Point to the space just past this span, useful for missing
|
|
|
|
/// values
|
|
|
|
pub fn past(&self) -> Span {
|
|
|
|
Span {
|
|
|
|
start: self.end,
|
|
|
|
end: self.end,
|
|
|
|
}
|
|
|
|
}
|
2021-06-30 01:42:56 +00:00
|
|
|
}
|
2021-09-02 01:29:43 +00:00
|
|
|
|
2021-12-19 19:25:02 +00:00
|
|
|
/// Used when you have a slice of spans of at least size 1
|
2021-09-02 01:29:43 +00:00
|
|
|
pub fn span(spans: &[Span]) -> Span {
|
|
|
|
let length = spans.len();
|
|
|
|
|
2022-12-03 09:44:12 +00:00
|
|
|
//TODO debug_assert!(length > 0, "expect spans > 0");
|
2021-09-02 01:29:43 +00:00
|
|
|
if length == 0 {
|
2022-12-03 09:44:12 +00:00
|
|
|
Span::unknown()
|
2021-09-02 01:29:43 +00:00
|
|
|
} else if length == 1 {
|
|
|
|
spans[0]
|
|
|
|
} else {
|
2022-12-03 09:44:12 +00:00
|
|
|
let end = spans
|
|
|
|
.iter()
|
|
|
|
.map(|s| s.end)
|
|
|
|
.max()
|
|
|
|
.expect("Must be an end. Length > 0");
|
|
|
|
Span::new(spans[0].start, end)
|
2021-09-02 01:29:43 +00:00
|
|
|
}
|
|
|
|
}
|