Struct text_editing::TextLine
source · pub struct TextLine { /* private fields */ }
Expand description
The text line represents editable text lines.
Implementations§
source§impl TextLine
impl TextLine
sourcepub fn cursor_movement(
movement: Direction,
skip: bool,
) -> fn(_: &Self, _: &mut usize) -> bool
pub fn cursor_movement( movement: Direction, skip: bool, ) -> fn(_: &Self, _: &mut usize) -> bool
Returns the appropriate movement function based on parameters movement
and skip
.
§Arguments
movement
- The direction of movement (Forward
orBackward
).skip
- Whether to skip over whitespace characters.
§Returns
A function pointer to the appropriate movement function.
§Examples
use text_editing::{TextLine, Direction};
let line = TextLine::from_string("Hello, world!".into());
let mut text_cursor = 7;
let movement_fn = TextLine::cursor_movement(Direction::Forward, false);
movement_fn(&line, &mut text_cursor);
assert_eq!(text_cursor, 8);
sourcepub fn forward(&self, text_cursor: &mut usize) -> bool
pub fn forward(&self, text_cursor: &mut usize) -> bool
Moves the text cursor forward by one.
§Arguments
text_cursor
- A mutable reference to the current text cursor position.
§Returns
true
if the cursor was successfully moved forward, false
otherwise.
§Examples
use text_editing::TextLine;
let line = TextLine::from_string("Hello, world!".into());
let mut text_cursor = 6;
assert!(line.forward(&mut text_cursor));
assert_eq!(text_cursor, 7);
sourcepub fn backward(&self, text_cursor: &mut usize) -> bool
pub fn backward(&self, text_cursor: &mut usize) -> bool
Moves the text cursor backward by one.
§Arguments
text_cursor
- A mutable reference to the current text cursor position.
§Returns
true
if the cursor was successfully moved backward, false
otherwise.
§Examples
use text_editing::TextLine;
let line = TextLine::from_string("Hello, world!".into());
let mut text_cursor = 7;
assert!(line.backward(&mut text_cursor));
assert_eq!(text_cursor, 6);
sourcepub fn skip_forward(&self, text_cursor: &mut usize) -> bool
pub fn skip_forward(&self, text_cursor: &mut usize) -> bool
Moves the text cursor forward until the end of the current word.
§Arguments
text_cursor
- A mutable reference to the current text cursor position.
§Returns
true
if the cursor was successfully moved to the end of the word, false
otherwise.
§Examples
use text_editing::TextLine;
let line = TextLine::from_string("Hello, world!".into());
let mut text_cursor = 6;
assert!(line.skip_forward(&mut text_cursor));
assert_eq!(text_cursor, 12);
sourcepub fn skip_backward(&self, text_cursor: &mut usize) -> bool
pub fn skip_backward(&self, text_cursor: &mut usize) -> bool
Moves the text cursor back until the start of the current word.
§Arguments
text_cursor
- A mutable reference to the current text cursor position.
§Returns
true
if the cursor was successfully moved to the start of the word, false
otherwise.
§Examples
use text_editing::TextLine;
let line = TextLine::from_string("Hello, world!".into());
let mut text_cursor = 10;
assert!(line.skip_backward(&mut text_cursor));
assert_eq!(text_cursor, 7);
source§impl TextLine
impl TextLine
sourcepub fn add_char(&mut self, text_cursor: &mut usize, c: char)
pub fn add_char(&mut self, text_cursor: &mut usize, c: char)
Adds a char at the current text cursor position.
§Arguments
text_cursor
- A mutable reference to the current text cursor position.c
- The char to add.
§Examples
use text_editing::TextLine;
let mut line = TextLine::from_string("Hello, ".into());
let mut text_cursor = 7;
line.add_char(&mut text_cursor, 'w');
assert_eq!(line.as_str(), "Hello, w");
assert_eq!(text_cursor, 8);
sourcepub fn remove_forward(&mut self, text_cursor: usize) -> bool
pub fn remove_forward(&mut self, text_cursor: usize) -> bool
Removes the next char at the current text cursor position, if it exists.
§Arguments
text_cursor
- The current text cursor position.
§Returns
true
if a char was removed, false
otherwise.
§Examples
use text_editing::TextLine;
let mut line = TextLine::from_string("Hello, world!".into());
let text_cursor = 7;
assert!(line.remove_forward(text_cursor));
assert_eq!(line.as_str(), "Hello, orld!");
sourcepub fn remove_forward_skip(&mut self, text_cursor: usize) -> bool
pub fn remove_forward_skip(&mut self, text_cursor: usize) -> bool
Removes the next word at the current text cursor position, if it exists.
§Arguments
text_cursor
- The current text cursor position.
§Returns
true
if a word was removed, false
otherwise.
§Examples
use text_editing::TextLine;
let mut line = TextLine::from_string("Hello, world!".into());
let text_cursor = 7;
assert!(line.remove_forward_skip(text_cursor));
assert_eq!(line.as_str(), "Hello, !");
sourcepub fn remove_backward(&mut self, text_cursor: &mut usize) -> bool
pub fn remove_backward(&mut self, text_cursor: &mut usize) -> bool
Removes the previous char at the current text cursor position, if it exists, and updates the text cursor.
§Arguments
text_cursor
- A mutable reference to the current text cursor position.
§Returns
true
if a char was removed, false
otherwise.
§Examples
use text_editing::TextLine;
let mut line = TextLine::from_string("Hello, world!".into());
let mut text_cursor = 7;
assert!(line.remove_backward(&mut text_cursor));
assert_eq!(line.as_str(), "Hello,world!");
assert_eq!(text_cursor, 6);
sourcepub fn remove_backward_skip(&mut self, text_cursor: &mut usize) -> bool
pub fn remove_backward_skip(&mut self, text_cursor: &mut usize) -> bool
Removes the previous word at the current text cursor position, if it exists, and updates the text cursor.
§Arguments
text_cursor
- A mutable reference to the current text cursor position.
§Returns
true
if a word was removed, false
otherwise.
§Examples
use text_editing::TextLine;
let mut line = TextLine::from_string("Hello, world!".into());
let mut text_cursor = 12;
assert!(line.remove_backward_skip(&mut text_cursor));
assert_eq!(line.as_str(), "Hello, !");
assert_eq!(text_cursor, 7);
source§impl TextLine
impl TextLine
sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty text line.
§Examples
use text_editing::TextLine;
let line = TextLine::new();
assert!(line.is_empty());
sourcepub fn from_string(text: String) -> Self
pub fn from_string(text: String) -> Self
sourcepub fn string_index(&self, index: usize) -> usize
pub fn string_index(&self, index: usize) -> usize
sourcepub fn remove_range(&mut self, range: Range<usize>)
pub fn remove_range(&mut self, range: Range<usize>)
sourcepub fn split(&mut self, index: usize) -> Self
pub fn split(&mut self, index: usize) -> Self
Splits a text line into two.
§Arguments
index
- The position to split the text line at.
§Returns
The new text line containing the text after the split position.
§Examples
use text_editing::TextLine;
let mut line = TextLine::from_string("Hello, world!".into());
let second_half = line.split(7);
assert_eq!(line.as_str(), "Hello, ");
assert_eq!(second_half.as_str(), "world!");
sourcepub fn join(&mut self, other: Self)
pub fn join(&mut self, other: Self)
Joins two text lines into one.
§Arguments
other
- The text line to append to the current text line.
§Examples
use text_editing::TextLine;
let mut line1 = TextLine::from_string("Hello, ".into());
let line2 = TextLine::from_string("world!".into());
line1.join(line2);
assert_eq!(line1.as_str(), "Hello, world!");
Trait Implementations§
Auto Trait Implementations§
impl Freeze for TextLine
impl RefUnwindSafe for TextLine
impl Send for TextLine
impl Sync for TextLine
impl Unpin for TextLine
impl UnwindSafe for TextLine
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
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)