Skip to content

Commit a1dd195

Browse files
committed
rename to work_break_chars
1 parent e2a00be commit a1dd195

File tree

5 files changed

+45
-35
lines changed

5 files changed

+45
-35
lines changed

src/core/text_editor.rs

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::collections::HashSet;
2+
13
use crate::{
24
core::cursor::Cursor,
35
grapheme::{Grapheme, Graphemes},
@@ -117,41 +119,41 @@ impl TextEditor {
117119
}
118120
}
119121

120-
/// Finds the nearest previous index of any character in `items` from the cursor position.
121-
fn find_previous_nearest_index(&self, items: &[char]) -> usize {
122+
/// Finds the nearest previous index of any character in `word_break_chars` from the cursor position.
123+
fn find_previous_nearest_index(&self, word_break_chars: &HashSet<char>) -> usize {
122124
let current_position = self.position();
123125
self.text()
124126
.chars()
125127
.iter()
126128
.enumerate()
127129
.filter(|&(i, _)| i < current_position.saturating_sub(1))
128130
.rev()
129-
.find(|&(_, c)| items.contains(c))
131+
.find(|&(_, c)| word_break_chars.contains(c))
130132
.map(|(i, _)| i + 1)
131133
.unwrap_or(0)
132134
}
133135

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

140-
/// Moves the cursor to the nearest previous character in `items`.
141-
pub fn move_to_previous_nearest(&mut self, items: &[char]) {
142-
let pos = self.find_previous_nearest_index(items);
142+
/// Moves the cursor to the nearest previous character in `word_break_chars`.
143+
pub fn move_to_previous_nearest(&mut self, word_break_chars: &HashSet<char>) {
144+
let pos = self.find_previous_nearest_index(word_break_chars);
143145
self.0.move_to(pos);
144146
}
145147

146-
/// Finds the nearest next index of any character in `items` from the cursor position.
147-
fn find_next_nearest_index(&self, items: &[char]) -> usize {
148+
/// Finds the nearest next index of any character in `word_break_chars` from the cursor position.
149+
fn find_next_nearest_index(&self, word_break_chars: &HashSet<char>) -> usize {
148150
let current_position = self.position();
149151
self.text()
150152
.chars()
151153
.iter()
152154
.enumerate()
153155
.filter(|&(i, _)| i > current_position)
154-
.find(|&(_, c)| items.contains(c))
156+
.find(|&(_, c)| word_break_chars.contains(c))
155157
.map(|(i, _)| {
156158
if i < self.0.contents().len() - 1 {
157159
i + 1
@@ -162,15 +164,15 @@ impl TextEditor {
162164
.unwrap_or(self.0.contents().len() - 1)
163165
}
164166

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

171-
/// Moves the cursor to the nearest next character in `items`.
172-
pub fn move_to_next_nearest(&mut self, items: &[char]) {
173-
let pos = self.find_next_nearest_index(items);
173+
/// Moves the cursor to the nearest next character in `word_break_chars`.
174+
pub fn move_to_next_nearest(&mut self, word_break_chars: &HashSet<char>) {
175+
let pos = self.find_next_nearest_index(word_break_chars);
174176
self.0.move_to(pos);
175177
}
176178

@@ -269,38 +271,42 @@ mod test {
269271
}
270272

271273
mod find_previous_nearest_index {
274+
use std::collections::HashSet;
275+
272276
use crate::text_editor::test::new_with_position;
273277

274278
#[test]
275279
fn test() {
276280
let mut txt = new_with_position(String::from("koko momo jojo "), 11); // indicate `o`.
277-
assert_eq!(10, txt.find_previous_nearest_index(&[' ']));
281+
assert_eq!(10, txt.find_previous_nearest_index(&HashSet::from([' '])));
278282
txt.0.move_to(10);
279-
assert_eq!(5, txt.find_previous_nearest_index(&[' ']));
283+
assert_eq!(5, txt.find_previous_nearest_index(&HashSet::from([' '])));
280284
}
281285

282286
#[test]
283287
fn test_with_no_target() {
284288
let txt = new_with_position(String::from("koko momo jojo "), 7); // indicate `m`.
285-
assert_eq!(0, txt.find_previous_nearest_index(&['z']));
289+
assert_eq!(0, txt.find_previous_nearest_index(&HashSet::from(['z'])));
286290
}
287291
}
288292

289293
mod find_next_nearest_index {
294+
use std::collections::HashSet;
295+
290296
use crate::text_editor::test::new_with_position;
291297

292298
#[test]
293299
fn test() {
294300
let mut txt = new_with_position(String::from("koko momo jojo "), 7); // indicate `m`.
295-
assert_eq!(10, txt.find_next_nearest_index(&[' ']));
301+
assert_eq!(10, txt.find_next_nearest_index(&HashSet::from([' '])));
296302
txt.0.move_to(10);
297-
assert_eq!(14, txt.find_next_nearest_index(&[' ']));
303+
assert_eq!(14, txt.find_next_nearest_index(&HashSet::from([' '])));
298304
}
299305

300306
#[test]
301307
fn test_with_no_target() {
302308
let txt = new_with_position(String::from("koko momo jojo "), 7); // indicate `m`.
303-
assert_eq!(14, txt.find_next_nearest_index(&['z']));
309+
assert_eq!(14, txt.find_next_nearest_index(&HashSet::from(['z'])));
304310
}
305311
}
306312

src/core/text_editor/render.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::collections::HashSet;
2+
13
use crate::{
24
crossterm::style::ContentStyle,
35
grapheme::{matrixify, StyledGraphemes},
@@ -36,8 +38,8 @@ pub struct Renderer {
3638

3739
/// Current edit mode, determining whether input inserts or overwrites existing text.
3840
pub edit_mode: Mode,
39-
/// Characters to be considered for nearest navigation.
40-
pub nearest_characters: Vec<char>,
41+
/// Characters to be for word break.
42+
pub word_break_chars: HashSet<char>,
4143
/// Number of lines available for rendering.
4244
pub lines: Option<usize>,
4345
}

src/preset/query_selector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl QuerySelector {
7171
active_char_style: StyleBuilder::new().bgc(Color::DarkCyan).build(),
7272
inactive_char_style: StyleBuilder::new().build(),
7373
edit_mode: Default::default(),
74-
nearest_characters: Default::default(),
74+
word_break_chars: Default::default(),
7575
lines: Default::default(),
7676
},
7777
listbox_renderer: listbox::Renderer {

src/preset/readline.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::collections::HashSet;
2+
13
use crate::{
24
crossterm::{
35
event::Event,
@@ -58,7 +60,7 @@ impl Default for Readline {
5860
active_char_style: StyleBuilder::new().bgc(Color::DarkCyan).build(),
5961
inactive_char_style: StyleBuilder::new().build(),
6062
edit_mode: Default::default(),
61-
nearest_characters: vec![' '],
63+
word_break_chars: HashSet::from([' ']),
6264
lines: Default::default(),
6365
},
6466
suggest: Default::default(),
@@ -145,9 +147,9 @@ impl Readline {
145147
self
146148
}
147149

148-
/// Sets the characters to be considered for nearest navigation.
149-
pub fn nearest_characters(mut self, characters: Vec<char>) -> Self {
150-
self.text_editor_renderer.nearest_characters = characters;
150+
/// Sets the characters to be for word break.
151+
pub fn word_break_chars(mut self, characters: HashSet<char>) -> Self {
152+
self.text_editor_renderer.word_break_chars = characters;
151153
self
152154
}
153155

src/preset/readline/keymap.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ pub fn default(
131131
state: KeyEventState::NONE,
132132
}) => text_editor_after_mut
133133
.texteditor
134-
.move_to_previous_nearest(&text_editor_after_mut.nearest_characters),
134+
.move_to_previous_nearest(&text_editor_after_mut.word_break_chars),
135135

136136
Event::Key(KeyEvent {
137137
code: KeyCode::Char('f'),
@@ -140,7 +140,7 @@ pub fn default(
140140
state: KeyEventState::NONE,
141141
}) => text_editor_after_mut
142142
.texteditor
143-
.move_to_next_nearest(&text_editor_after_mut.nearest_characters),
143+
.move_to_next_nearest(&text_editor_after_mut.word_break_chars),
144144

145145
// Erase char(s).
146146
Event::Key(KeyEvent {
@@ -164,7 +164,7 @@ pub fn default(
164164
state: KeyEventState::NONE,
165165
}) => text_editor_after_mut
166166
.texteditor
167-
.erase_to_previous_nearest(&text_editor_after_mut.nearest_characters),
167+
.erase_to_previous_nearest(&text_editor_after_mut.word_break_chars),
168168

169169
Event::Key(KeyEvent {
170170
code: KeyCode::Char('d'),
@@ -173,7 +173,7 @@ pub fn default(
173173
state: KeyEventState::NONE,
174174
}) => text_editor_after_mut
175175
.texteditor
176-
.erase_to_next_nearest(&text_editor_after_mut.nearest_characters),
176+
.erase_to_next_nearest(&text_editor_after_mut.word_break_chars),
177177

178178
// Choose history
179179
Event::Key(KeyEvent {

0 commit comments

Comments
 (0)