Add bounds check to prevent panic in token_at_offset

This commit is contained in:
Henry Chu 2025-03-21 12:04:09 +08:00
parent 1f08c3a2c8
commit 065132f258
No known key found for this signature in database

View file

@ -138,6 +138,10 @@ fn find_command_name_ast<L: rowan::Language>(
kind: L::Kind,
offset: TextSize,
) -> Option<Span> {
// `token_at_offset` will panic if offset is not within the range of `root`.
if !root.text_range().contains(offset) {
return None;
}
let token = root
.token_at_offset(offset)
.filter(|token| token.text_range().start() != offset)