diff options
| author | Laurenz <laurmaedje@gmail.com> | 2025-06-10 20:13:33 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-10 18:13:33 +0000 |
| commit | 98034903e4184708dbaf3cf6b23a4f4de5e910ec (patch) | |
| tree | 449ded5f9157587720ee6cdf656b18a121db4a74 /crates/typst-syntax | |
| parent | a6ab6ac99a3db30d833f60e59959785f60fdc853 (diff) | |
Adjust source file API surface (#6423)
Diffstat (limited to 'crates/typst-syntax')
| -rw-r--r-- | crates/typst-syntax/src/lines.rs | 2 | ||||
| -rw-r--r-- | crates/typst-syntax/src/reparser.rs | 4 | ||||
| -rw-r--r-- | crates/typst-syntax/src/source.rs | 27 |
3 files changed, 8 insertions, 25 deletions
diff --git a/crates/typst-syntax/src/lines.rs b/crates/typst-syntax/src/lines.rs index fa1e7756..768119dc 100644 --- a/crates/typst-syntax/src/lines.rs +++ b/crates/typst-syntax/src/lines.rs @@ -6,6 +6,8 @@ use std::sync::Arc; use crate::is_newline; /// A text buffer and metadata about lines. +/// +/// This is internally reference-counted and thus cheap to clone. #[derive(Clone)] pub struct Lines<S>(Arc<Repr<S>>); diff --git a/crates/typst-syntax/src/reparser.rs b/crates/typst-syntax/src/reparser.rs index c20d8314..55555c96 100644 --- a/crates/typst-syntax/src/reparser.rs +++ b/crates/typst-syntax/src/reparser.rs @@ -259,10 +259,10 @@ mod tests { panic!("test failed"); } if incremental { - assert_ne!(source.len_bytes(), range.len(), "should have been incremental"); + assert_ne!(source.text().len(), range.len(), "should have been incremental"); } else { assert_eq!( - source.len_bytes(), + source.text().len(), range.len(), "shouldn't have been incremental" ); diff --git a/crates/typst-syntax/src/source.rs b/crates/typst-syntax/src/source.rs index 514cb9a4..abde1f98 100644 --- a/crates/typst-syntax/src/source.rs +++ b/crates/typst-syntax/src/source.rs @@ -57,18 +57,14 @@ impl Source { } /// The whole source as a string slice. - pub fn lines(&self) -> Lines<String> { - Lines::clone(&self.0.lines) - } - - /// The whole source as a string slice. pub fn text(&self) -> &str { self.0.lines.text() } - /// Slice out the part of the source code enclosed by the range. - pub fn get(&self, range: Range<usize>) -> Option<&str> { - self.text().get(range) + /// An acceleration structure for conversion of UTF-8, UTF-16 and + /// line/column indices. + pub fn lines(&self) -> &Lines<String> { + &self.0.lines } /// Fully replace the source text. @@ -107,21 +103,6 @@ impl Source { reparse(&mut inner.root, inner.lines.text(), replace, with.len()) } - /// Get the length of the file in UTF-8 encoded bytes. - pub fn len_bytes(&self) -> usize { - self.0.lines.len_bytes() - } - - /// Get the length of the file in UTF-16 code units. - pub fn len_utf16(&self) -> usize { - self.0.lines.len_utf16() - } - - /// Get the length of the file in lines. - pub fn len_lines(&self) -> usize { - self.0.lines.len_lines() - } - /// Find the node with the given span. /// /// Returns `None` if the span does not point into this source file. |
