Convert a byte offset and byte length to an absolute line/character position and build an [`AbsoluteToken`]. `length` is a **byte** count (as stored in [`SymbolSpan`]). This function converts it to a UTF-16 code-unit count as required by the LSP semantic token protocol. Returns `None` if the offset is beyond the content length.
(
content: &str,
start_offset: u32,
byte_length: u32,
token_type: u32,
modifiers: u32,
)
| 806 | /// |
| 807 | /// Returns `None` if the offset is beyond the content length. |
| 808 | fn offset_to_absolute( |
| 809 | content: &str, |
| 810 | start_offset: u32, |
| 811 | byte_length: u32, |
| 812 | token_type: u32, |
| 813 | modifiers: u32, |
| 814 | ) -> Option<AbsoluteToken> { |
| 815 | let start = start_offset as usize; |
| 816 | let end = start + byte_length as usize; |
| 817 | let text = content.get(start..end)?; |
| 818 | let utf16_len: u32 = text.chars().map(|c| c.len_utf16() as u32).sum(); |
| 819 | if utf16_len == 0 { |
| 820 | return None; |
| 821 | } |
| 822 | let pos = crate::util::offset_to_position(content, start); |
| 823 | Some(AbsoluteToken { |
| 824 | line: pos.line, |
| 825 | start_char: pos.character, |
| 826 | length: utf16_len, |
| 827 | token_type, |
| 828 | modifiers, |
| 829 | }) |
| 830 | } |
| 831 | |
| 832 | /// Convert a list of absolute-positioned tokens into LSP delta-encoded |
| 833 | /// [`SemanticToken`] values. |
no test coverage detected