refactor: extract duplicated logic

pull/12514/head
Nikita Revenco 2025-01-12 20:53:00 +00:00
parent 78bfdb680f
commit 64390df159
2 changed files with 14 additions and 22 deletions

View File

@ -11,12 +11,7 @@ use crate::syntax::LanguageConfiguration;
use crate::Range; use crate::Range;
use crate::{surround, Syntax}; use crate::{surround, Syntax};
pub fn find_word_boundary( fn find_word_boundary(slice: RopeSlice, mut pos: usize, direction: Direction, long: bool) -> usize {
slice: RopeSlice,
mut pos: usize,
direction: Direction,
long: bool,
) -> usize {
use CharCategory::{Eol, Whitespace}; use CharCategory::{Eol, Whitespace};
let iter = match direction { 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 // count doesn't do anything yet
pub fn textobject_word( pub fn textobject_word(
slice: RopeSlice, slice: RopeSlice,
@ -83,11 +88,7 @@ pub fn textobject_word(
) -> Range { ) -> Range {
let pos = range.cursor(slice); let pos = range.cursor(slice);
let word_start = find_word_boundary(slice, pos, Direction::Backward, long); let (word_start, word_end) = find_word_boundaries(slice, pos, 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),
};
// Special case. // Special case.
if word_start == word_end { if word_start == word_end {

View File

@ -14,13 +14,12 @@ use crate::{
}; };
use helix_core::{ use helix_core::{
chars::CharCategory,
diagnostic::NumberOrString, diagnostic::NumberOrString,
graphemes::{next_grapheme_boundary, prev_grapheme_boundary}, graphemes::{next_grapheme_boundary, prev_grapheme_boundary},
movement::Direction, movement::Direction,
syntax::{self, HighlightEvent}, syntax::{self, HighlightEvent},
text_annotations::TextAnnotations, text_annotations::TextAnnotations,
textobject::find_word_boundary, textobject::find_word_boundaries,
unicode::width::UnicodeWidthStr, unicode::width::UnicodeWidthStr,
visual_offset_from_block, Change, Position, Range, Selection, Transaction, visual_offset_from_block, Change, Position, Range, Selection, Transaction,
}; };
@ -1200,15 +1199,7 @@ impl EditorView {
} }
} }
MouseClick::Double => { MouseClick::Double => {
let word_start = let (word_start, word_end) = find_word_boundaries(text, pos, false);
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),
};
Selection::single(word_start, word_end) Selection::single(word_start, word_end)
} }