From 9f3c2154fc8b3bc0c08bde43b96330ce752f50f4 Mon Sep 17 00:00:00 2001 From: Hillgrove Date: Fri, 29 May 2026 06:51:43 +0200 Subject: [PATCH] fix(lsp): clamp char index to rope length in char_to_position (#154) Fixes: kdl-org/vscode-kdl#29 Prevents a panic when a diagnostic span's end offset lands past the end of the rope. --- 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;