From 997cdc45f635f03a863642f0b490028dd9be36e4 Mon Sep 17 00:00:00 2001 From: Thomas KEMKEMIAN Date: Fri, 27 Feb 2026 01:12:29 +0100 Subject: [PATCH] fix(lsp): prevent char index out of bounds panic in diagnostic positions Convert byte indices to char indices before calculating positions in the rope. The previous implementation passed byte offsets directly to char indexing functions, causing a panic when the byte index exceeded the character count. --- tools/kdl-lsp/src/main.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tools/kdl-lsp/src/main.rs b/tools/kdl-lsp/src/main.rs index f8f003d..a1f833e 100644 --- a/tools/kdl-lsp/src/main.rs +++ b/tools/kdl-lsp/src/main.rs @@ -111,8 +111,8 @@ impl LanguageServer for Backend { .map(|diag| { Diagnostic::new( Range::new( - char_to_position(diag.span.offset(), &doc), - char_to_position(diag.span.offset() + diag.span.len(), &doc), + byte_to_position(diag.span.offset(), &doc), + byte_to_position(diag.span.offset() + diag.span.len(), &doc), ), diag.severity().map(to_lsp_sev), diag.code().map(|c| NumberOrString::String(c.to_string())), @@ -159,7 +159,8 @@ impl LanguageServer for Backend { // } } -fn char_to_position(char_idx: usize, rope: &Rope) -> Position { +fn byte_to_position(byte_idx: usize, rope: &Rope) -> Position { + let char_idx = rope.byte_to_char(byte_idx); let line_idx = rope.char_to_line(char_idx); let line_char_idx = rope.line_to_char(line_idx); let column_idx = char_idx - line_char_idx;