Clip points coming from language server

Antonio Scandurra and Max Brunsfeld created

This avoids panicking in Zed if the points they give us are invalid.

Co-Authored-By: Max Brunsfeld <max@zed.dev>

Change summary

crates/buffer/src/lib.rs   | 6 +++++-
crates/language/src/lib.rs | 4 +++-
2 files changed, 8 insertions(+), 2 deletions(-)

Detailed changes

crates/buffer/src/lib.rs 🔗

@@ -570,7 +570,7 @@ impl Buffer {
     }
 
     pub fn clip_point(&self, point: Point, bias: Bias) -> Point {
-        self.visible_text.clip_point(point, bias)
+        self.content().clip_point(point, bias)
     }
 
     pub fn clip_offset(&self, offset: usize, bias: Bias) -> usize {
@@ -1899,6 +1899,10 @@ impl<'a> Content<'a> {
         FullOffset(summary.visible + summary.deleted + overshoot)
     }
 
+    pub fn clip_point(&self, point: Point, bias: Bias) -> Point {
+        self.visible_text.clip_point(point, bias)
+    }
+
     fn point_for_offset(&self, offset: usize) -> Result<Point> {
         if offset <= self.len() {
             Ok(self.text_summary_for_range(0..offset).lines)

crates/language/src/lib.rs 🔗

@@ -711,7 +711,9 @@ impl Buffer {
                         end = last_edit_new_end + (end - last_edit_old_end);
                     }
 
-                    Some((start..end, (severity, diagnostic.message)))
+                    let range =
+                        content.clip_point(start, Bias::Left)..content.clip_point(end, Bias::Right);
+                    Some((range, (severity, diagnostic.message)))
                 }),
             )
         };