Skip to content

Commit

Permalink
Change Span aliases, added Start and End + other small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kaleidawave committed Sep 15, 2023
1 parent 0ccca4e commit 489b0da
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 46 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ repository = "https://github.com/kaleidawave/source-map"
[dependencies]
lsp-types = { version = "0", optional = true }
serde = { version = "1.0", features = ["derive"], optional = true }
base64 = { version = "0.13.0", optional = true }
base64 = { version = "0.21", optional = true }
codespan-reporting = { version = "0.11", optional = true }
self-rust-tokenize = { version = "0.3", optional = true }

[features]
default = ["global-source-filesystem"]
self-rust-tokenize = ["dep:self-rust-tokenize"]
lsp-types-morphisms = ["dep:lsp-types"]
span-serialize = ["dep:serde"]
serde-serialize = ["dep:serde"]
inline-source-map = ["dep:base64"]
codespan-reporting = ["dep:codespan-reporting"]
global-source-filesystem = []
8 changes: 5 additions & 3 deletions examples/source_map_creation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#[cfg(all(feature = "inline-source-map", feature = "global-source-filesystem"))]
fn main() {
use source_map::{
global_store::GlobalStore, FileSystem, SourceId, Span, StringWithSourceMap, ToString,
global_store::GlobalStore, FileSystem, SourceId, SpanWithSource, StringWithSourceMap,
ToString,
};
use split_indices::split_indices_from_str;
use std::{convert::TryInto, env::args, fs};
Expand Down Expand Up @@ -69,11 +70,12 @@ fn main() {

for (range, chunk) in split_indices_from_str(string, char::is_whitespace) {
if !chunk.is_empty() {
output.add_mapping(&Span {
let base_span = SpanWithSource {
start: range.start.try_into().unwrap(),
end: range.end.try_into().unwrap(),
source: source_id,
});
};
output.add_mapping(&base_span);
output.push_str(chunk);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
path::{Path, PathBuf},
};

use crate::{lines_columns_indexes::LineStarts, SourceId, Span};
use crate::{lines_columns_indexes::LineStarts, SourceId, SpanWithSource};

pub struct Source {
pub path: PathBuf,
Expand Down Expand Up @@ -88,8 +88,8 @@ pub trait FileSystem: Sized {
self.get_source_by_id(source_id, |source| source.content.to_owned())
}

fn get_file_whole_span(&self, source_id: SourceId) -> Span {
self.get_source_by_id(source_id, |source| Span {
fn get_file_whole_span(&self, source_id: SourceId) -> SpanWithSource {
self.get_source_by_id(source_id, |source| SpanWithSource {
start: 0,
end: source
.content
Expand Down
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use std::{
pub use filesystem::*;
pub use lines_columns_indexes::LineStarts;
pub use source_id::SourceId;
pub use span::{LineColumnPosition, LineColumnSpan, Position, Span};
pub use to_string::{Counter, StringWithSourceMap, ToString};
pub use span::*;
pub use to_string::*;

const BASE64_ALPHABET: &[u8; 64] =
b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
Expand Down Expand Up @@ -89,19 +89,19 @@ impl SourceMapBuilder {
}

/// Original line and original column are one indexed
pub fn add_mapping(&mut self, source_position: &Span) {
let Span {
pub fn add_mapping(&mut self, source_position: &SpanWithSource) {
let SpanWithSource {
start: source_byte_start,
// TODO should it read this
end: _source_byte_end,
source: from_source,
} = source_position.clone();
} = source_position;

self.used_sources.insert(from_source);
self.used_sources.insert(from_source.clone());

self.mappings.push(MappingOrBreak::Mapping(SourceMapping {
from_source,
source_byte_start: source_byte_start.try_into().unwrap(),
from_source: from_source.clone(),
source_byte_start: source_byte_start.clone().try_into().unwrap(),
on_output_column: self.current_output_column,
// source_byte_end: *source_byte_end,
// on_output_line: self.current_output_line,
Expand Down
2 changes: 1 addition & 1 deletion src/source_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{fmt, path::PathBuf};

/// A identifier for a [crate::Source]
#[derive(PartialEq, Eq, Clone, Copy, Hash)]
#[cfg_attr(feature = "span-serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "serde-serialize", derive(serde::Serialize))]
pub struct SourceId(pub(crate) u16);

impl fmt::Debug for SourceId {
Expand Down
146 changes: 124 additions & 22 deletions src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{convert::TryInto, fmt, ops::Range};

/// A start and end. Also contains trace of original source
#[derive(PartialEq, Eq, Clone, Hash)]
#[cfg_attr(feature = "span-serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "serde-serialize", derive(serde::Serialize))]
#[cfg_attr(
feature = "self-rust-tokenize",
derive(self_rust_tokenize::SelfRustTokenize)
Expand All @@ -15,10 +15,10 @@ pub struct BaseSpan<T> {
pub source: T,
}

pub type SourceLessSpan = BaseSpan<()>;
pub type Span = BaseSpan<SourceId>;
pub type Span = BaseSpan<()>;
pub type SpanWithSource = BaseSpan<SourceId>;

impl fmt::Debug for Span {
impl fmt::Debug for SpanWithSource {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!(
"{}..{}#{}",
Expand All @@ -27,29 +27,57 @@ impl fmt::Debug for Span {
}
}

impl fmt::Debug for SourceLessSpan {
impl fmt::Debug for Span {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!("{}..{}", self.start, self.end,))
}
}

impl SourceLessSpan {
impl Span {
/// TODO explain use cases
pub const NULL_SPAN: Span = Span {
start: 0,
end: 0,
source: (),
};

/// TODO explain use cases
pub fn is_null(&self) -> bool {
self.start == self.end
}

/// Returns whether the end of `self` is the start of `other`
pub fn is_adjacent_to(&self, other: &Self) -> bool {
self.end == other.start
pub fn is_adjacent_to(&self, other: impl Into<Start>) -> bool {
self.end == other.into().0
}

/// Returns a new [`SourceLessSpan`] which starts at the start of `self` a ends at the end of `other`
pub fn union(&self, other: &Self) -> SourceLessSpan {
SourceLessSpan {
/// Returns a new [`Span`] which starts at the start of `self` a ends at the end of `other`
pub fn union(&self, end: impl Into<End>) -> Span {
Span {
start: self.start,
end: other.end,
end: end.into().0,
source: (),
}
}

pub fn get_end(&self) -> End {
End(self.end)
}

pub fn get_start(&self) -> Start {
Start(self.start)
}

pub fn with_source(self, source: SourceId) -> SpanWithSource {
SpanWithSource {
start: self.start,
end: self.end,
source,
}
}
}

impl Span {
impl SpanWithSource {
pub fn get_start(&self) -> Position {
Position(self.start, self.source)
}
Expand Down Expand Up @@ -89,7 +117,7 @@ impl Span {
}

/// TODO explain use cases
pub const NULL_SPAN: Span = Span {
pub const NULL_SPAN: SpanWithSource = SpanWithSource {
start: 0,
end: 0,
source: SourceId::NULL,
Expand All @@ -99,6 +127,14 @@ impl Span {
pub fn is_null(&self) -> bool {
self.source == SourceId::NULL
}

pub fn without_source(self) -> Span {
Span {
start: self.start,
end: self.end,
source: (),
}
}
}

// TODO why are two implementations needed
Expand All @@ -120,17 +156,83 @@ impl<T> From<BaseSpan<T>> for Range<usize> {
}
}

/// The byte start
#[derive(Debug, Clone, Copy)]
pub struct Start(pub u32);

impl Start {
pub fn new(pos: u32) -> Self {
Self(pos)
}

pub fn with_length(&self, len: usize) -> BaseSpan<()> {
BaseSpan {
start: self.0,
end: self.0 + len as u32,
source: (),
}
}

pub fn get_end_after(&self, len: usize) -> End {
End(self.0 + len as u32)
}
}

/// The byte start
#[derive(Debug, Clone, Copy)]
pub struct End(pub u32);

impl End {
pub fn new(pos: u32) -> Self {
Self(pos)
}

pub fn is_adjacent_to(&self, other: impl Into<Start>) -> bool {
self.0 == other.into().0
}
}

impl From<Span> for Start {
fn from(value: Span) -> Self {
Start(value.start)
}
}

impl From<Span> for End {
fn from(value: Span) -> Self {
End(value.start)
}
}

impl<'a> From<&'a Span> for Start {
fn from(value: &'a Span) -> Self {
Start(value.start)
}
}

impl<'a> From<&'a Span> for End {
fn from(value: &'a Span) -> Self {
End(value.start)
}
}

impl Start {
pub fn union(&self, end: impl Into<End>) -> Span {
Span {
start: self.0,
end: end.into().0,
source: (),
}
}
}

/// A scalar/singular byte wise position. **Zero based**
#[derive(PartialEq, Eq, Clone)]
pub struct Position(pub u32, pub SourceId);

impl fmt::Debug for Position {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.1.is_null() {
f.write_fmt(format_args!("{}", self.0,))
} else {
f.write_fmt(format_args!("{}#{}", self.0, self.1 .0))
}
f.write_fmt(format_args!("{}#{}", self.0, self.1 .0))
}
}

Expand Down Expand Up @@ -188,7 +290,7 @@ pub struct LineColumnSpan<T: StringEncoding> {
}

impl<T: StringEncoding> LineColumnSpan<T> {
pub fn into_scalar_span(self, fs: &impl FileSystem) -> Span {
pub fn into_scalar_span(self, fs: &impl FileSystem) -> SpanWithSource {
fs.get_source_by_id(self.source, |source| {
let line_start_byte = source.line_starts.0[self.line_start as usize];
let column_start_length = T::encoded_length_to_byte_count(
Expand All @@ -202,7 +304,7 @@ impl<T: StringEncoding> LineColumnSpan<T> {
self.column_start as usize,
);

Span {
SpanWithSource {
start: (line_start_byte + column_start_length).try_into().unwrap(),
end: (line_end_byte + column_end_length).try_into().unwrap(),
source: self.source,
Expand Down Expand Up @@ -283,7 +385,7 @@ Another line";
fn scalar_span_to_line_column() {
let (fs, source) = get_file_system_and_source();

let paragraph_span = Span {
let paragraph_span = SpanWithSource {
start: 19,
end: 28,
source,
Expand Down
10 changes: 5 additions & 5 deletions src/to_string.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{FileSystem, SourceMap, SourceMapBuilder, Span};
use crate::{FileSystem, SourceMap, SourceMapBuilder, SpanWithSource};

/// A trait for defining behavior of adding content to a buffer. As well as register markers for source maps
pub trait ToString {
Expand All @@ -13,7 +13,7 @@ pub trait ToString {
fn push_str_contains_new_line(&mut self, string: &str);

/// Adds a mapping of the from a original position in the source to the position in the current buffer
fn add_mapping(&mut self, source_span: &Span);
fn add_mapping(&mut self, source_span: &SpanWithSource);
}

// TODO clarify calls
Expand All @@ -34,7 +34,7 @@ impl ToString for String {
self.push_str(string)
}

fn add_mapping(&mut self, _source_span: &Span) {}
fn add_mapping(&mut self, _source_span: &SpanWithSource) {}
}

/// Building a source along with its source map
Expand Down Expand Up @@ -89,7 +89,7 @@ impl ToString for StringWithSourceMap {
self.0.push_str(slice);
}

fn add_mapping(&mut self, source_span: &Span) {
fn add_mapping(&mut self, source_span: &SpanWithSource) {
self.1.add_mapping(source_span);
}
}
Expand Down Expand Up @@ -125,7 +125,7 @@ impl ToString for Counter {
self.0 += string.len();
}

fn add_mapping(&mut self, _source_span: &Span) {}
fn add_mapping(&mut self, _source_span: &SpanWithSource) {}
}

#[cfg(test)]
Expand Down
4 changes: 2 additions & 2 deletions tests/to_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#[test]
fn to_tokens() {
use self_rust_tokenize::SelfRustTokenize;
use source_map::{SourceId, Span};
use source_map::{SourceId, SpanWithSource};

let span = Span {
let span = SpanWithSource {
start: 10,
end: 20,
source: SourceId::NULL,
Expand Down

0 comments on commit 489b0da

Please sign in to comment.