From a6ab61e3c724f9a6fabc8b79ac89cfd8de6fe259 Mon Sep 17 00:00:00 2001 From: Tobias Schmitz Date: Tue, 3 Jun 2025 18:03:39 +0200 Subject: [PATCH] refactor: rename `Lines::str` to `text` --- crates/typst-syntax/src/lines.rs | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/crates/typst-syntax/src/lines.rs b/crates/typst-syntax/src/lines.rs index 046965ab6..63b005107 100644 --- a/crates/typst-syntax/src/lines.rs +++ b/crates/typst-syntax/src/lines.rs @@ -13,7 +13,7 @@ pub struct Lines(Arc>); #[derive(Clone)] struct Repr { lines: Vec, - str: S, + text: S, } /// Metadata about a line. @@ -27,18 +27,18 @@ pub struct Line { impl> Lines { /// TODO: memoize this? - pub fn new(str: S) -> Self { - let lines = lines(str.as_ref()); - Lines(Arc::new(Repr { lines, str })) + pub fn new(text: S) -> Self { + let lines = lines(text.as_ref()); + Lines(Arc::new(Repr { lines, text })) } 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. 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. @@ -146,8 +146,8 @@ impl Lines { /// Tries to convert the bytes #[comemo::memoize] pub fn from_bytes(bytes: &[u8]) -> Result, Utf8Error> { - let str = std::str::from_utf8(bytes)?; - Ok(Lines::new(str.to_string())) + let text = std::str::from_utf8(bytes)?; + Ok(Lines::new(text.to_string())) } /// Fully replace the source text. @@ -213,32 +213,34 @@ impl Lines { let inner = Arc::make_mut(&mut self.0); // Update the text itself. - inner.str.replace_range(replace.clone(), with); + inner.text.replace_range(replace.clone(), with); // Remove invalidated line starts. inner.lines.truncate(line + 1); // 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(); } // Recalculate the line starts after the edit. - inner - .lines - .extend(lines_from(start_byte, start_utf16, &inner.str[start_byte..])); + inner.lines.extend(lines_from( + start_byte, + start_utf16, + &inner.text[start_byte..], + )); } } impl Hash for Lines { fn hash(&self, state: &mut H) { - self.0.str.hash(state); + self.0.text.hash(state); } } impl> AsRef for Lines { fn as_ref(&self) -> &str { - self.0.str.as_ref() + self.0.text.as_ref() } }