Properly update inlay hints when settings are changed

This commit is contained in:
Kirill Bulatov
2023-06-29 22:25:49 +03:00
parent 1ed52276e0
commit c898298c5c
4 changed files with 315 additions and 189 deletions
+230 -18
View File
@@ -1,18 +1,29 @@
use std::{
cmp,
ops::Range,
path::{Path, PathBuf},
};
use crate::{Anchor, ExcerptId};
use crate::{editor_settings, Anchor, Editor, ExcerptId, MultiBuffer};
use anyhow::Context;
use clock::{Global, Local};
use project::InlayHint;
use gpui::{ModelHandle, Task, ViewContext};
use log::error;
use project::{InlayHint, InlayHintKind};
use util::post_inc;
use collections::{BTreeMap, HashMap};
use collections::{hash_map, BTreeMap, HashMap, HashSet};
#[derive(Clone, Debug, Default)]
#[derive(Debug, Copy, Clone)]
pub enum InlayRefreshReason {
Settings(editor_settings::InlayHints),
Regular,
}
#[derive(Debug, Clone, Default)]
pub struct InlayCache {
inlays_per_buffer: HashMap<PathBuf, BufferInlays>,
allowed_hint_kinds: HashSet<Option<InlayHintKind>>,
next_inlay_id: usize,
}
@@ -37,6 +48,10 @@ impl<T> OrderedByAnchorOffset<T> {
fn into_ordered_elements(self) -> impl Iterator<Item = (Anchor, T)> {
self.0.into_values()
}
fn ordered_elements(&self) -> impl Iterator<Item = &(Anchor, T)> {
self.0.values()
}
}
impl<T> Default for OrderedByAnchorOffset<T> {
@@ -54,14 +69,150 @@ struct BufferInlays {
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct InlayId(pub usize);
#[derive(Debug)]
#[derive(Debug, Default)]
pub struct InlaysUpdate {
pub to_remove: Vec<InlayId>,
pub to_insert: Vec<(InlayId, Anchor, InlayHint)>,
}
impl InlaysUpdate {
fn merge(&mut self, other: Self) {
let mut new_to_remove = other.to_remove.iter().copied().collect::<HashSet<_>>();
self.to_insert
.retain(|(inlay_id, _, _)| !new_to_remove.remove(&inlay_id));
self.to_remove.extend(new_to_remove);
self.to_insert
.extend(other.to_insert.into_iter().filter(|(inlay_id, _, _)| {
!self
.to_remove
.iter()
.any(|removed_inlay_id| removed_inlay_id == inlay_id)
}));
}
}
pub struct QueryInlaysRange {
pub buffer_id: u64,
pub buffer_path: PathBuf,
pub buffer_version: Global,
pub excerpt_id: ExcerptId,
pub excerpt_offset_range: Range<usize>,
}
impl InlayCache {
pub fn inlays_up_to_date(
pub fn new(inlay_hint_settings: editor_settings::InlayHints) -> Self {
Self {
inlays_per_buffer: HashMap::default(),
allowed_hint_kinds: allowed_inlay_hint_types(inlay_hint_settings),
next_inlay_id: 0,
}
}
pub fn fetch_inlays(
&mut self,
multi_buffer: ModelHandle<MultiBuffer>,
inlay_fetch_ranges: impl Iterator<Item = QueryInlaysRange>,
cx: &mut ViewContext<Editor>,
) -> Task<anyhow::Result<InlaysUpdate>> {
let mut inlay_fetch_tasks = Vec::new();
for inlay_fetch_range in inlay_fetch_ranges {
let inlays_up_to_date = self.inlays_up_to_date(
&inlay_fetch_range.buffer_path,
&inlay_fetch_range.buffer_version,
inlay_fetch_range.excerpt_id,
);
let task_multi_buffer = multi_buffer.clone();
let task = cx.spawn(|editor, mut cx| async move {
if inlays_up_to_date {
anyhow::Ok((inlay_fetch_range, None))
} else {
let Some(buffer_handle) = cx.read(|cx| task_multi_buffer.read(cx).buffer(inlay_fetch_range.buffer_id))
else { return Ok((inlay_fetch_range, Some(Vec::new()))) };
let task = editor
.update(&mut cx, |editor, cx| {
let max_buffer_offset = buffer_handle.read(cx).len();
let excerpt_offset_range = &inlay_fetch_range.excerpt_offset_range;
editor.project.as_ref().map(|project| {
project.update(cx, |project, cx| {
project.query_inlay_hints_for_buffer(
buffer_handle,
excerpt_offset_range.start..excerpt_offset_range.end.min(max_buffer_offset),
cx,
)
})
})
})
.context("inlays fecth task spawn")?;
Ok((inlay_fetch_range, match task {
Some(task) => task.await.context("inlays for buffer task")?,
None => Some(Vec::new()),
}))
}
});
inlay_fetch_tasks.push(task);
}
let final_task = cx.spawn(|editor, mut cx| async move {
let mut inlay_updates: HashMap<
PathBuf,
(
Global,
HashMap<ExcerptId, Option<(Range<usize>, OrderedByAnchorOffset<InlayHint>)>>,
),
> = HashMap::default();
let multi_buffer_snapshot =
editor.read_with(&cx, |editor, cx| editor.buffer().read(cx).snapshot(cx))?;
for task_result in futures::future::join_all(inlay_fetch_tasks).await {
match task_result {
Ok((request_key, response_inlays)) => {
let inlays_per_excerpt = HashMap::from_iter([(
request_key.excerpt_id,
response_inlays
.map(|excerpt_inlays| {
excerpt_inlays.into_iter().fold(
OrderedByAnchorOffset::default(),
|mut ordered_inlays, inlay| {
let anchor = multi_buffer_snapshot.anchor_in_excerpt(
request_key.excerpt_id,
inlay.position,
);
ordered_inlays.add(anchor, inlay);
ordered_inlays
},
)
})
.map(|inlays| (request_key.excerpt_offset_range, inlays)),
)]);
match inlay_updates.entry(request_key.buffer_path) {
hash_map::Entry::Occupied(mut o) => {
o.get_mut().1.extend(inlays_per_excerpt);
}
hash_map::Entry::Vacant(v) => {
v.insert((request_key.buffer_version, inlays_per_excerpt));
}
}
}
Err(e) => error!("Failed to update inlays for buffer: {e:#}"),
}
}
let updates = if !inlay_updates.is_empty() {
let inlays_update = editor.update(&mut cx, |editor, _| {
editor.inlay_cache.apply_fetch_inlays(inlay_updates)
})?;
inlays_update
} else {
InlaysUpdate::default()
};
anyhow::Ok(updates)
});
final_task
}
fn inlays_up_to_date(
&self,
buffer_path: &Path,
buffer_version: &Global,
@@ -69,17 +220,17 @@ impl InlayCache {
) -> bool {
let Some(buffer_inlays) = self.inlays_per_buffer.get(buffer_path) else { return false };
let buffer_up_to_date = buffer_version == &buffer_inlays.buffer_version
|| buffer_inlays.buffer_version.changed_since(buffer_version);
|| buffer_inlays.buffer_version.changed_since(&buffer_version);
buffer_up_to_date && buffer_inlays.inlays_per_excerpts.contains_key(&excerpt_id)
}
pub fn update_inlays(
fn apply_fetch_inlays(
&mut self,
inlay_updates: HashMap<
fetched_inlays: HashMap<
PathBuf,
(
Global,
HashMap<ExcerptId, Option<OrderedByAnchorOffset<InlayHint>>>,
HashMap<ExcerptId, Option<(Range<usize>, OrderedByAnchorOffset<InlayHint>)>>,
),
>,
) -> InlaysUpdate {
@@ -87,10 +238,17 @@ impl InlayCache {
let mut to_remove = Vec::new();
let mut to_insert = Vec::new();
for (buffer_path, (buffer_version, new_buffer_inlays)) in inlay_updates {
for (buffer_path, (buffer_version, new_buffer_inlays)) in fetched_inlays {
match old_inlays.remove(&buffer_path) {
Some(mut old_buffer_inlays) => {
for (excerpt_id, new_excerpt_inlays) in new_buffer_inlays {
let (_, mut new_excerpt_inlays) = match new_excerpt_inlays {
Some((excerpt_offset_range, new_inlays)) => (
excerpt_offset_range,
new_inlays.into_ordered_elements().fuse().peekable(),
),
None => continue,
};
if self.inlays_up_to_date(&buffer_path, &buffer_version, excerpt_id) {
continue;
}
@@ -99,12 +257,7 @@ impl InlayCache {
.inlays_per_buffer
.get_mut(&buffer_path)
.expect("element expected: `old_inlays.remove` returned `Some`");
let mut new_excerpt_inlays = match new_excerpt_inlays {
Some(new_inlays) => {
new_inlays.into_ordered_elements().fuse().peekable()
}
None => continue,
};
if old_buffer_inlays
.inlays_per_excerpts
.remove(&excerpt_id)
@@ -192,7 +345,7 @@ impl InlayCache {
OrderedByAnchorOffset<(InlayId, InlayHint)>,
> = HashMap::default();
for (new_excerpt_id, new_ordered_inlays) in new_buffer_inlays {
if let Some(new_ordered_inlays) = new_ordered_inlays {
if let Some((_, new_ordered_inlays)) = new_ordered_inlays {
for (new_anchor, new_inlay) in
new_ordered_inlays.into_ordered_elements()
{
@@ -230,6 +383,49 @@ impl InlayCache {
}
}
pub fn apply_settings(
&mut self,
inlay_hint_settings: editor_settings::InlayHints,
) -> InlaysUpdate {
let new_allowed_inlay_hint_types = allowed_inlay_hint_types(inlay_hint_settings);
let new_allowed_hint_kinds = new_allowed_inlay_hint_types
.difference(&self.allowed_hint_kinds)
.copied()
.collect::<HashSet<_>>();
let removed_hint_kinds = self
.allowed_hint_kinds
.difference(&new_allowed_inlay_hint_types)
.collect::<HashSet<_>>();
let mut to_remove = Vec::new();
let mut to_insert = Vec::new();
for (anchor, (inlay_id, inlay_hint)) in self
.inlays_per_buffer
.iter()
.map(|(_, buffer_inlays)| {
buffer_inlays
.inlays_per_excerpts
.iter()
.map(|(_, excerpt_inlays)| excerpt_inlays.ordered_elements())
.flatten()
})
.flatten()
{
if removed_hint_kinds.contains(&inlay_hint.kind) {
to_remove.push(*inlay_id);
} else if new_allowed_hint_kinds.contains(&inlay_hint.kind) {
to_insert.push((*inlay_id, *anchor, inlay_hint.to_owned()));
}
}
self.allowed_hint_kinds = new_allowed_hint_kinds;
InlaysUpdate {
to_remove,
to_insert,
}
}
pub fn clear(&mut self) -> Vec<InlayId> {
self.inlays_per_buffer
.drain()
@@ -248,3 +444,19 @@ impl InlayCache {
.collect()
}
}
fn allowed_inlay_hint_types(
inlay_hint_settings: editor_settings::InlayHints,
) -> HashSet<Option<InlayHintKind>> {
let mut new_allowed_inlay_hint_types = HashSet::default();
if inlay_hint_settings.show_type_hints {
new_allowed_inlay_hint_types.insert(Some(InlayHintKind::Type));
}
if inlay_hint_settings.show_parameter_hints {
new_allowed_inlay_hint_types.insert(Some(InlayHintKind::Parameter));
}
if inlay_hint_settings.show_other_hints {
new_allowed_inlay_hint_types.insert(None);
}
new_allowed_inlay_hint_types
}