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
This commit is contained in:
Hillgrove 2026-04-19 18:03:22 +02:00
parent 6841734233
commit e56b6c1cba
1 changed files with 1 additions and 0 deletions

View File

@ -160,6 +160,7 @@ impl LanguageServer for Backend {
} }
fn char_to_position(char_idx: usize, rope: &Rope) -> Position { 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_idx = rope.char_to_line(char_idx);
let line_char_idx = rope.line_to_char(line_idx); let line_char_idx = rope.line_to_char(line_idx);
let column_idx = char_idx - line_char_idx; let column_idx = char_idx - line_char_idx;