/// TextIndex provides conversions between byte offsets (used in the core) /// and LSP positions (line, column in UTF-16 units). /// /// Notes: /// - `line_starts` stores byte offsets for the start of each line. /// - We keep a copy of the text to allow conversions without external dependencies. /// - The LSP column is counted in UTF-16 units, excluding the end-of-line `\n`. #[derive(Clone, Debug)] pub struct TextIndex { text: String, line_starts: Vec, } impl TextIndex { /// Builds the index from the file's current textual content. pub fn new(text: &str) -> Self { let mut line_starts = Vec::with_capacity(128); line_starts.push(0); for (byte, ch) in text.char_indices() { if ch == '\n' { // the start of the next line is the byte after the '\n' line_starts.push((byte + 1) as u32); } } Self { text: text.to_string(), line_starts, } } /// Number of lines (0-based; empty lines count). #[inline] pub fn line_count(&self) -> u32 { self.line_starts.len() as u32 } /// Converts a byte offset (within the file) to (line, UTF-16 column) in LSP format. /// /// For offsets exactly at end-of-line, the column will be the line's UTF-16 length. pub fn byte_to_lsp(&self, byte: u32) -> (u32, u32) { let byte = byte.min(self.text.len() as u32); let line = match self.line_starts.binary_search(&byte) { Ok(i) => i as u32, Err(i) => (i.saturating_sub(1)) as u32, }; let (line_start, line_end) = self.line_bounds(line); let rel = byte.saturating_sub(line_start as u32) as usize; let slice = &self.text[line_start..line_end]; let mut utf16_col: u32 = 0; for (i, ch) in slice.char_indices() { if i >= rel { break; } utf16_col += ch.len_utf16() as u32; } (line, utf16_col) } /// Converts (line, UTF-16 column) to a byte offset. /// /// - Lines outside the range are clamped to [0, last]. /// - Columns larger than the line's UTF-16 length return end-of-line. pub fn lsp_to_byte(&self, line: u32, utf16_col: u32) -> u32 { let line = line.min(self.line_count().saturating_sub(1)); let (line_start, line_end) = self.line_bounds(line); let slice = &self.text[line_start..line_end]; let mut acc: u32 = 0; for (i, ch) in slice.char_indices() { if acc >= utf16_col { return (line_start + i) as u32; } acc += ch.len_utf16() as u32; } // If the target column is after the last character, return end-of-line. line_end as u32 } #[inline] fn line_bounds(&self, line: u32) -> (usize, usize) { let start = *self .line_starts .get(line as usize) .unwrap_or(self.line_starts.last().unwrap()); let next = self.line_starts.get(line as usize + 1).copied(); // If there is a next line, `next` points to the byte after the current line's '\n', // so the content ends at `next - 1`. Otherwise (last line), the content ends at `text.len()`. let end = match next { Some(next_start) => next_start.saturating_sub(1), None => self.text.len() as u32, }; (start as usize, end as usize) } } #[cfg(test)] mod tests_internal { use super::*; #[test] fn line_bounds_basic() { let s = "ab\ncd\n"; let idx = TextIndex::new(s); assert_eq!(idx.line_count(), 3); // line 0: "ab" assert_eq!(idx.line_bounds(0), (0, 2)); // line 1: "cd" assert_eq!(idx.line_bounds(1), (3, 5)); // line 2: final empty line assert_eq!(idx.line_bounds(2), (6, 6)); } }