Skip to main content

gram_data/
utf16.rs

1//! Map UTF-8 byte offsets to LSP positions (UTF-16 code units).
2
3use tree_sitter::Point;
4
5/// UTF-16 column of `line_prefix` (full line content before cursor on that line).
6fn utf16_col_in_line(line_prefix: &str) -> u32 {
7    let mut utf16 = 0u32;
8    for ch in line_prefix.chars() {
9        utf16 += ch.len_utf16() as u32;
10    }
11    utf16
12}
13
14/// Convert a UTF-8 byte index in `source` to LSP `(line, character)` with UTF-16 `character`.
15pub fn byte_offset_to_position(source: &str, byte_idx: usize) -> (u32, u32) {
16    let byte_idx = byte_idx.min(source.len());
17    let mut line = 0u32;
18    let mut line_start = 0usize;
19    for (i, ch) in source.char_indices() {
20        if i == byte_idx {
21            let col = utf16_col_in_line(&source[line_start..i]);
22            return (line, col);
23        }
24        if ch == '\n' {
25            line += 1;
26            line_start = i + ch.len_utf8();
27        }
28    }
29    (line, utf16_col_in_line(&source[line_start..]))
30}
31
32/// Byte range (start inclusive, end exclusive) to LSP `(line, char)` pairs.
33pub fn byte_range_to_lsp_range(
34    source: &str,
35    start_byte: usize,
36    end_byte: usize,
37) -> ((u32, u32), (u32, u32)) {
38    let start = byte_offset_to_position(source, start_byte);
39    let end = byte_offset_to_position(source, end_byte.max(start_byte));
40    (start, end)
41}
42
43/// Map tree-sitter `Point` to byte offset then to LSP range.
44pub fn point_range_to_lsp_range(
45    source: &str,
46    start: Point,
47    end: Point,
48) -> ((u32, u32), (u32, u32)) {
49    let start_byte = point_to_byte_offset(source, start);
50    let end_byte = point_to_byte_offset(source, end).max(start_byte);
51    byte_range_to_lsp_range(source, start_byte, end_byte)
52}
53
54fn point_to_byte_offset(source: &str, point: Point) -> usize {
55    let mut row = 0usize;
56    let mut line_start = 0usize;
57    for (i, ch) in source.char_indices() {
58        if row == point.row {
59            let rest = &source[line_start..];
60            let line_len = rest.find('\n').unwrap_or(rest.len());
61            let col_b = point.column.min(line_len);
62            return line_start + col_b;
63        }
64        if ch == '\n' {
65            row += 1;
66            line_start = i + ch.len_utf8();
67        }
68    }
69    source.len()
70}
71
72#[cfg(test)]
73mod tests {
74    use super::*;
75
76    #[test]
77    fn ascii_lines() {
78        let s = "ab\ncd";
79        assert_eq!(byte_offset_to_position(s, 0), (0, 0));
80        assert_eq!(byte_offset_to_position(s, 1), (0, 1));
81        assert_eq!(byte_offset_to_position(s, 2), (0, 2)); // start of `\n`
82        assert_eq!(byte_offset_to_position(s, 3), (1, 0)); // `c`
83    }
84
85    #[test]
86    fn point_column_clamped_to_current_line_bytes() {
87        let s = "hi\nyo";
88        // Row 0, column past end of "hi" (2 bytes) must not extend into "yo" or `\n`.
89        let p = Point { row: 0, column: 99 };
90        assert_eq!(point_to_byte_offset(s, p), 2);
91    }
92}