Skip to main content

rbx_rsml/
range_from_span.rs

1use ropey::Rope;
2use crate::types::{Position, Range};
3
4pub trait RangeFromSpan {
5    fn from_span(rope: &Rope, location: (usize, usize)) -> Range;
6}
7
8impl RangeFromSpan for Range {
9    fn from_span(rope: &Rope, location: (usize, usize)) -> Range {
10        let (start_byte_idx, end_byte_idx) = location;
11        let (start_char_idx, end_char_idx) =
12            (rope.byte_to_char(start_byte_idx), rope.byte_to_char(end_byte_idx));
13
14        let start_line_idx = rope.char_to_line(start_char_idx);
15        let start_line = rope.line_to_char(start_line_idx);
16        let start_col = start_char_idx - start_line;
17
18        let end_line_idx = rope.char_to_line(end_char_idx);
19        let end_line = rope.line_to_char(end_line_idx);
20        let end_col = end_char_idx - end_line;
21
22        Range {
23            start: Position {
24                line: start_line_idx as u32,
25                character: start_col as u32,
26            },
27            end: Position {
28                line: end_line_idx as u32,
29                character: end_col as u32,
30            },
31        }
32    }
33}