Calculate anchors for new hints

Kirill Bulatov created

Change summary

crates/editor/src/display_map.rs           | 50 +++++++++++------------
crates/editor/src/display_map/inlay_map.rs |  2 
crates/editor/src/editor.rs                |  3 
crates/project/src/project.rs              |  1 
4 files changed, 26 insertions(+), 30 deletions(-)

Detailed changes

crates/editor/src/display_map.rs 🔗

@@ -6,7 +6,8 @@ mod tab_map;
 mod wrap_map;
 
 use crate::{
-    Anchor, AnchorRangeExt, InlayHintLocation, MultiBuffer, MultiBufferSnapshot, ToOffset, ToPoint,
+    display_map::inlay_map::InlayProperties, Anchor, AnchorRangeExt, InlayHintLocation,
+    MultiBuffer, MultiBufferSnapshot, ToOffset, ToPoint,
 };
 pub use block_map::{BlockMap, BlockPoint};
 use collections::{HashMap, HashSet};
@@ -284,37 +285,34 @@ impl DisplayMap {
             .update(cx, |map, cx| map.set_wrap_width(width, cx))
     }
 
-    pub fn set_inlay_hints(
+    pub fn splice_inlay_hints(
         &mut self,
         new_hints: &HashMap<InlayHintLocation, Vec<project::InlayHint>>,
         cx: &mut ModelContext<Self>,
     ) {
-        // TODO kb map this to Anchor and set to the map
         let multi_buffer = self.buffer.read(cx);
+        let multi_snapshot = multi_buffer.snapshot(cx);
+
+        let mut hints_to_add = Vec::new();
+        for (&location, hints) in new_hints {
+            for hint in hints {
+                let hint_anchor =
+                    multi_snapshot.anchor_in_excerpt(location.excerpt_id, hint.position);
+                hints_to_add.push((
+                    location,
+                    InlayProperties {
+                        position: hint_anchor,
+                        text: hint.text().trim_end().into(),
+                    },
+                ))
+            }
+        }
 
-        // multi_buffer.anchor_in_excerpt(excerpt_id, hint.position);
-        // TODO kb !!! rework things from buffer_id to excerpt_id
-        // let hint_anchor = multi_buffer
-        //     .snapshot(cx)
-        //     .anchor_in_excerpt(excerpt_id, hint.position);
-
-        // self.inlay_map.splice(
-        //     vec![],
-        //     new_hints
-        //         .into_iter()
-        //         .filter_map(|hint| {
-        //             let snapshot = buffers_to_local_id
-        //                 .get(&hint.buffer_id)?
-        //                 .read(cx)
-        //                 .snapshot();
-        //             Some(Inlay {
-        //                 position: hint.position,
-        //                 text: hint.text().trim_end().into(),
-        //             })
-        //         })
-        //         .collect(),
-        // )
-        todo!("TODO kb")
+        self.inlay_map.splice(
+            // TODO kb this is wrong, calc diffs in the editor instead.
+            self.inlay_map.inlays.keys().copied().collect(),
+            hints_to_add,
+        );
     }
 
     fn tab_size(buffer: &ModelHandle<MultiBuffer>, cx: &mut ModelContext<Self>) -> NonZeroU32 {

crates/editor/src/display_map/inlay_map.rs 🔗

@@ -31,7 +31,7 @@ pub struct InlayId(usize);
 pub struct InlayMap {
     snapshot: Mutex<InlaySnapshot>,
     next_inlay_id: usize,
-    inlays: HashMap<InlayId, (InlayHintLocation, Inlay)>,
+    pub(super) inlays: HashMap<InlayId, (InlayHintLocation, Inlay)>,
 }
 
 #[derive(Clone)]

crates/editor/src/editor.rs 🔗

@@ -2698,12 +2698,11 @@ impl Editor {
                 }
             }
 
-            // TODO kb wrong, need a splice here instead
             if !new_hints.is_empty() {
                 editor
                     .update(&mut cx, |editor, cx| {
                         editor.display_map.update(cx, |display_map, cx| {
-                            display_map.set_inlay_hints(&new_hints, cx);
+                            display_map.splice_inlay_hints(&new_hints, cx);
                         });
                     })
                     .log_err()

crates/project/src/project.rs 🔗

@@ -2825,7 +2825,6 @@ impl Project {
 
             language_server
                 .on_request::<lsp::request::InlayHintRefreshRequest, _, _>({
-                    let this = this.downgrade();
                     move |(), mut cx| async move {
                         let this = this
                             .upgrade(&cx)