Crate single_whitespace_text_editing

Source
Expand description

A library for text editing operations with single whitespace handling.

This library provides the SingleWhitespaceEditAction enum and its associated functions for performing text editing operations based on the input character. The library ensures that there is always exactly one whitespace character in a row, automatically removing any additional whitespace when necessary.

A library for text editing operations with single whitespace handling.

This library provides the SingleWhitespaceEditAction enum and its associated functions for performing text editing operations based on the input character. The library ensures that there is always exactly one whitespace character in a row, automatically removing any additional whitespace when necessary.

The following operations are supported:

  • Most common characters: Adds a character at the cursor position.
  • Newline or carriage return: No operation is performed.
  • Whitespace characters (except tab): Adds a single space at the cursor position.
  • Delete (‘\x7F’): Removes the character or word forward from the cursor position.
  • Backspace (‘\x08’): Removes the character or word backward from the cursor position.
  • Any other character: No operation is performed.

§Examples

§Adding a Character

use std::str::FromStr;

use single_whitespace_text_editing::SingleWhitespaceEditAction;
use text_editing::TextLine;

let mut text = TextLine::from_str("Hello World").unwrap();
let mut cursor = 11;
let edit_op = SingleWhitespaceEditAction::new('!', false).unwrap();
edit_op.apply(&mut text, &mut cursor);
assert_eq!(text.as_str(), "Hello World!");

§Adding a Space

use std::str::FromStr;

use single_whitespace_text_editing::SingleWhitespaceEditAction;
use text_editing::TextLine;

let mut text = TextLine::from_str("HelloWorld").unwrap();
let mut cursor = 5;
let edit_op = SingleWhitespaceEditAction::new(' ', false).unwrap();
edit_op.apply(&mut text, &mut cursor);
assert_eq!(text.as_str(), "Hello World");

§Pressing Space Before a Space

use std::str::FromStr;

use single_whitespace_text_editing::SingleWhitespaceEditAction;
use text_editing::TextLine;

let mut text = TextLine::from_str("Hello World").unwrap();
let mut cursor = 5;
let edit_op = SingleWhitespaceEditAction::new(' ', false).unwrap();
edit_op.apply(&mut text, &mut cursor);
assert_eq!(text.as_str(), "Hello World");
assert_eq!(cursor, 6);

§Removing multiple Characters with Backspace

use std::str::FromStr;

use single_whitespace_text_editing::SingleWhitespaceEditAction;
use text_editing::TextLine;

let mut text = TextLine::from_str("Hello World!").unwrap();
let mut cursor = 6;
let edit_op = SingleWhitespaceEditAction::new('\x08', true).unwrap();
edit_op.apply(&mut text, &mut cursor);
assert_eq!(text.as_str(), "World!");

§Removing a Character, Resulting in Double Spaces

use std::str::FromStr;

use single_whitespace_text_editing::SingleWhitespaceEditAction;
use text_editing::TextLine;

let mut text = TextLine::from_str("Hello , World!").unwrap();
let mut cursor = 7;
let edit_op = SingleWhitespaceEditAction::new('\x08', false).unwrap();
edit_op.apply(&mut text, &mut cursor);
assert_eq!(text.as_str(), "Hello World!");

§Removing a Character with Delete

use std::str::FromStr;

use single_whitespace_text_editing::SingleWhitespaceEditAction;
use text_editing::TextLine;

let mut text = TextLine::from_str("Hello World!").unwrap();
let mut cursor = 5;
let edit_op = SingleWhitespaceEditAction::new('\x7F', false).unwrap();
edit_op.apply(&mut text, &mut cursor);
assert_eq!(text.as_str(), "HelloWorld!");

Structs§

SingleWhitespaceEditAction
Represents text editing operations.