Skip to content

Commit

Permalink
add Range::{from_node,into_byte_range}
Browse files Browse the repository at this point in the history
  • Loading branch information
dead10ck authored and the-mikedavis committed Apr 9, 2024
1 parent 07cb24a commit cf9b88f
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion helix-core/src/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()));
Expand Down

0 comments on commit cf9b88f

Please sign in to comment.