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

Function offset_to_position

src/util.rs:322–344  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

320/// If `offset` is past the end of `content`, the position at the end of
321/// the file is returned.
322pub(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`.

Callers 15

class_to_symbolFunction · 0.85
method_to_symbolFunction · 0.85
property_to_symbolFunction · 0.85
constant_to_symbolFunction · 0.85
function_to_symbolFunction · 0.85
offset_to_absoluteFunction · 0.85
to_selection_rangeFunction · 0.85
handle_document_linkMethod · 0.85
emit_parameter_hintsMethod · 0.85
emit_closure_hintsMethod · 0.85

Calls

no outgoing calls