add Range::{from_node,into_byte_range}
This commit is contained in:
parent
07cb24abdd
commit
cf9b88f9bd
1 changed files with 16 additions and 1 deletions
|
@ -14,6 +14,7 @@ use crate::{
|
||||||
use helix_stdx::rope::{self, RopeSliceExt};
|
use helix_stdx::rope::{self, RopeSliceExt};
|
||||||
use smallvec::{smallvec, SmallVec};
|
use smallvec::{smallvec, SmallVec};
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
use tree_sitter::Node;
|
||||||
|
|
||||||
/// A single selection range.
|
/// A single selection range.
|
||||||
///
|
///
|
||||||
|
@ -73,6 +74,12 @@ impl Range {
|
||||||
Self::new(head, head)
|
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.
|
/// Start of the range.
|
||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
|
@ -376,6 +383,12 @@ impl Range {
|
||||||
let second = graphemes.next();
|
let second = graphemes.next();
|
||||||
first.is_some() && second.is_none()
|
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 {
|
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;
|
let mut start = sel_start;
|
||||||
|
|
||||||
for line in sel.slice(text).lines() {
|
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();
|
let line_end = start + line.len_chars();
|
||||||
// TODO: retain range direction
|
// TODO: retain range direction
|
||||||
result.push(Range::new(start, line_end - line_ending.len_chars()));
|
result.push(Range::new(start, line_end - line_ending.len_chars()));
|
||||||
|
|
Loading…
Add table
Reference in a new issue