From 64390df1591f415378b2c5e3dc0a1d4fe5db5a97 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+NikitaRevenco@users.noreply.github.com> Date: Sun, 12 Jan 2025 20:53:00 +0000 Subject: [PATCH] refactor: extract duplicated logic --- helix-core/src/textobject.rs | 23 ++++++++++++----------- helix-term/src/ui/editor.rs | 13 ++----------- 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/helix-core/src/textobject.rs b/helix-core/src/textobject.rs index 1225b70a7..fd0b656d7 100644 --- a/helix-core/src/textobject.rs +++ b/helix-core/src/textobject.rs @@ -11,12 +11,7 @@ use crate::syntax::LanguageConfiguration; use crate::Range; use crate::{surround, Syntax}; -pub fn find_word_boundary( - slice: RopeSlice, - mut pos: usize, - direction: Direction, - long: bool, -) -> usize { +fn find_word_boundary(slice: RopeSlice, mut pos: usize, direction: Direction, long: bool) -> usize { use CharCategory::{Eol, Whitespace}; let iter = match direction { @@ -73,6 +68,16 @@ impl Display for TextObject { } } +pub fn find_word_boundaries(slice: RopeSlice, pos: usize, is_long: bool) -> (usize, usize) { + let word_start = find_word_boundary(slice, pos, Direction::Backward, is_long); + let word_end = match slice.get_char(pos).map(categorize_char) { + None | Some(CharCategory::Whitespace | CharCategory::Eol) => pos, + _ => find_word_boundary(slice, pos + 1, Direction::Forward, is_long), + }; + + (word_start, word_end) +} + // count doesn't do anything yet pub fn textobject_word( slice: RopeSlice, @@ -83,11 +88,7 @@ pub fn textobject_word( ) -> Range { let pos = range.cursor(slice); - let word_start = find_word_boundary(slice, pos, Direction::Backward, long); - let word_end = match slice.get_char(pos).map(categorize_char) { - None | Some(CharCategory::Whitespace | CharCategory::Eol) => pos, - _ => find_word_boundary(slice, pos + 1, Direction::Forward, long), - }; + let (word_start, word_end) = find_word_boundaries(slice, pos, long); // Special case. if word_start == word_end { diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index c05f1acbd..da2273c2c 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -14,13 +14,12 @@ use crate::{ }; use helix_core::{ - chars::CharCategory, diagnostic::NumberOrString, graphemes::{next_grapheme_boundary, prev_grapheme_boundary}, movement::Direction, syntax::{self, HighlightEvent}, text_annotations::TextAnnotations, - textobject::find_word_boundary, + textobject::find_word_boundaries, unicode::width::UnicodeWidthStr, visual_offset_from_block, Change, Position, Range, Selection, Transaction, }; @@ -1200,15 +1199,7 @@ impl EditorView { } } MouseClick::Double => { - let word_start = - find_word_boundary(text, pos, Direction::Backward, false); - let word_end = match text - .get_char(pos) - .map(helix_core::chars::categorize_char) - { - None | Some(CharCategory::Whitespace | CharCategory::Eol) => pos, - _ => find_word_boundary(text, pos + 1, Direction::Forward, false), - }; + let (word_start, word_end) = find_word_boundaries(text, pos, false); Selection::single(word_start, word_end) }