Skip to main content

TextLine

Struct TextLine 

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

The text line represents editable text lines.

Implementations§

Source§

impl TextLine

Source

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 or Backward).
  • 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);
Source

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);
Source

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);
Source

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);
Source

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

Source

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);
Source

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!");
Source

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, !");
Source

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);
Source

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

pub fn cursor_to_start(&self, text_cursor: &mut usize)

Moves the text cursor to the start of the line.

§Arguments
  • text_cursor - A mutable reference to the current text cursor position.
§Examples
use text_editing::TextLine;

let line = TextLine::from_string("Hello, world!".into());
let mut text_cursor = 7;
line.cursor_to_start(&mut text_cursor);
assert_eq!(text_cursor, 0);
Source

pub fn cursor_to_end(&self, text_cursor: &mut usize)

Moves the text cursor to the end of the line.

§Arguments
  • text_cursor - A mutable reference to the current text cursor position.
§Examples
use text_editing::TextLine;

let line = TextLine::from_string("Hello, world!".into());
let mut text_cursor = 7;
line.cursor_to_end(&mut text_cursor);
assert_eq!(text_cursor, 13);
Source

pub fn insert_str(&mut self, text_cursor: &mut usize, text: &str)

Inserts a string at the current text cursor position and advances the cursor.

§Arguments
  • text_cursor - A mutable reference to the current text cursor position.
  • text - The string to insert.
§Examples
use text_editing::TextLine;

let mut line = TextLine::from_string("Hello, !".into());
let mut text_cursor = 7;
line.insert_str(&mut text_cursor, "world");
assert_eq!(line.as_str(), "Hello, world!");
assert_eq!(text_cursor, 12);
Source§

impl TextLine

Source

pub fn selection_range( cursor: usize, anchor: Option<usize>, ) -> Option<Range<usize>>

Returns the ordered selection range from an anchor and cursor position.

Returns None if no anchor is set.

§Arguments
  • cursor - The current text cursor position.
  • anchor - The optional selection anchor position.
§Examples
use text_editing::TextLine;

assert_eq!(TextLine::selection_range(10, Some(5)), Some(5..10));
assert_eq!(TextLine::selection_range(3, Some(8)), Some(3..8));
assert_eq!(TextLine::selection_range(5, None), None);
Source

pub fn selected_text( &self, cursor: usize, anchor: Option<usize>, ) -> Option<&str>

Returns the selected text, if a selection is active.

§Arguments
  • cursor - The current text cursor position.
  • anchor - The optional selection anchor position.
§Examples
use text_editing::TextLine;

let line = TextLine::from_string("Hello, world!".into());
assert_eq!(line.selected_text(12, Some(7)), Some("world"));
assert_eq!(line.selected_text(5, None), None);
Source

pub fn delete_selection( &mut self, cursor: &mut usize, anchor: &mut Option<usize>, ) -> bool

Deletes the currently selected text and updates cursor and anchor.

Returns true if text was deleted, false if no selection was active or the selection was empty.

§Arguments
  • cursor - A mutable reference to the current text cursor position.
  • anchor - A mutable reference to the optional selection anchor.
§Examples
use text_editing::TextLine;

let mut line = TextLine::from_string("Hello, world!".into());
let mut cursor = 12;
let mut anchor = Some(7);
assert!(line.delete_selection(&mut cursor, &mut anchor));
assert_eq!(line.as_str(), "Hello, !");
assert_eq!(cursor, 7);
assert_eq!(anchor, None);
Source

pub fn replace_selection( &mut self, cursor: &mut usize, anchor: &mut Option<usize>, text: &str, )

Replaces the current selection with the given text.

If no selection is active, inserts the text at the cursor position.

§Arguments
  • cursor - A mutable reference to the current text cursor position.
  • anchor - A mutable reference to the optional selection anchor.
  • text - The replacement text.
§Examples
use text_editing::TextLine;

let mut line = TextLine::from_string("Hello, world!".into());
let mut cursor = 12;
let mut anchor = Some(7);
line.replace_selection(&mut cursor, &mut anchor, "Rust");
assert_eq!(line.as_str(), "Hello, Rust!");
assert_eq!(cursor, 11);
assert_eq!(anchor, None);
Source

pub fn select_all(&self, cursor: &mut usize, anchor: &mut Option<usize>)

Selects all text in the line.

Sets the anchor to the beginning and the cursor to the end.

§Arguments
  • cursor - A mutable reference to the current text cursor position.
  • anchor - A mutable reference to the optional selection anchor.
§Examples
use text_editing::TextLine;

let line = TextLine::from_string("Hello, world!".into());
let mut cursor = 5;
let mut anchor = None;
line.select_all(&mut cursor, &mut anchor);
assert_eq!(cursor, 13);
assert_eq!(anchor, Some(0));
Source

pub fn select_forward( &self, cursor: &mut usize, anchor: &mut Option<usize>, ) -> bool

Extends the selection forward by one character.

Sets the anchor to the current cursor position if no selection is active, then moves the cursor forward.

§Examples
use text_editing::TextLine;

let line = TextLine::from_string("Hello, world!".into());
let mut cursor = 7;
let mut anchor = None;
assert!(line.select_forward(&mut cursor, &mut anchor));
assert_eq!(cursor, 8);
assert_eq!(anchor, Some(7));
Source

pub fn select_backward( &self, cursor: &mut usize, anchor: &mut Option<usize>, ) -> bool

Extends the selection backward by one character.

Sets the anchor to the current cursor position if no selection is active, then moves the cursor backward.

§Examples
use text_editing::TextLine;

let line = TextLine::from_string("Hello, world!".into());
let mut cursor = 7;
let mut anchor = None;
assert!(line.select_backward(&mut cursor, &mut anchor));
assert_eq!(cursor, 6);
assert_eq!(anchor, Some(7));
Source

pub fn select_forward_skip( &self, cursor: &mut usize, anchor: &mut Option<usize>, ) -> bool

Extends the selection forward by one word.

Sets the anchor to the current cursor position if no selection is active, then moves the cursor to the end of the next word.

§Examples
use text_editing::TextLine;

let line = TextLine::from_string("Hello, world!".into());
let mut cursor = 7;
let mut anchor = None;
assert!(line.select_forward_skip(&mut cursor, &mut anchor));
assert_eq!(cursor, 12);
assert_eq!(anchor, Some(7));
Source

pub fn select_backward_skip( &self, cursor: &mut usize, anchor: &mut Option<usize>, ) -> bool

Extends the selection backward by one word.

Sets the anchor to the current cursor position if no selection is active, then moves the cursor to the start of the previous word.

§Examples
use text_editing::TextLine;

let line = TextLine::from_string("Hello, world!".into());
let mut cursor = 12;
let mut anchor = None;
assert!(line.select_backward_skip(&mut cursor, &mut anchor));
assert_eq!(cursor, 7);
assert_eq!(anchor, Some(12));
Source

pub fn clear_selection(anchor: &mut Option<usize>)

Clears the current selection without modifying the text.

§Examples
use text_editing::TextLine;

let mut anchor = Some(5);
TextLine::clear_selection(&mut anchor);
assert_eq!(anchor, None);
Source§

impl TextLine

Source

pub fn new() -> Self

Creates a new empty text line.

§Examples
use text_editing::TextLine;

let line = TextLine::new();
assert!(line.is_empty());
Source

pub fn from_string(text: String) -> Self

Creates a text line from a String.

§Examples
use text_editing::TextLine;

let line = TextLine::from_string("Hello, world!".into());
assert_eq!(line.as_str(), "Hello, world!");
Source

pub fn clear(&mut self)

Clears the text line, removing all content.

§Examples
use text_editing::TextLine;

let mut line = TextLine::from_string("Hello, world!".into());
line.clear();
assert!(line.is_empty());
Source

pub fn is_empty(&self) -> bool

Checks if the line is empty.

§Examples
use text_editing::TextLine;

let line = TextLine::new();
assert!(line.is_empty());
Source

pub fn len(&self) -> usize

Returns the length of the text line.

§Examples
use text_editing::TextLine;

let line = TextLine::from_string("Hello, world!".into());
assert_eq!(line.len(), 13);
Source

pub fn as_str(&self) -> &str

Returns the text of the text line as a str reference.

§Examples
use text_editing::TextLine;

let line = TextLine::from_string("Hello, world!".into());
assert_eq!(line.as_str(), "Hello, world!");
Source

pub fn string_index(&self, index: usize) -> usize

Converts the character index to the string index.

§Examples
use text_editing::TextLine;

let line = TextLine::from_string("Hello, world!".into());
assert_eq!(line.string_index(7), 7);
Source

pub fn char_at(&self, at: usize) -> char

Returns the char at the specified position.

§Panics

Panics if the position is out of bounds.

§Examples
use text_editing::TextLine;

let line = TextLine::from_string("Hello, world!".into());
assert_eq!(line.char_at(7), 'w');
Source

pub fn insert(&mut self, index: usize, c: char)

Inserts a new char into the text line.

§Examples
use text_editing::TextLine;

let mut line = TextLine::from_string("Hello, orld!".into());
line.insert(7, 'w');
assert_eq!(line.as_str(), "Hello, world!");
Source

pub fn remove(&mut self, index: usize) -> char

Removes a char from the text line and returns it.

§Examples
use text_editing::TextLine;

let mut line = TextLine::from_string("Hello, world!".into());
assert_eq!(line.remove(7), 'w');
assert_eq!(line.as_str(), "Hello, orld!");
Source

pub fn remove_range(&mut self, range: Range<usize>)

Removes the specified range from the text line.

§Examples
use text_editing::TextLine;

let mut line = TextLine::from_string("Hello, world!".into());
line.remove_range(7..12);
assert_eq!(line.as_str(), "Hello, !");
Source

pub fn split(&mut self, index: usize) -> Self

Splits a text line into two.

§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!");
Source

pub fn join(&mut self, other: Self)

Joins two text lines into one.

§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§

Source§

impl Clone for TextLine

Source§

fn clone(&self) -> TextLine

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 Default for TextLine

Source§

fn default() -> TextLine

Returns the “default value” for a type. Read more
Source§

impl Display for TextLine

Source§

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

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

impl From<String> for TextLine

Source§

fn from(text: String) -> Self

Converts to this type from the input type.
Source§

impl From<TextLine> for String

Source§

fn from(text_line: TextLine) -> Self

Converts to this type from the input type.
Source§

impl FromStr for TextLine

Source§

type Err = Infallible

The associated error which can be returned from parsing.
Source§

fn from_str(text: &str) -> Result<Self, Infallible>

Parses a string s to return a value of this type. Read more

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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.