refactor: rename Lines::str to text

This commit is contained in:
Tobias Schmitz 2025-06-03 18:03:39 +02:00
parent 37c5e0cdce
commit a6ab61e3c7
No known key found for this signature in database

View File

@ -13,7 +13,7 @@ pub struct Lines<S>(Arc<Repr<S>>);
#[derive(Clone)] #[derive(Clone)]
struct Repr<S> { struct Repr<S> {
lines: Vec<Line>, lines: Vec<Line>,
str: S, text: S,
} }
/// Metadata about a line. /// Metadata about a line.
@ -27,18 +27,18 @@ pub struct Line {
impl<S: AsRef<str>> Lines<S> { impl<S: AsRef<str>> Lines<S> {
/// TODO: memoize this? /// TODO: memoize this?
pub fn new(str: S) -> Self { pub fn new(text: S) -> Self {
let lines = lines(str.as_ref()); let lines = lines(text.as_ref());
Lines(Arc::new(Repr { lines, str })) Lines(Arc::new(Repr { lines, text }))
} }
pub fn text(&self) -> &str { pub fn text(&self) -> &str {
self.0.str.as_ref() self.0.text.as_ref()
} }
/// Get the length of the file in UTF-8 encoded bytes. /// Get the length of the file in UTF-8 encoded bytes.
pub fn len_bytes(&self) -> usize { pub fn len_bytes(&self) -> usize {
self.0.str.as_ref().len() self.0.text.as_ref().len()
} }
/// Get the length of the file in UTF-16 code units. /// Get the length of the file in UTF-16 code units.
@ -146,8 +146,8 @@ impl Lines<String> {
/// Tries to convert the bytes /// Tries to convert the bytes
#[comemo::memoize] #[comemo::memoize]
pub fn from_bytes(bytes: &[u8]) -> Result<Lines<String>, Utf8Error> { pub fn from_bytes(bytes: &[u8]) -> Result<Lines<String>, Utf8Error> {
let str = std::str::from_utf8(bytes)?; let text = std::str::from_utf8(bytes)?;
Ok(Lines::new(str.to_string())) Ok(Lines::new(text.to_string()))
} }
/// Fully replace the source text. /// Fully replace the source text.
@ -213,32 +213,34 @@ impl Lines<String> {
let inner = Arc::make_mut(&mut self.0); let inner = Arc::make_mut(&mut self.0);
// Update the text itself. // Update the text itself.
inner.str.replace_range(replace.clone(), with); inner.text.replace_range(replace.clone(), with);
// Remove invalidated line starts. // Remove invalidated line starts.
inner.lines.truncate(line + 1); inner.lines.truncate(line + 1);
// Handle adjoining of \r and \n. // Handle adjoining of \r and \n.
if inner.str[..start_byte].ends_with('\r') && with.starts_with('\n') { if inner.text[..start_byte].ends_with('\r') && with.starts_with('\n') {
inner.lines.pop(); inner.lines.pop();
} }
// Recalculate the line starts after the edit. // Recalculate the line starts after the edit.
inner inner.lines.extend(lines_from(
.lines start_byte,
.extend(lines_from(start_byte, start_utf16, &inner.str[start_byte..])); start_utf16,
&inner.text[start_byte..],
));
} }
} }
impl<S: Hash> Hash for Lines<S> { impl<S: Hash> Hash for Lines<S> {
fn hash<H: Hasher>(&self, state: &mut H) { fn hash<H: Hasher>(&self, state: &mut H) {
self.0.str.hash(state); self.0.text.hash(state);
} }
} }
impl<S: AsRef<str>> AsRef<str> for Lines<S> { impl<S: AsRef<str>> AsRef<str> for Lines<S> {
fn as_ref(&self) -> &str { fn as_ref(&self) -> &str {
self.0.str.as_ref() self.0.text.as_ref()
} }
} }