Fix crash when querying for enclosing brackets at EOF

This commit is contained in:
Max Brunsfeld 2022-09-02 10:23:46 -07:00
parent 0588360bf0
commit 0777af1dd3
2 changed files with 11 additions and 1 deletions

View file

@ -2059,7 +2059,8 @@ impl BufferSnapshot {
range: Range<T>,
) -> Option<(Range<usize>, Range<usize>)> {
// Find bracket pairs that *inclusively* contain the given range.
let range = range.start.to_offset(self).saturating_sub(1)..range.end.to_offset(self) + 1;
let range = range.start.to_offset(self).saturating_sub(1)
..self.len().min(range.end.to_offset(self) + 1);
let mut matches = self.syntax.matches(range, &self.text, |grammar| {
grammar.brackets_config.as_ref().map(|c| &c.query)
});

View file

@ -571,6 +571,15 @@ fn test_enclosing_bracket_ranges(cx: &mut MutableAppContext) {
Point::new(3, 4)..Point::new(3, 5)
))
);
// Regression test: avoid crash when querying at the end of the buffer.
assert_eq!(
buffer.enclosing_bracket_point_ranges(buffer.len() - 1..buffer.len()),
Some((
Point::new(0, 6)..Point::new(0, 7),
Point::new(4, 0)..Point::new(4, 1)
))
);
}
#[gpui::test]