Skip to content

Commit

Permalink
rename to work_break_chars
Browse files Browse the repository at this point in the history
  • Loading branch information
ynqa committed Mar 25, 2024
1 parent e2a00be commit a1dd195
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 35 deletions.
54 changes: 30 additions & 24 deletions src/core/text_editor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashSet;

use crate::{
core::cursor::Cursor,
grapheme::{Grapheme, Graphemes},
Expand Down Expand Up @@ -117,41 +119,41 @@ impl TextEditor {
}
}

/// Finds the nearest previous index of any character in `items` from the cursor position.
fn find_previous_nearest_index(&self, items: &[char]) -> usize {
/// Finds the nearest previous index of any character in `word_break_chars` from the cursor position.
fn find_previous_nearest_index(&self, word_break_chars: &HashSet<char>) -> usize {
let current_position = self.position();
self.text()
.chars()
.iter()
.enumerate()
.filter(|&(i, _)| i < current_position.saturating_sub(1))
.rev()
.find(|&(_, c)| items.contains(c))
.find(|&(_, c)| word_break_chars.contains(c))
.map(|(i, _)| i + 1)
.unwrap_or(0)
}

/// Erases the text from the current cursor position to the nearest previous character in `items`.
pub fn erase_to_previous_nearest(&mut self, items: &[char]) {
let pos = self.find_previous_nearest_index(items);
/// Erases the text from the current cursor position to the nearest previous character in `word_break_chars`.
pub fn erase_to_previous_nearest(&mut self, word_break_chars: &HashSet<char>) {
let pos = self.find_previous_nearest_index(word_break_chars);
self.erase_to_position(pos);
}

/// Moves the cursor to the nearest previous character in `items`.
pub fn move_to_previous_nearest(&mut self, items: &[char]) {
let pos = self.find_previous_nearest_index(items);
/// Moves the cursor to the nearest previous character in `word_break_chars`.
pub fn move_to_previous_nearest(&mut self, word_break_chars: &HashSet<char>) {
let pos = self.find_previous_nearest_index(word_break_chars);
self.0.move_to(pos);
}

/// Finds the nearest next index of any character in `items` from the cursor position.
fn find_next_nearest_index(&self, items: &[char]) -> usize {
/// Finds the nearest next index of any character in `word_break_chars` from the cursor position.
fn find_next_nearest_index(&self, word_break_chars: &HashSet<char>) -> usize {
let current_position = self.position();
self.text()
.chars()
.iter()
.enumerate()
.filter(|&(i, _)| i > current_position)
.find(|&(_, c)| items.contains(c))
.find(|&(_, c)| word_break_chars.contains(c))
.map(|(i, _)| {
if i < self.0.contents().len() - 1 {
i + 1
Expand All @@ -162,15 +164,15 @@ impl TextEditor {
.unwrap_or(self.0.contents().len() - 1)
}

/// Erases the text from the current cursor position to the nearest next character in `items`.
pub fn erase_to_next_nearest(&mut self, items: &[char]) {
let pos = self.find_next_nearest_index(items);
/// Erases the text from the current cursor position to the nearest next character in `word_break_chars`.
pub fn erase_to_next_nearest(&mut self, word_break_chars: &HashSet<char>) {
let pos = self.find_next_nearest_index(word_break_chars);
self.erase_to_position(pos);
}

/// Moves the cursor to the nearest next character in `items`.
pub fn move_to_next_nearest(&mut self, items: &[char]) {
let pos = self.find_next_nearest_index(items);
/// Moves the cursor to the nearest next character in `word_break_chars`.
pub fn move_to_next_nearest(&mut self, word_break_chars: &HashSet<char>) {
let pos = self.find_next_nearest_index(word_break_chars);
self.0.move_to(pos);
}

Expand Down Expand Up @@ -269,38 +271,42 @@ mod test {
}

mod find_previous_nearest_index {
use std::collections::HashSet;

use crate::text_editor::test::new_with_position;

#[test]
fn test() {
let mut txt = new_with_position(String::from("koko momo jojo "), 11); // indicate `o`.
assert_eq!(10, txt.find_previous_nearest_index(&[' ']));
assert_eq!(10, txt.find_previous_nearest_index(&HashSet::from([' '])));
txt.0.move_to(10);
assert_eq!(5, txt.find_previous_nearest_index(&[' ']));
assert_eq!(5, txt.find_previous_nearest_index(&HashSet::from([' '])));
}

#[test]
fn test_with_no_target() {
let txt = new_with_position(String::from("koko momo jojo "), 7); // indicate `m`.
assert_eq!(0, txt.find_previous_nearest_index(&['z']));
assert_eq!(0, txt.find_previous_nearest_index(&HashSet::from(['z'])));
}
}

mod find_next_nearest_index {
use std::collections::HashSet;

use crate::text_editor::test::new_with_position;

#[test]
fn test() {
let mut txt = new_with_position(String::from("koko momo jojo "), 7); // indicate `m`.
assert_eq!(10, txt.find_next_nearest_index(&[' ']));
assert_eq!(10, txt.find_next_nearest_index(&HashSet::from([' '])));
txt.0.move_to(10);
assert_eq!(14, txt.find_next_nearest_index(&[' ']));
assert_eq!(14, txt.find_next_nearest_index(&HashSet::from([' '])));
}

#[test]
fn test_with_no_target() {
let txt = new_with_position(String::from("koko momo jojo "), 7); // indicate `m`.
assert_eq!(14, txt.find_next_nearest_index(&['z']));
assert_eq!(14, txt.find_next_nearest_index(&HashSet::from(['z'])));
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/core/text_editor/render.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashSet;

use crate::{
crossterm::style::ContentStyle,
grapheme::{matrixify, StyledGraphemes},
Expand Down Expand Up @@ -36,8 +38,8 @@ pub struct Renderer {

/// Current edit mode, determining whether input inserts or overwrites existing text.
pub edit_mode: Mode,
/// Characters to be considered for nearest navigation.
pub nearest_characters: Vec<char>,
/// Characters to be for word break.
pub word_break_chars: HashSet<char>,
/// Number of lines available for rendering.
pub lines: Option<usize>,
}
Expand Down
2 changes: 1 addition & 1 deletion src/preset/query_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl QuerySelector {
active_char_style: StyleBuilder::new().bgc(Color::DarkCyan).build(),
inactive_char_style: StyleBuilder::new().build(),
edit_mode: Default::default(),
nearest_characters: Default::default(),
word_break_chars: Default::default(),
lines: Default::default(),
},
listbox_renderer: listbox::Renderer {
Expand Down
10 changes: 6 additions & 4 deletions src/preset/readline.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashSet;

use crate::{
crossterm::{
event::Event,
Expand Down Expand Up @@ -58,7 +60,7 @@ impl Default for Readline {
active_char_style: StyleBuilder::new().bgc(Color::DarkCyan).build(),
inactive_char_style: StyleBuilder::new().build(),
edit_mode: Default::default(),
nearest_characters: vec![' '],
word_break_chars: HashSet::from([' ']),
lines: Default::default(),
},
suggest: Default::default(),
Expand Down Expand Up @@ -145,9 +147,9 @@ impl Readline {
self
}

/// Sets the characters to be considered for nearest navigation.
pub fn nearest_characters(mut self, characters: Vec<char>) -> Self {
self.text_editor_renderer.nearest_characters = characters;
/// Sets the characters to be for word break.
pub fn word_break_chars(mut self, characters: HashSet<char>) -> Self {
self.text_editor_renderer.word_break_chars = characters;
self
}

Expand Down
8 changes: 4 additions & 4 deletions src/preset/readline/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub fn default(
state: KeyEventState::NONE,
}) => text_editor_after_mut
.texteditor
.move_to_previous_nearest(&text_editor_after_mut.nearest_characters),
.move_to_previous_nearest(&text_editor_after_mut.word_break_chars),

Event::Key(KeyEvent {
code: KeyCode::Char('f'),
Expand All @@ -140,7 +140,7 @@ pub fn default(
state: KeyEventState::NONE,
}) => text_editor_after_mut
.texteditor
.move_to_next_nearest(&text_editor_after_mut.nearest_characters),
.move_to_next_nearest(&text_editor_after_mut.word_break_chars),

// Erase char(s).
Event::Key(KeyEvent {
Expand All @@ -164,7 +164,7 @@ pub fn default(
state: KeyEventState::NONE,
}) => text_editor_after_mut
.texteditor
.erase_to_previous_nearest(&text_editor_after_mut.nearest_characters),
.erase_to_previous_nearest(&text_editor_after_mut.word_break_chars),

Event::Key(KeyEvent {
code: KeyCode::Char('d'),
Expand All @@ -173,7 +173,7 @@ pub fn default(
state: KeyEventState::NONE,
}) => text_editor_after_mut
.texteditor
.erase_to_next_nearest(&text_editor_after_mut.nearest_characters),
.erase_to_next_nearest(&text_editor_after_mut.word_break_chars),

// Choose history
Event::Key(KeyEvent {
Expand Down

0 comments on commit a1dd195

Please sign in to comment.