text-editing 0.3.0

A simple string with utilities for editing
Documentation
use super::TextLine;

impl TextLine {
    /// 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);
    /// ```
    pub fn add_char(&mut self, text_cursor: &mut usize, c: char) {
        self.insert(*text_cursor, c);
        *text_cursor += 1;
    }

    /// 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!");
    /// ```
    pub fn remove_forward(&mut self, text_cursor: usize) -> bool {
        let remove = text_cursor < self.len();
        if remove {
            self.remove(text_cursor);
        }
        remove
    }

    /// 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, !");
    /// ```
    pub fn remove_forward_skip(&mut self, text_cursor: usize) -> bool {
        let mut end_cursor = text_cursor;
        self.skip_forward(&mut end_cursor);
        let range = text_cursor..end_cursor;
        let moved = !range.is_empty();
        if moved {
            self.remove_range(range);
        }
        moved
    }

    /// 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);
    /// ```
    pub fn remove_backward(&mut self, text_cursor: &mut usize) -> bool {
        let remove = *text_cursor > 0;
        if remove {
            *text_cursor -= 1;
            self.remove(*text_cursor);
        }
        remove
    }

    /// 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);
    /// ```
    pub fn remove_backward_skip(&mut self, text_cursor: &mut usize) -> bool {
        let end_cursor = *text_cursor;
        self.skip_backward(text_cursor);
        let range = *text_cursor..end_cursor;
        let moved = !range.is_empty();
        if moved {
            self.remove_range(range);
        }
        moved
    }

    /// 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);
    /// ```
    pub const fn cursor_to_start(&self, text_cursor: &mut usize) {
        *text_cursor = 0;
    }

    /// 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);
    /// ```
    pub const fn cursor_to_end(&self, text_cursor: &mut usize) {
        *text_cursor = self.len();
    }

    /// 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);
    /// ```
    pub fn insert_str(&mut self, text_cursor: &mut usize, text: &str) {
        let pos = *text_cursor;
        let byte_index = self.string_index(pos);
        let mut count = 0;
        let mut total_extra = 0;
        for c in text.chars() {
            count += 1;
            total_extra += c.len_utf8() - 1;
        }
        self.text.insert_str(byte_index, text);
        *text_cursor = pos + count;
        if !self.indices.is_empty() {
            let base = if pos > 0 { self.indices[pos - 1] } else { 0 };
            self.indices.splice(
                pos..pos,
                text.chars().scan(base, |running, c| {
                    *running += c.len_utf8() - 1;
                    Some(*running)
                }),
            );
            for v in &mut self.indices[pos + count..] {
                *v += total_extra;
            }
        } else if total_extra > 0 {
            self.enable_indices();
        }
    }
}