From d506522eef277a4fc5ca6006e13d88ac62c6a9d2 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Wed, 7 Jun 2023 23:39:58 +0300 Subject: [PATCH] Correctly pass inlay hints --- crates/editor/src/display_map.rs | 43 ++++++++-------------- crates/editor/src/display_map/inlay_map.rs | 10 ++--- crates/project/src/lsp_command.rs | 4 +- crates/project/src/project.rs | 2 +- 4 files changed, 22 insertions(+), 37 deletions(-) diff --git a/crates/editor/src/display_map.rs b/crates/editor/src/display_map.rs index ade36990de..67b3c19d78 100644 --- a/crates/editor/src/display_map.rs +++ b/crates/editor/src/display_map.rs @@ -100,13 +100,9 @@ impl DisplayMap { let edits = self.buffer_subscription.consume().into_inner(); let (fold_snapshot, edits) = self.fold_map.read(buffer_snapshot, edits); let (suggestion_snapshot, edits) = self.suggestion_map.sync(fold_snapshot.clone(), edits); - let (inlay_snapshot, edits) = self - .inlay_map - .sync(suggestion_snapshot.clone(), edits); + let (inlay_snapshot, edits) = self.inlay_map.sync(suggestion_snapshot.clone(), edits); let tab_size = Self::tab_size(&self.buffer, cx); - let (tab_snapshot, edits) = - self.tab_map - .sync(inlay_snapshot.clone(), edits, tab_size); + let (tab_snapshot, edits) = self.tab_map.sync(inlay_snapshot.clone(), edits, tab_size); let (wrap_snapshot, edits) = self .wrap_map .update(cx, |map, cx| map.sync(tab_snapshot.clone(), edits, cx)); @@ -293,24 +289,24 @@ impl DisplayMap { new_hints: &[project::InlayHint], cx: &mut ModelContext, ) { - dbg!("---", new_hints.len()); let multi_buffer = self.buffer.read(cx); - let zz = dbg!(multi_buffer + // TODO kb carry both remote and local ids of the buffer? + // now, `.buffer` requires remote id, hence this map. + let buffers_to_local_id = multi_buffer .all_buffers() .into_iter() - .map(|buffer_handle| buffer_handle.id()) - .collect::>()); + .map(|buffer_handle| (buffer_handle.id(), buffer_handle)) + .collect::>(); self.inlay_map.set_inlay_hints( new_hints .into_iter() .filter_map(|hint| { - // TODO kb this is all wrong, need to manage both(?) or at least the remote buffer id when needed. - // Here though, `.buffer(` requires remote buffer id, so use the workaround above. - dbg!(zz.contains(&(hint.buffer_id as usize))); - let buffer = dbg!(multi_buffer.buffer(dbg!(hint.buffer_id)))?.read(cx); - let snapshot = buffer.snapshot(); + let snapshot = buffers_to_local_id + .get(&hint.buffer_id)? + .read(cx) + .snapshot(); Some(InlayHintToRender { position: inlay_map::InlayPoint(text::ToPoint::to_point( &hint.position, @@ -419,9 +415,7 @@ impl DisplaySnapshot { fn point_to_display_point(&self, point: Point, bias: Bias) -> DisplayPoint { let fold_point = self.fold_snapshot.to_fold_point(point, bias); let suggestion_point = self.suggestion_snapshot.to_suggestion_point(fold_point); - let inlay_point = self - .inlay_snapshot - .to_inlay_point(suggestion_point); + let inlay_point = self.inlay_snapshot.to_inlay_point(suggestion_point); let tab_point = self.tab_snapshot.to_tab_point(inlay_point); let wrap_point = self.wrap_snapshot.tab_point_to_wrap_point(tab_point); let block_point = self.block_snapshot.to_block_point(wrap_point); @@ -432,13 +426,8 @@ impl DisplaySnapshot { let block_point = point.0; let wrap_point = self.block_snapshot.to_wrap_point(block_point); let tab_point = self.wrap_snapshot.to_tab_point(wrap_point); - let inlay_point = self - .tab_snapshot - .to_inlay_point(tab_point, bias) - .0; - let suggestion_point = self - .inlay_snapshot - .to_suggestion_point(inlay_point, bias); + let inlay_point = self.tab_snapshot.to_inlay_point(tab_point, bias).0; + let suggestion_point = self.inlay_snapshot.to_suggestion_point(inlay_point, bias); let fold_point = self.suggestion_snapshot.to_fold_point(suggestion_point); fold_point.to_buffer_point(&self.fold_snapshot) } @@ -853,9 +842,7 @@ impl DisplayPoint { let wrap_point = map.block_snapshot.to_wrap_point(self.0); let tab_point = map.wrap_snapshot.to_tab_point(wrap_point); let inlay_point = map.tab_snapshot.to_inlay_point(tab_point, bias).0; - let suggestion_point = map - .inlay_snapshot - .to_suggestion_point(inlay_point, bias); + let suggestion_point = map.inlay_snapshot.to_suggestion_point(inlay_point, bias); let fold_point = map.suggestion_snapshot.to_fold_point(suggestion_point); fold_point.to_buffer_offset(&map.fold_snapshot) } diff --git a/crates/editor/src/display_map/inlay_map.rs b/crates/editor/src/display_map/inlay_map.rs index 18ac59fcef..afb4e28de3 100644 --- a/crates/editor/src/display_map/inlay_map.rs +++ b/crates/editor/src/display_map/inlay_map.rs @@ -29,12 +29,12 @@ pub struct InlayHintId(usize); pub struct InlayMap { snapshot: Mutex, next_hint_id: AtomicUsize, - hints: HashMap, + inlay_hints: HashMap, } #[derive(Clone)] pub struct InlaySnapshot { - // TODO kb merge these two together + // TODO kb merge these two together? pub suggestion_snapshot: SuggestionSnapshot, transforms: SumTree, pub version: usize, @@ -141,7 +141,7 @@ impl InlayMap { Self { snapshot: Mutex::new(snapshot.clone()), next_hint_id: AtomicUsize::new(0), - hints: HashMap::default(), + inlay_hints: HashMap::default(), }, snapshot, ) @@ -160,7 +160,6 @@ impl InlayMap { let mut inlay_edits = Vec::new(); - dbg!(&suggestion_edits); for suggestion_edit in suggestion_edits { let old = suggestion_edit.old; let new = suggestion_edit.new; @@ -190,9 +189,8 @@ impl InlayMap { } pub fn set_inlay_hints(&mut self, new_hints: Vec) { - dbg!(new_hints.len()); // TODO kb reuse ids for hints that did not change and similar things - self.hints = new_hints + self.inlay_hints = new_hints .into_iter() .map(|hint| { ( diff --git a/crates/project/src/lsp_command.rs b/crates/project/src/lsp_command.rs index e735773f4b..3089683cd5 100644 --- a/crates/project/src/lsp_command.rs +++ b/crates/project/src/lsp_command.rs @@ -1834,7 +1834,7 @@ impl LspCommand for InlayHints { .unwrap_or_default() .into_iter() .map(|lsp_hint| InlayHint { - buffer_id: buffer.id() as u64, + buffer_id: buffer.id(), position: origin_buffer.anchor_after( origin_buffer .clip_point_utf16(point_from_lsp(lsp_hint.position), Bias::Left), @@ -2007,7 +2007,7 @@ impl LspCommand for InlayHints { let mut hints = Vec::new(); for message_hint in message.hints { let hint = InlayHint { - buffer_id: buffer.id() as u64, + buffer_id: buffer.id(), position: message_hint .position .and_then(language::proto::deserialize_anchor) diff --git a/crates/project/src/project.rs b/crates/project/src/project.rs index c33b563ea5..48d07af78e 100644 --- a/crates/project/src/project.rs +++ b/crates/project/src/project.rs @@ -329,7 +329,7 @@ pub struct Location { #[derive(Debug, Clone, PartialEq, Eq)] pub struct InlayHint { - pub buffer_id: u64, + pub buffer_id: usize, pub position: Anchor, pub label: InlayHintLabel, pub kind: Option,