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

Function position_to_byte_offset

src/util.rs:352–372  ·  view source on GitHub ↗

Convert an LSP `Position` (line, character) to a byte offset in `content`. Characters are counted as UTF-16 code units per the LSP specification. If the position is past the end of the file, the content length is returned.

(content: &str, position: Position)

Source from the content-addressed store, hash-verified

350/// If the position is past the end of the file, the content length is
351/// returned.
352pub(crate) fn position_to_byte_offset(content: &str, position: Position) -> usize {
353 let mut line = 0u32;
354 let mut col = 0u32;
355 for (i, ch) in content.char_indices() {
356 if line == position.line && col == position.character {
357 return i;
358 }
359 if ch == '\n' {
360 if line == position.line {
361 // Position is past the end of this line — clamp to newline.
362 return i;
363 }
364 line += 1;
365 col = 0;
366 } else {
367 col += ch.len_utf16() as u32;
368 }
369 }
370 // Position at end of content.
371 content.len()
372}
373
374/// Convert a UTF-16 column offset to a byte offset within a single line.
375///

Callers 15

apply_text_editsFunction · 0.85
did_changeMethod · 0.85
position_to_offsetFunction · 0.85
paren_follows_cursorFunction · 0.85
detect_catch_contextFunction · 0.85
is_inside_docblockFunction · 0.85
classify_string_contextFunction · 0.85
extract_function_bodyFunction · 0.85
get_text_after_triggerFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected