Skip to main content

TextBuffer

Struct TextBuffer 

Source
pub struct TextBuffer { /* private fields */ }
Expand description

Re-export of TextBuffer — a rope-based text buffer for efficient editing.

Use TextBuffer::new to create a buffer from a string slice. A rope-based text buffer for efficient editing operations.

TextBuffer wraps a ropey::Rope to provide fast insertions, deletions, and character counting on texts of any size — from short snippets to files with millions of lines.

§Invariants

  • The internal rope is always well-formed (never None or in an undefined state) after construction via TextBuffer::new.
  • Character counts returned by TextBuffer::len_chars count Unicode scalar values, so multi-byte characters like emoji are counted as a single character.

§Example

use dscode_core::TextBuffer;

let buffer = TextBuffer::new("Hello, world!");
assert_eq!(buffer.len_chars(), 13);
assert_eq!(buffer.get_text(), "Hello, world!");
assert!(!buffer.is_empty());

Implementations§

Source§

impl TextBuffer

Source

pub fn new(content: &str) -> Self

Create a new text buffer initialised with the given content.

The entire content string is copied into an internal rope data structure. For an empty buffer, pass "".

§Arguments
  • content — The initial text content of the buffer. An empty string produces an empty buffer.
§Example
use dscode_core::TextBuffer;

let buffer = TextBuffer::new("some text");
Examples found in repository?
examples/basic_text_buffer.rs (line 9)
7fn main() {
8    // Create a new buffer from a string
9    let buffer = TextBuffer::new("Hello, world!");
10    println!("Text: {}", buffer.get_text());
11    println!("Length: {} chars", buffer.len_chars());
12    println!("Is empty: {}", buffer.is_empty());
13
14    // Create an empty buffer
15    let empty = TextBuffer::new("");
16    println!("Empty buffer length: {}", empty.len_chars());
17    assert!(empty.is_empty());
18}
Source

pub fn get_text(&self) -> String

Get the full text content of the buffer as a String.

This copies the entire rope contents into a new string. For very large buffers, consider whether you truly need the whole text at once.

§Example
use dscode_core::TextBuffer;

let buffer = TextBuffer::new("abc");
assert_eq!(buffer.get_text(), "abc");
Examples found in repository?
examples/basic_text_buffer.rs (line 10)
7fn main() {
8    // Create a new buffer from a string
9    let buffer = TextBuffer::new("Hello, world!");
10    println!("Text: {}", buffer.get_text());
11    println!("Length: {} chars", buffer.len_chars());
12    println!("Is empty: {}", buffer.is_empty());
13
14    // Create an empty buffer
15    let empty = TextBuffer::new("");
16    println!("Empty buffer length: {}", empty.len_chars());
17    assert!(empty.is_empty());
18}
Source

pub fn len_chars(&self) -> usize

Get the number of Unicode scalar values in the buffer.

This counts characters, not bytes. For example, "café" returns 4, and "🦀" returns 1.

§Example
use dscode_core::TextBuffer;

let buffer = TextBuffer::new("café");
assert_eq!(buffer.len_chars(), 4);
Examples found in repository?
examples/basic_text_buffer.rs (line 11)
7fn main() {
8    // Create a new buffer from a string
9    let buffer = TextBuffer::new("Hello, world!");
10    println!("Text: {}", buffer.get_text());
11    println!("Length: {} chars", buffer.len_chars());
12    println!("Is empty: {}", buffer.is_empty());
13
14    // Create an empty buffer
15    let empty = TextBuffer::new("");
16    println!("Empty buffer length: {}", empty.len_chars());
17    assert!(empty.is_empty());
18}
Source

pub fn is_empty(&self) -> bool

Check whether the buffer contains no characters.

Returns true when TextBuffer::len_chars is zero.

§Example
use dscode_core::TextBuffer;

let buffer = TextBuffer::new("");
assert!(buffer.is_empty());
Examples found in repository?
examples/basic_text_buffer.rs (line 12)
7fn main() {
8    // Create a new buffer from a string
9    let buffer = TextBuffer::new("Hello, world!");
10    println!("Text: {}", buffer.get_text());
11    println!("Length: {} chars", buffer.len_chars());
12    println!("Is empty: {}", buffer.is_empty());
13
14    // Create an empty buffer
15    let empty = TextBuffer::new("");
16    println!("Empty buffer length: {}", empty.len_chars());
17    assert!(empty.is_empty());
18}

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, 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<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more