From e56b6c1cbac3b1f6e5c24d715d1fa5e823dc37f3 Mon Sep 17 00:00:00 2001 From: Hillgrove Date: Sun, 19 Apr 2026 18:03:22 +0200 Subject: [PATCH] fix(lsp): clamp char index to rope length in char_to_position Prevents a panic when a diagnostic span's end offset lands past the end of the rope. Fixes kdl-org/vscode-kdl#29 --- tools/kdl-lsp/src/main.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/kdl-lsp/src/main.rs b/tools/kdl-lsp/src/main.rs index f8f003d..8a8fb70 100644 --- a/tools/kdl-lsp/src/main.rs +++ b/tools/kdl-lsp/src/main.rs @@ -160,6 +160,7 @@ impl LanguageServer for Backend { } fn char_to_position(char_idx: usize, rope: &Rope) -> Position { + let char_idx = char_idx.min(rope.len_chars()); 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;