Properly refresh hints on editor open

Kirill Bulatov created

Change summary

crates/editor/src/editor.rs           |  1 -
crates/editor/src/element.rs          |  2 +-
crates/editor/src/inlay_hint_cache.rs | 14 +++++++++++---
crates/editor/src/scroll.rs           |  8 ++++++--
4 files changed, 18 insertions(+), 7 deletions(-)

Detailed changes

crates/editor/src/editor.rs 🔗

@@ -1385,7 +1385,6 @@ impl Editor {
         }
 
         this.report_editor_event("open", None, cx);
-        this.refresh_inlays(InlayRefreshReason::VisibleExcerptsChange, cx);
         this
     }
 

crates/editor/src/element.rs 🔗

@@ -1921,7 +1921,7 @@ impl Element<Editor> for EditorElement {
         let em_advance = style.text.em_advance(cx.font_cache());
         let overscroll = vec2f(em_width, 0.);
         let snapshot = {
-            editor.set_visible_line_count(size.y() / line_height);
+            editor.set_visible_line_count(size.y() / line_height, cx);
 
             let editor_width = text_width - gutter_margin - overscroll.x() - em_width;
             let wrap_width = match editor.soft_wrap_mode(cx) {

crates/editor/src/inlay_hint_cache.rs 🔗

@@ -331,18 +331,19 @@ impl InlayHintCache {
                         .or_insert_with(|| {
                             BufferHints::new(new_hints_per_buffer.buffer_version.clone())
                         });
-                    let mut shown_buffer_hints = currently_shown_hints
-                        .remove(&new_buffer_id)
-                        .unwrap_or_default();
+
                     if cached_buffer_hints
                         .buffer_version
                         .changed_since(&new_hints_per_buffer.buffer_version)
                     {
+                        currently_shown_hints.remove(&new_buffer_id);
                         continue;
                     } else {
                         cached_buffer_hints.buffer_version = new_hints_per_buffer.buffer_version;
                     }
 
+                    let shown_buffer_hints =
+                        currently_shown_hints.entry(new_buffer_id).or_default();
                     for (new_excerpt_id, new_hints_per_excerpt) in
                         new_hints_per_buffer.hints_per_excerpt
                     {
@@ -489,6 +490,13 @@ impl InlayHintCache {
 
                     if shown_buffer_hints.is_empty() {
                         currently_shown_hints.remove(&new_buffer_id);
+                    } else {
+                        to_remove.extend(
+                            shown_buffer_hints
+                                .iter()
+                                .flat_map(|(_, hints_by_excerpt)| hints_by_excerpt.iter())
+                                .map(|(_, hint_id)| *hint_id),
+                        );
                     }
                 }
 

crates/editor/src/scroll.rs 🔗

@@ -295,8 +295,12 @@ impl Editor {
         self.scroll_manager.visible_line_count
     }
 
-    pub(crate) fn set_visible_line_count(&mut self, lines: f32) {
-        self.scroll_manager.visible_line_count = Some(lines)
+    pub(crate) fn set_visible_line_count(&mut self, lines: f32, cx: &mut ViewContext<Self>) {
+        let had_no_visibles = self.scroll_manager.visible_line_count.is_none();
+        self.scroll_manager.visible_line_count = Some(lines);
+        if had_no_visibles {
+            self.refresh_inlays(InlayRefreshReason::VisibleExcerptsChange, cx);
+        }
     }
 
     pub fn set_scroll_position(&mut self, scroll_position: Vector2F, cx: &mut ViewContext<Self>) {