Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/span/src/lib.rs')
-rw-r--r--crates/span/src/lib.rs23
1 files changed, 20 insertions, 3 deletions
diff --git a/crates/span/src/lib.rs b/crates/span/src/lib.rs
index bfe7b2620d..f6581de38c 100644
--- a/crates/span/src/lib.rs
+++ b/crates/span/src/lib.rs
@@ -20,6 +20,7 @@ pub use self::{
map::{RealSpanMap, SpanMap},
};
+use serde::{Deserialize, Deserializer, Serialize, Serializer};
pub use syntax::Edition;
pub use text_size::{TextRange, TextSize};
pub use vfs::FileId;
@@ -68,11 +69,12 @@ impl Span {
/// Spans represent a region of code, used by the IDE to be able link macro inputs and outputs
/// together. Positions in spans are relative to some [`SpanAnchor`] to make them more incremental
/// friendly.
-#[derive(Clone, Copy, PartialEq, Eq, Hash)]
+#[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Span {
/// The text range of this span, relative to the anchor.
/// We need the anchor for incrementality, as storing absolute ranges will require
/// recomputation on every change in a file at all times.
+ #[serde(serialize_with = "serialize_text_range", deserialize_with = "deserialize_text_range")]
pub range: TextRange,
/// The anchor this span is relative to.
pub anchor: SpanAnchor,
@@ -80,6 +82,21 @@ pub struct Span {
pub ctx: SyntaxContext,
}
+fn serialize_text_range<S>(range: &TextRange, serializer: S) -> Result<S::Ok, S::Error>
+where
+ S: Serializer,
+{
+ (u32::from(range.start()), u32::from(range.end())).serialize(serializer)
+}
+
+fn deserialize_text_range<'de, D>(deserializer: D) -> Result<TextRange, D::Error>
+where
+ D: Deserializer<'de>,
+{
+ let (start, end) = <(u32, u32)>::deserialize(deserializer)?;
+ Ok(TextRange::new(TextSize::from(start), TextSize::from(end)))
+}
+
impl fmt::Debug for Span {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if f.alternate() {
@@ -112,7 +129,7 @@ impl fmt::Display for Span {
}
}
-#[derive(Copy, Clone, PartialEq, Eq, Hash)]
+#[derive(Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct SpanAnchor {
pub file_id: EditionedFileId,
pub ast_id: ErasedFileAstId,
@@ -126,7 +143,7 @@ impl fmt::Debug for SpanAnchor {
/// A [`FileId`] and [`Edition`] bundled up together.
/// The MSB is reserved for `HirFileId` encoding, more upper bits are used to then encode the edition.
-#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
+#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct EditionedFileId(u32);
impl fmt::Debug for EditionedFileId {