diff options
author | Blaž Hrastnik | 2021-01-22 08:13:14 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2021-01-22 08:13:14 +0000 |
commit | 2bea5db7bdb3ad3fa029df830d824cd5c26a153f (patch) | |
tree | e45016157d4c3db7a56c5935f3e33843bc0daf87 /helix-core | |
parent | a702af0aeb4562cbcdf8e2bc4008b8ce95da9a56 (diff) |
commands: Implement select_on_matches.
Diffstat (limited to 'helix-core')
-rw-r--r-- | helix-core/src/selection.rs | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs index 9413fead..55514864 100644 --- a/helix-core/src/selection.rs +++ b/helix-core/src/selection.rs @@ -258,6 +258,35 @@ impl Selection { // TODO: checkSelection -> check if valid for doc length +pub fn select_on_matches( + text: &RopeSlice, + selections: &Selection, + regex: &crate::regex::Regex, +) -> Selection { + let mut result = SmallVec::with_capacity(selections.ranges().len()); + + for sel in selections.ranges() { + // TODO: can't avoid occasional allocations since Regex can't operate on chunks yet + let fragment = sel.fragment(&text); + + let mut sel_start = sel.from(); + let sel_end = sel.to(); + + let mut start_byte = text.char_to_byte(sel_start); + + for mat in regex.find_iter(&fragment) { + // TODO: retain range direction + + let start = text.byte_to_char(start_byte + mat.start()); + let end = text.byte_to_char(start_byte + mat.end()); + result.push(Range::new(start, end - 1)); + } + } + + // TODO: figure out a new primary index + Selection::new(result, 0) +} + // TODO: support to split on capture #N instead of whole match pub fn split_on_matches( text: &RopeSlice, |