inlay_cache.rs

  1use std::{
  2    cmp,
  3    ops::Range,
  4    path::{Path, PathBuf},
  5};
  6
  7use crate::{editor_settings, Anchor, Editor, ExcerptId, MultiBuffer};
  8use anyhow::Context;
  9use clock::{Global, Local};
 10use gpui::{ModelHandle, Task, ViewContext};
 11use log::error;
 12use project::{InlayHint, InlayHintKind};
 13use util::post_inc;
 14
 15use collections::{hash_map, BTreeMap, HashMap, HashSet};
 16
 17#[derive(Debug, Clone)]
 18pub struct Inlay {
 19    pub id: InlayId,
 20    pub position: Anchor,
 21    pub text: text::Rope,
 22}
 23
 24#[derive(Debug, Clone)]
 25pub struct InlayProperties<T> {
 26    pub position: Anchor,
 27    pub text: T,
 28}
 29
 30#[derive(Debug, Copy, Clone)]
 31pub enum InlayRefreshReason {
 32    Settings(editor_settings::InlayHints),
 33    Regular,
 34}
 35
 36#[derive(Debug, Clone, Default)]
 37pub struct InlayCache {
 38    inlays_per_buffer: HashMap<PathBuf, BufferInlays>,
 39    allowed_hint_kinds: HashSet<Option<InlayHintKind>>,
 40    next_inlay_id: usize,
 41}
 42
 43#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
 44pub struct AnchorKey {
 45    offset: usize,
 46    version: Local,
 47}
 48
 49#[derive(Clone, Debug)]
 50pub struct OrderedByAnchorOffset<T>(pub BTreeMap<AnchorKey, (Anchor, T)>);
 51
 52impl<T> OrderedByAnchorOffset<T> {
 53    pub fn add(&mut self, anchor: Anchor, t: T) {
 54        let key = AnchorKey {
 55            offset: anchor.text_anchor.offset,
 56            version: anchor.text_anchor.timestamp,
 57        };
 58        self.0.insert(key, (anchor, t));
 59    }
 60
 61    fn into_ordered_elements(self) -> impl Iterator<Item = (Anchor, T)> {
 62        self.0.into_values()
 63    }
 64
 65    fn ordered_elements(&self) -> impl Iterator<Item = &(Anchor, T)> {
 66        self.0.values()
 67    }
 68}
 69
 70impl<T> Default for OrderedByAnchorOffset<T> {
 71    fn default() -> Self {
 72        Self(BTreeMap::default())
 73    }
 74}
 75
 76#[derive(Clone, Debug, Default)]
 77struct BufferInlays {
 78    buffer_version: Global,
 79    inlays_per_excerpts: HashMap<ExcerptId, OrderedByAnchorOffset<(InlayId, InlayHint)>>,
 80}
 81
 82#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
 83pub struct InlayId(pub usize);
 84
 85#[derive(Debug, Default)]
 86pub struct InlaySplice {
 87    pub to_remove: Vec<InlayId>,
 88    pub to_insert: Vec<(InlayId, Anchor, InlayHint)>,
 89}
 90
 91pub struct QueryInlaysRange {
 92    pub buffer_id: u64,
 93    pub buffer_path: PathBuf,
 94    pub buffer_version: Global,
 95    pub excerpt_id: ExcerptId,
 96    pub excerpt_offset_range: Range<usize>,
 97}
 98
 99impl InlayCache {
100    pub fn new(inlay_hint_settings: editor_settings::InlayHints) -> Self {
101        Self {
102            inlays_per_buffer: HashMap::default(),
103            allowed_hint_kinds: allowed_inlay_hint_types(inlay_hint_settings),
104            next_inlay_id: 0,
105        }
106    }
107
108    pub fn fetch_inlays(
109        &mut self,
110        multi_buffer: ModelHandle<MultiBuffer>,
111        inlay_fetch_ranges: impl Iterator<Item = QueryInlaysRange>,
112        cx: &mut ViewContext<Editor>,
113    ) -> Task<anyhow::Result<InlaySplice>> {
114        let mut inlay_fetch_tasks = Vec::new();
115        for inlay_fetch_range in inlay_fetch_ranges {
116            let inlays_up_to_date = self.inlays_up_to_date(
117                &inlay_fetch_range.buffer_path,
118                &inlay_fetch_range.buffer_version,
119                inlay_fetch_range.excerpt_id,
120            );
121            let task_multi_buffer = multi_buffer.clone();
122            let task = cx.spawn(|editor, mut cx| async move {
123                if inlays_up_to_date {
124                    anyhow::Ok((inlay_fetch_range, None))
125                } else {
126                    let Some(buffer_handle) = cx.read(|cx| task_multi_buffer.read(cx).buffer(inlay_fetch_range.buffer_id))
127                        else { return Ok((inlay_fetch_range, Some(Vec::new()))) };
128                    let task = editor
129                        .update(&mut cx, |editor, cx| {
130                            let max_buffer_offset = buffer_handle.read(cx).len();
131                            let excerpt_offset_range = &inlay_fetch_range.excerpt_offset_range;
132                            editor.project.as_ref().map(|project| {
133                                project.update(cx, |project, cx| {
134                                    project.query_inlay_hints_for_buffer(
135                                        buffer_handle,
136                                        excerpt_offset_range.start..excerpt_offset_range.end.min(max_buffer_offset),
137                                        cx,
138                                    )
139                                })
140                            })
141                        })
142                        .context("inlays fecth task spawn")?;
143
144                    Ok((inlay_fetch_range, match task {
145                        Some(task) => task.await.context("inlays for buffer task")?,
146                        None => Some(Vec::new()),
147                    }))
148                }
149            });
150            inlay_fetch_tasks.push(task);
151        }
152
153        let final_task = cx.spawn(|editor, mut cx| async move {
154            let mut inlay_updates: HashMap<
155                PathBuf,
156                (
157                    Global,
158                    HashMap<ExcerptId, Option<(Range<usize>, OrderedByAnchorOffset<InlayHint>)>>,
159                ),
160            > = HashMap::default();
161            let multi_buffer_snapshot =
162                editor.read_with(&cx, |editor, cx| editor.buffer().read(cx).snapshot(cx))?;
163
164            for task_result in futures::future::join_all(inlay_fetch_tasks).await {
165                match task_result {
166                    Ok((request_key, response_inlays)) => {
167                        let inlays_per_excerpt = HashMap::from_iter([(
168                            request_key.excerpt_id,
169                            response_inlays
170                                .map(|excerpt_inlays| {
171                                    excerpt_inlays.into_iter().fold(
172                                        OrderedByAnchorOffset::default(),
173                                        |mut ordered_inlays, inlay| {
174                                            let anchor = multi_buffer_snapshot.anchor_in_excerpt(
175                                                request_key.excerpt_id,
176                                                inlay.position,
177                                            );
178                                            ordered_inlays.add(anchor, inlay);
179                                            ordered_inlays
180                                        },
181                                    )
182                                })
183                                .map(|inlays| (request_key.excerpt_offset_range, inlays)),
184                        )]);
185                        match inlay_updates.entry(request_key.buffer_path) {
186                            hash_map::Entry::Occupied(mut o) => {
187                                o.get_mut().1.extend(inlays_per_excerpt);
188                            }
189                            hash_map::Entry::Vacant(v) => {
190                                v.insert((request_key.buffer_version, inlays_per_excerpt));
191                            }
192                        }
193                    }
194                    Err(e) => error!("Failed to update inlays for buffer: {e:#}"),
195                }
196            }
197
198            let updates = if !inlay_updates.is_empty() {
199                let inlays_update = editor.update(&mut cx, |editor, _| {
200                    editor.inlay_cache.apply_fetch_inlays(inlay_updates)
201                })?;
202                inlays_update
203            } else {
204                InlaySplice::default()
205            };
206
207            anyhow::Ok(updates)
208        });
209
210        final_task
211    }
212
213    fn inlays_up_to_date(
214        &self,
215        buffer_path: &Path,
216        buffer_version: &Global,
217        excerpt_id: ExcerptId,
218    ) -> bool {
219        let Some(buffer_inlays) = self.inlays_per_buffer.get(buffer_path) else { return false };
220        let buffer_up_to_date = buffer_version == &buffer_inlays.buffer_version
221            || buffer_inlays.buffer_version.changed_since(&buffer_version);
222        buffer_up_to_date && buffer_inlays.inlays_per_excerpts.contains_key(&excerpt_id)
223    }
224
225    fn apply_fetch_inlays(
226        &mut self,
227        fetched_inlays: HashMap<
228            PathBuf,
229            (
230                Global,
231                HashMap<ExcerptId, Option<(Range<usize>, OrderedByAnchorOffset<InlayHint>)>>,
232            ),
233        >,
234    ) -> InlaySplice {
235        let mut old_inlays = self.inlays_per_buffer.clone();
236        let mut to_remove = Vec::new();
237        let mut to_insert = Vec::new();
238
239        for (buffer_path, (buffer_version, new_buffer_inlays)) in fetched_inlays {
240            match old_inlays.remove(&buffer_path) {
241                Some(mut old_buffer_inlays) => {
242                    for (excerpt_id, new_excerpt_inlays) in new_buffer_inlays {
243                        let (_, mut new_excerpt_inlays) = match new_excerpt_inlays {
244                            Some((excerpt_offset_range, new_inlays)) => (
245                                excerpt_offset_range,
246                                new_inlays.into_ordered_elements().fuse().peekable(),
247                            ),
248                            None => continue,
249                        };
250                        if self.inlays_up_to_date(&buffer_path, &buffer_version, excerpt_id) {
251                            continue;
252                        }
253
254                        let self_inlays_per_buffer = self
255                            .inlays_per_buffer
256                            .get_mut(&buffer_path)
257                            .expect("element expected: `old_inlays.remove` returned `Some`");
258
259                        if old_buffer_inlays
260                            .inlays_per_excerpts
261                            .remove(&excerpt_id)
262                            .is_some()
263                        {
264                            let self_excerpt_inlays = self_inlays_per_buffer
265                                .inlays_per_excerpts
266                                .get_mut(&excerpt_id)
267                                .expect("element expected: `old_excerpt_inlays` is `Some`");
268                            let mut hints_to_add = Vec::<(Anchor, (InlayId, InlayHint))>::new();
269                            // TODO kb update inner buffer_id and version with the new data?
270                            self_excerpt_inlays.0.retain(
271                                |_, (old_anchor, (old_inlay_id, old_inlay))| {
272                                    let mut retain = false;
273
274                                    while let Some(new_offset) = new_excerpt_inlays
275                                        .peek()
276                                        .map(|(new_anchor, _)| new_anchor.text_anchor.offset)
277                                    {
278                                        let old_offset = old_anchor.text_anchor.offset;
279                                        match new_offset.cmp(&old_offset) {
280                                            cmp::Ordering::Less => {
281                                                let (new_anchor, new_inlay) =
282                                                    new_excerpt_inlays.next().expect(
283                                                        "element expected: `peek` returned `Some`",
284                                                    );
285                                                hints_to_add.push((
286                                                    new_anchor,
287                                                    (
288                                                        InlayId(post_inc(&mut self.next_inlay_id)),
289                                                        new_inlay,
290                                                    ),
291                                                ));
292                                            }
293                                            cmp::Ordering::Equal => {
294                                                let (new_anchor, new_inlay) =
295                                                    new_excerpt_inlays.next().expect(
296                                                        "element expected: `peek` returned `Some`",
297                                                    );
298                                                if &new_inlay == old_inlay {
299                                                    retain = true;
300                                                } else {
301                                                    hints_to_add.push((
302                                                        new_anchor,
303                                                        (
304                                                            InlayId(post_inc(
305                                                                &mut self.next_inlay_id,
306                                                            )),
307                                                            new_inlay,
308                                                        ),
309                                                    ));
310                                                }
311                                            }
312                                            cmp::Ordering::Greater => break,
313                                        }
314                                    }
315
316                                    if !retain {
317                                        to_remove.push(*old_inlay_id);
318                                    }
319                                    retain
320                                },
321                            );
322
323                            for (new_anchor, (id, new_inlay)) in hints_to_add {
324                                self_excerpt_inlays.add(new_anchor, (id, new_inlay.clone()));
325                                to_insert.push((id, new_anchor, new_inlay));
326                            }
327                        }
328
329                        for (new_anchor, new_inlay) in new_excerpt_inlays {
330                            let id = InlayId(post_inc(&mut self.next_inlay_id));
331                            self_inlays_per_buffer
332                                .inlays_per_excerpts
333                                .entry(excerpt_id)
334                                .or_default()
335                                .add(new_anchor, (id, new_inlay.clone()));
336                            to_insert.push((id, new_anchor, new_inlay));
337                        }
338                    }
339                }
340                None => {
341                    let mut inlays_per_excerpts: HashMap<
342                        ExcerptId,
343                        OrderedByAnchorOffset<(InlayId, InlayHint)>,
344                    > = HashMap::default();
345                    for (new_excerpt_id, new_ordered_inlays) in new_buffer_inlays {
346                        if let Some((_, new_ordered_inlays)) = new_ordered_inlays {
347                            for (new_anchor, new_inlay) in
348                                new_ordered_inlays.into_ordered_elements()
349                            {
350                                let id = InlayId(post_inc(&mut self.next_inlay_id));
351                                inlays_per_excerpts
352                                    .entry(new_excerpt_id)
353                                    .or_default()
354                                    .add(new_anchor, (id, new_inlay.clone()));
355                                to_insert.push((id, new_anchor, new_inlay));
356                            }
357                        }
358                    }
359                    self.inlays_per_buffer.insert(
360                        buffer_path,
361                        BufferInlays {
362                            buffer_version,
363                            inlays_per_excerpts,
364                        },
365                    );
366                }
367            }
368        }
369
370        for (_, old_buffer_inlays) in old_inlays {
371            for (_, old_excerpt_inlays) in old_buffer_inlays.inlays_per_excerpts {
372                for (_, (id_to_remove, _)) in old_excerpt_inlays.into_ordered_elements() {
373                    to_remove.push(id_to_remove);
374                }
375            }
376        }
377
378        to_insert.retain(|(_, _, new_hint)| self.allowed_hint_kinds.contains(&new_hint.kind));
379
380        InlaySplice {
381            to_remove,
382            to_insert,
383        }
384    }
385
386    pub fn apply_settings(
387        &mut self,
388        inlay_hint_settings: editor_settings::InlayHints,
389    ) -> InlaySplice {
390        let new_allowed_inlay_hint_types = allowed_inlay_hint_types(inlay_hint_settings);
391
392        let new_allowed_hint_kinds = new_allowed_inlay_hint_types
393            .difference(&self.allowed_hint_kinds)
394            .copied()
395            .collect::<HashSet<_>>();
396        let removed_hint_kinds = self
397            .allowed_hint_kinds
398            .difference(&new_allowed_inlay_hint_types)
399            .collect::<HashSet<_>>();
400        let mut to_remove = Vec::new();
401        let mut to_insert = Vec::new();
402        for (anchor, (inlay_id, inlay_hint)) in self
403            .inlays_per_buffer
404            .iter()
405            .map(|(_, buffer_inlays)| {
406                buffer_inlays
407                    .inlays_per_excerpts
408                    .iter()
409                    .map(|(_, excerpt_inlays)| excerpt_inlays.ordered_elements())
410                    .flatten()
411            })
412            .flatten()
413        {
414            if removed_hint_kinds.contains(&inlay_hint.kind) {
415                to_remove.push(*inlay_id);
416            } else if new_allowed_hint_kinds.contains(&inlay_hint.kind) {
417                to_insert.push((*inlay_id, *anchor, inlay_hint.to_owned()));
418            }
419        }
420
421        self.allowed_hint_kinds = new_allowed_hint_kinds;
422
423        InlaySplice {
424            to_remove,
425            to_insert,
426        }
427    }
428
429    pub fn clear(&mut self) -> Vec<InlayId> {
430        self.inlays_per_buffer
431            .drain()
432            .map(|(_, buffer_inlays)| {
433                buffer_inlays
434                    .inlays_per_excerpts
435                    .into_iter()
436                    .map(|(_, excerpt_inlays)| {
437                        excerpt_inlays
438                            .into_ordered_elements()
439                            .map(|(_, (id, _))| id)
440                    })
441                    .flatten()
442            })
443            .flatten()
444            .collect()
445    }
446}
447
448fn allowed_inlay_hint_types(
449    inlay_hint_settings: editor_settings::InlayHints,
450) -> HashSet<Option<InlayHintKind>> {
451    let mut new_allowed_inlay_hint_types = HashSet::default();
452    if inlay_hint_settings.show_type_hints {
453        new_allowed_inlay_hint_types.insert(Some(InlayHintKind::Type));
454    }
455    if inlay_hint_settings.show_parameter_hints {
456        new_allowed_inlay_hint_types.insert(Some(InlayHintKind::Parameter));
457    }
458    if inlay_hint_settings.show_other_hints {
459        new_allowed_inlay_hint_types.insert(None);
460    }
461    new_allowed_inlay_hint_types
462}