pub struct SourceText { /* private fields */ }Expand description
Represents source code text with line mapping and optional URL reference.
This struct manages the raw source text and provides utilities for:
- Text extraction at specific offsets or ranges
- Character and line/column position tracking
- LSP position and range conversions (when lsp-typesfeature is enabled)
- Error reporting with precise location information
Implementations§
Source§impl SourceText
 
impl SourceText
Sourcepub fn apply_edits(&mut self, edits: &[TextEdit]) -> usize
 
pub fn apply_edits(&mut self, edits: &[TextEdit]) -> usize
Applies multiple text edits to the source text and returns the minimum affected offset.
This method is used for incremental updates to source code, such as those received from LSP clients or other text editing operations.
§Arguments
- edits- A slice of- TextEditoperations to apply
§Returns
The minimum byte offset that was affected by any of the edits. This is useful for determining where to restart parsing after incremental changes.
§Examples
let mut source = SourceText::new("let x = 5;");
let edits = vec![TextEdit { span: 4..5, text: "y".to_string() }];
let min_offset = source.apply_edits(&edits);
assert_eq!(min_offset, 4);Sourcepub fn view(&self, range: Range<usize>) -> SourceView<'_>
 
pub fn view(&self, range: Range<usize>) -> SourceView<'_>
Creates a new SourceText containing a slice of the original text.
This method extracts a portion of the source text and creates a new
SourceText instance with the extracted content. The line map is
rebuilt for the new content.
§Arguments
- range- The byte range to extract from the original text
§Returns
A new SourceText instance containing the extracted text slice
§Examples
let source = SourceText::new("fn main() {\n    println!(\"Hello\");\n}");
let slice = source.slice(0..12); // "fn main() {"Sourcepub fn get_url(&self) -> Option<&Url>
 
pub fn get_url(&self) -> Option<&Url>
Gets the URL associated with this source text, if any.
§Returns
An Option<&Url> containing the URL reference if one was set,
or None if no URL is associated with this source text.
§Examples
let source = SourceText::new_with_url("code", Url::parse("file:///main.rs").unwrap());
assert!(source.get_url().is_some());Source§impl SourceText
 
impl SourceText
Sourcepub fn new_with_url(input: impl ToString, url: Url) -> Self
 
pub fn new_with_url(input: impl ToString, url: Url) -> Self
Sourcepub fn lsp_to_text_edit(&self, edit: TextEdit) -> TextEdit
 
pub fn lsp_to_text_edit(&self, edit: TextEdit) -> TextEdit
Sourcepub fn syntax_error(
    &self,
    message: impl Into<String>,
    offset: usize,
) -> OakError
 
pub fn syntax_error( &self, message: impl Into<String>, offset: usize, ) -> OakError
Creates a kind error with location information.
§Arguments
- message- The error message
- offset- The byte offset where the error occurred
§Returns
A PexError with precise location information including line and column.
§Examples
let source = SourceText::new("let x =");
let error = source.syntax_error("Unexpected end of input", 7);Creates a kind error with location information.
§Arguments
- message- The error message
- offset- The byte offset where the error occurred
§Returns
A PexError with precise location information including line and column.
§Examples
let source = SourceText::new("let x =");
let error = source.syntax_error("Unexpected end of input", 7);Sourcepub fn unexpected_character(&self, character: char, offset: usize) -> OakError
 
pub fn unexpected_character(&self, character: char, offset: usize) -> OakError
Creates an error for an unexpected character with location information.
§Arguments
- character- The unexpected character
- offset- The byte offset where the unexpected character was found
§Returns
A PexError with precise location information including line and column.
§Examples
let source = SourceText::new("let x@y = 5");
let error = source.unexpected_character('@', 6);Sourcepub fn get_location(&self, offset: usize) -> SourceLocation
 
pub fn get_location(&self, offset: usize) -> SourceLocation
Gets the source location for a given byte offset.
§Arguments
- offset- The byte offset to get location for
§Returns
A SourceLocation with line, column, and optional URL information.
§Examples
let source = SourceText::new("line 1\nline 2\n");
let location = source.get_location(8); // Start of second line
assert_eq!(location.line, 2);
assert_eq!(location.column, 0);Trait Implementations§
Source§impl Clone for SourceText
 
impl Clone for SourceText
Source§fn clone(&self) -> SourceText
 
fn clone(&self) -> SourceText
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
 
fn clone_from(&mut self, source: &Self)
source. Read more