wright/source_tracking/fragment.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
//! [Fragment] struct and implementation for dealing with fragments of source code.
use super::SourceRef;
use derive_more::Display;
use std::{ops::Range, str::Chars, sync::Arc};
#[cfg(doc)]
use crate::source_tracking::source::Source;
/// A fragment of source code.
///
/// This can be part of (or all of) a [Source].
#[derive(Clone, Debug, Display)]
#[display("{}", "self.as_str()")]
pub struct Fragment {
/// The [Source] that this fragment is in.
pub source: SourceRef,
/// Fragments are represented using byte ranges in the [Source] referenced by [Fragment::source].
///
/// This [Fragment] is considered invalid if this range is out of order or either end of it is not
/// on a char boundary in source according to [str::is_char_boundary].
pub range: Range<usize>,
}
impl Fragment {
/// Check that this [Fragment] is valid, and references a real existing (though possibly empty) part of
/// the [Fragment::source].
pub fn is_valid(&self) -> bool {
// Get a string reference to the whole source.
let source_as_str: &str = self.source.source().as_ref();
// Check validity.
self.range.end >= self.range.start
&& source_as_str.is_char_boundary(self.range.start)
&& source_as_str.is_char_boundary(self.range.end)
}
/// Get the [str] represented by this [Fragment].
///
/// # Panics
/// - This will [panic] in the unlikely event that [Fragment::range] is out of bounds or lands between char
/// boundaries for [Fragment::source].
pub fn as_str(&self) -> &str {
&self.source.source().as_str()[self.range.clone()]
}
/// Get the length (in bytes) of this [Fragment].
/// Does not check this [Fragment] for validity.
pub const fn len(&self) -> usize {
self.range.end.saturating_sub(self.range.start)
}
/// Check if this fragment has a [`Fragment::len`] `== 0`.
/// Does not check this [Fragment] for validity.
pub const fn is_empty(&self) -> bool {
self.len() == 0
}
/// Return true if this [Fragment] entirely contains another [Fragment] and they're from the same [Source] by
/// [Source::id].
///
/// If `other` is empty, it can still be considered to be contained in this [Fragment] if its
/// [Fragment::range] is entirely within `self`'s [Fragment::range] (basically whether the location of the empty
/// fragment is in this one).
pub fn contains(&self, other: &Self) -> bool {
self.source.id == other.source.id
&& self.range.start <= other.range.start
&& self.range.end >= other.range.end
}
/// Get the number of bytes between the beginning of `origin` and the beginning of `self`.
///
/// # Panics:
/// - Panics if `self` is not a [Fragment] within `origin` according to [`Fragment::contains`].
pub fn offset_from(&self, origin: &Self) -> usize {
if !origin.contains(self) {
panic!("This fragment must be contained in the original fragment");
}
self.range.start - origin.range.start
}
/// Get a [Chars] [Iterator] over the [char]acters in this [Fragment].
pub fn chars(&self) -> Chars<'_> {
self.as_str().chars()
}
/// Get a sub-fragment of this fragment (see [Fragment::contains]) with the whitespace at either end trimmed off.
/// This will return the fragment unchanged if it is empty.
///
/// This calls [Fragment::trim_start] and then [Fragment::trim_end] internally and should match the behavior of
/// [str::trim].
///
/// If this returns an empty [Fragment] it will be at the end of the parent [Fragment].
pub fn trimmed(self) -> Self {
self.trim_start().trim_end()
}
/// Get a sub-fragment of this fragment (see [Fragment::contains]) with the whitespace trimmed off the end.
/// This will return it unchanged if empty.
///
/// See [str::trim_end] for exact behaviors.
pub fn trim_end(mut self) -> Self {
// Get the string representation of this fragment.
let original_str: &str = self.as_str();
// Trim it.
let trimmed_str: &str = original_str.trim_end();
// Calculate the new end of the range.
let new_end: usize = self.range.start + trimmed_str.len();
// Update self.
self.range = self.range.start..new_end;
// Return the updated self.
self
}
/// Get a sub-fragment of this fragment (see [Fragment::contains]) with the whitespace trimmed off the start.
/// This will return it unchanged if empty.
///
/// See [str::trim_start] for exact behaviors.
pub fn trim_start(mut self) -> Self {
// Get the string representation of this fragment.
let original_str: &str = self.as_str();
// Trim it.
let trimmed_str: &str = original_str.trim_start();
// Calculate the new start of the range.
let new_start: usize = self.range.end - trimmed_str.len();
// Update self.
self.range = new_start..self.range.end;
// Return the updated self.
self
}
/// Split this [Fragment] into two sub-[Fragment]s, the left containing the first `bytes_from_start`
/// bytes, and the right containing the rest.
///
/// # Panics
/// - This will panic if the provided `bytes_from_start` does not land on a unicode character boundary or is larger
/// than the length of this fragment according to [str::is_char_boundary].
pub fn split_at(&self, bytes_from_start: usize) -> (Self, Self) {
// Check boundaries.
if !self.as_str().is_char_boundary(bytes_from_start) {
panic!("Cannot split in the middle of a unicode character");
}
self.split_at_unchecked(bytes_from_start)
}
/// This is the same as [Fragment::split_at] except it does not check that the created fragments are valid or
/// that either can call [Fragment::as_str] without panicking.
/// Use with caution.
pub fn split_at_unchecked(&self, bytes_from_start: usize) -> (Self, Self) {
// Calculate ranges.
let left_range: Range<usize> = self.range.start..(self.range.start + bytes_from_start);
let right_range: Range<usize> = (self.range.start + bytes_from_start)..self.range.end;
// Construct fragments.
(
Fragment {
source: self.source.clone(),
range: left_range,
},
Fragment {
source: self.source.clone(),
range: right_range,
},
)
}
/// Move the start of this [Fragment] forward by a given number of bytes.
///
/// # Panics
/// - Panics if the advancing by `bytes` would create an invalid [Fragment].
pub fn advance_by(&mut self, bytes: usize) {
// Bounds check.
if !self.as_str().is_char_boundary(bytes) {
panic!("Advancing by {bytes} bytes would create an invalid fragment.");
}
self.advance_by_unchecked(bytes);
}
/// This is the same as [Fragment::advance_by] except without the bounds checking. Use carefully or the created
/// [Fragment]s will be invalid.
#[inline]
pub fn advance_by_unchecked(&mut self, bytes: usize) {
self.range.start += bytes;
}
/// Retain up to `bytes` bytes of this [Fragment].
///
/// # Panics
/// - Panics if the updated [Fragment] would be invalid.
pub fn retain(&mut self, bytes: usize) {
// Bounds check.
if !self.as_str().is_char_boundary(bytes) {
panic!("Retaining to {bytes} bytes would create an invalid fragment.");
}
self.retain_unchecked(bytes);
}
/// This is the same as [Fragment::retain] except without the bounds checking. Use carefully or the created
/// [Fragment]s will be invalid.
#[inline]
pub fn retain_unchecked(&mut self, bytes: usize) {
self.range.end = self.range.start + bytes;
}
/// Get a [Range] of line indices (0-indexed, see [Source::get_line]) that this fragment overlaps.
pub fn line_indices(&self) -> Range<usize> {
let start_line_index: usize = self.source.line_index(self.range.start);
// Subtract one when doing the end because if this fragment ends at the end of a line, we don't want to include
// the next line (obo -- range is exclusive).
let ending_line_index: usize = self.source.line_index(self.range.end - 1);
// Return the range.
start_line_index..ending_line_index
}
/// Get the line number (not index) that this line starts on.
///
/// This re-calculates [Fragment::line_indices], which may be expensive on very large files, so use with care.
pub fn starts_on_line(&self) -> usize {
self.line_indices().start + 1
}
/// Get the number of bytes between the start of the line that this [Fragment] starts on and the start of this
/// [Fragment]
pub fn starting_col_index(&self) -> usize {
let line_start_index = Arc::clone(&self.source)
.get_line(self.line_indices().start)
.range
.start;
self.range.start - line_start_index
}
}
impl PartialEq for Fragment {
/// Fragment equality is based on referencing the same [Source] using [Arc::ptr_eq] and having the same
/// [Fragment::range].
fn eq(&self, other: &Self) -> bool {
self.source.id == other.source.id && self.range == other.range
}
}
impl Eq for Fragment {}
#[cfg(test)]
mod tests {
use super::Fragment;
use crate::source_tracking::{filename::FileName, source::Source};
use std::sync::Arc;
/// Utility function to create a one-off fragment over a static string.
fn from_static(s: &'static str) -> Fragment {
let source = Source::new_from_static_str(FileName::None, s);
let arc = Arc::new(source);
Fragment {
range: 0..arc.source().as_ref().len(),
source: arc,
}
}
#[test]
fn test_split_single() {
let a = from_static("+");
let (left, right) = a.split_at(1);
assert_eq!(left.as_str(), "+");
assert_eq!(right.as_str(), "");
}
#[test]
fn test_offset_from() {
let a = from_static("abcde");
let (b, c) = a.split_at(2);
assert_eq!(b.offset_from(&a), 0);
assert_eq!(c.offset_from(&a), 2);
}
#[test]
#[should_panic]
fn test_offset_panics() {
let a = from_static("abc");
let b = from_static("def");
a.offset_from(&b);
}
#[test]
fn test_trimmed_is_contained() {
let a = from_static(" aa aa ");
let b = a.clone().trimmed();
assert!(a.contains(&b));
assert_eq!(b.len(), 5);
}
#[test]
fn trimmed_empty() {
let empty = from_static("");
assert_eq!(empty.clone().trimmed(), empty);
}
#[test]
fn trimmed_whitespace() {
let w = from_static(" ");
assert!(w.clone().trimmed().is_empty());
}
}