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::{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 {

View File

@ -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)
}