MCPcopy Create free account
hub / github.com/PHPantom-dev/phpantom_lsp / offset_to_absolute

Function offset_to_absolute

src/semantic_tokens.rs:808–830  ·  view source on GitHub ↗

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,
)

Source from the content-addressed store, hash-verified

806///
807/// Returns `None` if the offset is beyond the content length.
808fn 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.

Callers 1

collect_tokensMethod · 0.85

Calls 3

offset_to_positionFunction · 0.85
getMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected