SourceContent

Struct SourceContent 

Source
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

Source

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.

Source

pub fn language(&self) -> &str

Get the language identifier of this source file

Source

pub fn version(&self) -> i32

Get the current version of this source file’s content

Source

pub fn set_version(&mut self, version: i32)

Set the current version of this content

Source

pub fn uri(&self) -> &Uri

Get the URI of this source file

Source

pub fn as_str(&self) -> &str

Returns the underlying content as a string slice

Source

pub fn as_bytes(&self) -> &[u8]

Returns the underlying content as a byte slice

Source

pub fn len(&self) -> usize

Returns the size in bytes of the underlying content

Source

pub fn is_empty(&self) -> bool

Returns true if the underlying content is empty

Source

pub fn source_range(&self) -> Range<ByteIndex>

Returns the range of valid byte indices for this file

Source

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 byte indices, not character indices.

Returns None if the given span is out of bounds, or if the bounds do not fall on valid UTF-8 character boundaries.

Source

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

Source

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.

Source

pub fn line_count(&self) -> usize

Returns the number of lines in the source content

Source

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

Source

pub fn last_line_index(&self) -> LineIndex

Returns the index of the last line in this file

Source

pub fn line_range(&self, line_index: LineIndex) -> Option<Range<ByteIndex>>

Get the range of byte indices covered by the given line

Source

pub fn line_index(&self, byte_index: ByteIndex) -> LineIndex

Get the index of the line to which byte_index belongs

Source

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.

Source

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

Source

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

Source§

fn clone(&self) -> SourceContent

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SourceContent

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for SourceContent

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Hash for SourceContent

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for SourceContent

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for SourceContent

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for SourceContent

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for SourceContent

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for SourceContent

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,