Convert a byte offset in `content` to an LSP `Position` (line, character). This is the inverse of [`position_to_byte_offset`]. Characters are counted as UTF-16 code units per the LSP specification. If `offset` is past the end of `content`, the position at the end of the file is returned.
(content: &str, offset: usize)
| 320 | /// If `offset` is past the end of `content`, the position at the end of |
| 321 | /// the file is returned. |
| 322 | pub(crate) fn offset_to_position(content: &str, offset: usize) -> Position { |
| 323 | let mut line = 0u32; |
| 324 | let mut col = 0u32; |
| 325 | for (i, ch) in content.char_indices() { |
| 326 | if i == offset { |
| 327 | return Position { |
| 328 | line, |
| 329 | character: col, |
| 330 | }; |
| 331 | } |
| 332 | if ch == '\n' { |
| 333 | line += 1; |
| 334 | col = 0; |
| 335 | } else { |
| 336 | col += ch.len_utf16() as u32; |
| 337 | } |
| 338 | } |
| 339 | // offset == content.len() (end of file) |
| 340 | Position { |
| 341 | line, |
| 342 | character: col, |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | /// Convert an LSP `Position` (line, character) to a byte offset in |
| 347 | /// `content`. |
no outgoing calls