Skip to content

Commit

Permalink
Fix panic in vim text-objects (#22753)
Browse files Browse the repository at this point in the history
Caused by messing up offsets between multi-buffers and excerpts :(

Fixes #22739

Release Notes:

- Fixed a panic in vim text objects in multibuffers
  • Loading branch information
ConradIrwin authored and notpeter committed Jan 8, 2025
1 parent 5d35ba3 commit 14b2cd0
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions crates/vim/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,13 +514,15 @@ fn text_object(

let excerpt = snapshot.excerpt_containing(offset..offset)?;
let buffer = excerpt.buffer();
let offset = excerpt.map_offset_to_buffer(offset);

let mut matches: Vec<Range<usize>> = buffer
.text_object_ranges(offset..offset, TreeSitterOptions::default())
.filter_map(|(r, m)| if m == target { Some(r) } else { None })
.collect();
matches.sort_by_key(|r| (r.end - r.start));
if let Some(range) = matches.first() {
if let Some(buffer_range) = matches.first() {
let range = excerpt.map_range_from_buffer(buffer_range.clone());
return Some(range.start.to_display_point(map)..range.end.to_display_point(map));
}

Expand All @@ -537,12 +539,14 @@ fn text_object(
.filter_map(|(r, m)| if m == target { Some(r) } else { None })
.collect();
matches.sort_by_key(|r| r.start);
if let Some(range) = matches.first() {
if !range.is_empty() {
if let Some(buffer_range) = matches.first() {
if !buffer_range.is_empty() {
let range = excerpt.map_range_from_buffer(buffer_range.clone());
return Some(range.start.to_display_point(map)..range.end.to_display_point(map));
}
}
return Some(around_range.start.to_display_point(map)..around_range.end.to_display_point(map));
let buffer_range = excerpt.map_range_from_buffer(around_range.clone());
return Some(buffer_range.start.to_display_point(map)..buffer_range.end.to_display_point(map));
}

fn argument(
Expand Down

0 comments on commit 14b2cd0

Please sign in to comment.