pub struct SourceContent { /* private fields */ }
Expand description
Represents key information about a source file and its content:
- The path to the file (or its name, in the case of virtual files)
- The content of the file
- The byte offsets of every line in the file, for use in looking up line/column information
Implementations§
Source§impl SourceContent
impl SourceContent
Sourcepub fn new(
language: impl AsRef<str>,
uri: impl Into<Uri>,
content: impl Into<String>,
) -> Self
pub fn new( language: impl AsRef<str>, uri: impl Into<Uri>, content: impl Into<String>, ) -> Self
Create a new SourceContent from the (possibly virtual) file path, and its content as a UTF-8 string.
When created, the line starts for this file will be computed, which requires scanning the file content once.
Sourcepub fn set_version(&mut self, version: i32)
pub fn set_version(&mut self, version: i32)
Set the current version of this content
Sourcepub fn source_range(&self) -> Range<ByteIndex>
pub fn source_range(&self) -> Range<ByteIndex>
Returns the range of valid byte indices for this file
Sourcepub fn source_slice(&self, span: impl Into<Range<usize>>) -> Option<&str>
pub fn source_slice(&self, span: impl Into<Range<usize>>) -> Option<&str>
Returns a subset of the underlying content as a string slice.
The bounds of the given span are character indices, not byte indices.
Returns None
if the given span is out of bounds
Sourcepub fn byte_slice(&self, span: impl Into<Range<ByteIndex>>) -> Option<&[u8]>
pub fn byte_slice(&self, span: impl Into<Range<ByteIndex>>) -> Option<&[u8]>
Returns a subset of the underlying content as a byte slice.
Returns None
if the given span is out of bounds
Sourcepub fn select(&self, range: Selection) -> Option<&str>
pub fn select(&self, range: Selection) -> Option<&str>
Like Self::source_slice, but the slice is computed like a selection in an editor, i.e. based on line/column positions, rather than raw character indices.
This is useful when mapping LSP operations to content in the source file.
Sourcepub fn line_count(&self) -> usize
pub fn line_count(&self) -> usize
Returns the number of lines in the source content
Sourcepub fn line_start(&self, line_index: LineIndex) -> Option<ByteIndex>
pub fn line_start(&self, line_index: LineIndex) -> Option<ByteIndex>
Returns the byte index at which the line corresponding to line_index
starts
Returns None
if the given index is out of bounds
Sourcepub fn last_line_index(&self) -> LineIndex
pub fn last_line_index(&self) -> LineIndex
Returns the index of the last line in this file
Sourcepub fn line_range(&self, line_index: LineIndex) -> Option<Range<ByteIndex>>
pub fn line_range(&self, line_index: LineIndex) -> Option<Range<ByteIndex>>
Get the range of byte indices covered by the given line
Sourcepub fn line_index(&self, byte_index: ByteIndex) -> LineIndex
pub fn line_index(&self, byte_index: ByteIndex) -> LineIndex
Get the index of the line to which byte_index
belongs
Sourcepub fn line_column_to_offset(
&self,
line_index: LineIndex,
column_index: ColumnIndex,
) -> Option<ByteIndex>
pub fn line_column_to_offset( &self, line_index: LineIndex, column_index: ColumnIndex, ) -> Option<ByteIndex>
Get the ByteIndex corresponding to the given line and column indices.
Returns None
if the line or column indices are out of bounds.
Sourcepub fn location(&self, byte_index: ByteIndex) -> Option<FileLineCol>
pub fn location(&self, byte_index: ByteIndex) -> Option<FileLineCol>
Get a FileLineCol corresponding to the line/column in this file at which byte_index
occurs
Sourcepub fn update(
&mut self,
text: String,
range: Option<Selection>,
version: i32,
) -> Result<(), SourceContentUpdateError>
pub fn update( &mut self, text: String, range: Option<Selection>, version: i32, ) -> Result<(), SourceContentUpdateError>
Update the source document after being notified of a change event.
The version
indicates the new version of the document
NOTE: This is intended to update a super::SourceManager’s view of the content of the document, not to perform an update against the actual file, wherever it may be.
Trait Implementations§
Source§impl Clone for SourceContent
impl Clone for SourceContent
Source§fn clone(&self) -> SourceContent
fn clone(&self) -> SourceContent
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more