Unnamed repository; edit this file 'description' to name the repository.
add Range::{from_node,into_byte_range}
Skyler Hawthorne 2024-04-09
parent 07cb24a · commit cf9b88f
-rw-r--r--helix-core/src/selection.rs17
1 files changed, 16 insertions, 1 deletions
diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs
index 579499de..65261287 100644
--- a/helix-core/src/selection.rs
+++ b/helix-core/src/selection.rs
@@ -14,6 +14,7 @@ use crate::{
use helix_stdx::rope::{self, RopeSliceExt};
use smallvec::{smallvec, SmallVec};
use std::borrow::Cow;
+use tree_sitter::Node;
/// A single selection range.
///
@@ -73,6 +74,12 @@ impl Range {
Self::new(head, head)
}
+ pub fn from_node(node: Node, text: RopeSlice, direction: Direction) -> Self {
+ let from = text.byte_to_char(node.start_byte());
+ let to = text.byte_to_char(node.end_byte());
+ Range::new(from, to).with_direction(direction)
+ }
+
/// Start of the range.
#[inline]
#[must_use]
@@ -376,6 +383,12 @@ impl Range {
let second = graphemes.next();
first.is_some() && second.is_none()
}
+
+ /// Converts this char range into an in order byte range, discarding
+ /// direction.
+ pub fn into_byte_range(&self, text: RopeSlice) -> (usize, usize) {
+ (text.char_to_byte(self.from()), text.char_to_byte(self.to()))
+ }
}
impl From<(usize, usize)> for Range {
@@ -783,7 +796,9 @@ pub fn split_on_newline(text: RopeSlice, selection: &Selection) -> Selection {
let mut start = sel_start;
for line in sel.slice(text).lines() {
- let Some(line_ending) = get_line_ending(&line) else { break };
+ let Some(line_ending) = get_line_ending(&line) else {
+ break;
+ };
let line_end = start + line.len_chars();
// TODO: retain range direction
result.push(Range::new(start, line_end - line_ending.len_chars()));