pub struct TextRange { /* private fields */ }Expand description
A range in the source text [start, end), represented as byte offsets.
Stores only offsets.
Implementations§
Source§impl TextRange
impl TextRange
Sourcepub const fn new(start: TextSize, end: TextSize) -> Self
pub const fn new(start: TextSize, end: TextSize) -> Self
Creates a new range from start (inclusive) and end (exclusive) offsets.
Sourcepub const fn contains(self, offset: TextSize) -> bool
pub const fn contains(self, offset: TextSize) -> bool
Whether offset is contained in this range.
Sourcepub const fn from_offset_len(offset: usize, len: usize) -> Self
pub const fn from_offset_len(offset: usize, len: usize) -> Self
Creates a range from an absolute byte offset and a length.
Sourcepub fn source_text<'a>(&self, source: &'a str) -> &'a str
pub fn source_text<'a>(&self, source: &'a str) -> &'a str
Extracts the corresponding slice from the source text.
Returns an empty string if the range is empty or offsets are out of bounds.
Examples found in repository?
examples/parse_numpy.rs (line 48)
7fn main() {
8 let docstring = r#"
9Calculate the area of a rectangle.
10
11This function takes the width and height of a rectangle
12and returns its area.
13
14Parameters
15----------
16width : float
17 The width of the rectangle.
18height : float
19 The height of the rectangle.
20
21Returns
22-------
23float
24 The area of the rectangle.
25
26Raises
27------
28ValueError
29 If width or height is negative.
30
31Examples
32--------
33>>> calculate_area(5.0, 3.0)
3415.0
35"#;
36
37 let parsed = parse_numpy(docstring);
38 let doc = NumPyDocstring::cast(parsed.root()).unwrap();
39
40 println!("╔══════════════════════════════════════════════════╗");
41 println!("║ NumPy-style Docstring Example ║");
42 println!("╚══════════════════════════════════════════════════╝");
43
44 println!();
45
46 // Display: raw source text
47 println!("── raw text ────────────────────────────────────────");
48 println!("{}", doc.syntax().range().source_text(parsed.source()));
49
50 println!();
51
52 // pretty_print: structured AST
53 println!("── parsed AST ──────────────────────────────────────");
54 print!("{}", parsed.pretty_print());
55}Trait Implementations§
impl Copy for TextRange
impl Eq for TextRange
impl StructuralPartialEq for TextRange
Auto Trait Implementations§
impl Freeze for TextRange
impl RefUnwindSafe for TextRange
impl Send for TextRange
impl Sync for TextRange
impl Unpin for TextRange
impl UnsafeUnpin for TextRange
impl UnwindSafe for TextRange
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more