Finish modelling
This commit is contained in:
parent
4c78019317
commit
8d982a6c2d
2 changed files with 337 additions and 332 deletions
|
@ -2647,6 +2647,7 @@ impl Editor {
|
||||||
vec![new_query],
|
vec![new_query],
|
||||||
current_inlays,
|
current_inlays,
|
||||||
false,
|
false,
|
||||||
|
cx,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2668,6 +2669,7 @@ impl Editor {
|
||||||
replacement_queries,
|
replacement_queries,
|
||||||
current_inlays,
|
current_inlays,
|
||||||
true,
|
true,
|
||||||
|
cx,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,17 +1,17 @@
|
||||||
use std::cmp;
|
use std::cmp;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
display_map::Inlay, editor_settings, Anchor, Editor, ExcerptId, InlayId, MultiBuffer,
|
display_map::Inlay, editor_settings, Anchor, Editor, ExcerptId, InlayId, MultiBufferSnapshot,
|
||||||
MultiBufferSnapshot,
|
|
||||||
};
|
};
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use clock::Global;
|
use clock::Global;
|
||||||
use futures::{stream::FuturesUnordered, FutureExt, StreamExt};
|
use futures::{stream::FuturesUnordered, FutureExt, StreamExt};
|
||||||
use gpui::{ModelHandle, Task, ViewContext};
|
use gpui::{Task, ViewContext};
|
||||||
use log::error;
|
use log::error;
|
||||||
use project::{InlayHint, InlayHintKind};
|
use project::{InlayHint, InlayHintKind};
|
||||||
|
|
||||||
use collections::{hash_map, HashMap, HashSet};
|
use collections::{HashMap, HashSet};
|
||||||
|
use util::post_inc;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct InlayHintCache {
|
pub struct InlayHintCache {
|
||||||
|
@ -57,36 +57,36 @@ impl InlayHintCache {
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let (hint_updates_tx, hint_updates_rx) = smol::channel::unbounded();
|
let (hint_updates_tx, hint_updates_rx) = smol::channel::unbounded();
|
||||||
let (update_results_tx, update_results_rx) = smol::channel::unbounded();
|
let (update_results_tx, update_results_rx) = smol::channel::unbounded();
|
||||||
|
|
||||||
spawn_hints_update_loop(hint_updates_rx, update_results_tx, cx);
|
spawn_hints_update_loop(hint_updates_rx, update_results_tx, cx);
|
||||||
cx.spawn(|editor, mut cx| async move {
|
cx.spawn(|editor, mut cx| async move {
|
||||||
while let Ok(update_result) = update_results_rx.recv().await {
|
while let Ok(update_result) = update_results_rx.recv().await {
|
||||||
let editor_absent = editor
|
let editor_absent = editor
|
||||||
.update(&mut cx, |editor, cx| {
|
.update(&mut cx, |editor, cx| {
|
||||||
let multi_buffer_snapshot = editor.buffer().read(cx).snapshot(cx);
|
let multi_buffer_snapshot = editor.buffer().read(cx).snapshot(cx);
|
||||||
let inlay_hint_cache = &mut editor.inlay_hint_cache;
|
if let Some((splice, remove_from_cache)) = match update_result {
|
||||||
if let Some(new_allowed_hint_kinds) = update_result.new_allowed_hint_kinds {
|
UpdateResult::HintQuery {
|
||||||
inlay_hint_cache.allowed_hint_kinds = new_allowed_hint_kinds;
|
query,
|
||||||
}
|
add_to_cache,
|
||||||
|
remove_from_cache,
|
||||||
inlay_hint_cache.hints_in_buffers.retain(|_, buffer_hints| {
|
remove_from_visible,
|
||||||
buffer_hints.hints_per_excerpt.retain(|_, excerpt_hints| {
|
} => editor.buffer().read(cx).buffer(query.buffer_id).and_then(
|
||||||
excerpt_hints.retain(|(_, hint_id)| {
|
|buffer| {
|
||||||
!update_result.remove_from_cache.contains(hint_id)
|
if !buffer.read(cx).version.changed_since(&query.buffer_version)
|
||||||
});
|
{
|
||||||
!excerpt_hints.is_empty()
|
let mut new_hints_splice = InlaySplice {
|
||||||
});
|
to_remove: remove_from_visible,
|
||||||
!buffer_hints.hints_per_excerpt.is_empty()
|
to_insert: Vec::new(),
|
||||||
});
|
};
|
||||||
inlay_hint_cache.inlay_hints.retain(|hint_id, _| {
|
for (new_buffer_id, new_buffer_inlays) in add_to_cache {
|
||||||
!update_result.remove_from_cache.contains(hint_id)
|
let cached_buffer_hints = editor
|
||||||
});
|
.inlay_hint_cache
|
||||||
|
|
||||||
for (new_buffer_id, new_buffer_inlays) in update_result.add_to_cache {
|
|
||||||
let cached_buffer_hints = inlay_hint_cache
|
|
||||||
.hints_in_buffers
|
.hints_in_buffers
|
||||||
.entry(new_buffer_id)
|
.entry(new_buffer_id)
|
||||||
.or_insert_with(|| {
|
.or_insert_with(|| {
|
||||||
BufferHints::new(new_buffer_inlays.buffer_version.clone())
|
BufferHints::new(
|
||||||
|
new_buffer_inlays.buffer_version.clone(),
|
||||||
|
)
|
||||||
});
|
});
|
||||||
if cached_buffer_hints
|
if cached_buffer_hints
|
||||||
.buffer_version
|
.buffer_version
|
||||||
|
@ -101,29 +101,92 @@ impl InlayHintCache {
|
||||||
.hints_per_excerpt
|
.hints_per_excerpt
|
||||||
.entry(excerpt_id)
|
.entry(excerpt_id)
|
||||||
.or_default();
|
.or_default();
|
||||||
for (new_hint_position, new_hint, new_inlay_id) in
|
for (shown_id, new_hint_position, new_hint) in
|
||||||
new_excerpt_inlays
|
new_excerpt_inlays
|
||||||
{
|
{
|
||||||
if let hash_map::Entry::Vacant(v) =
|
let new_inlay_id = match shown_id {
|
||||||
inlay_hint_cache.inlay_hints.entry(new_inlay_id)
|
Some(id) => id,
|
||||||
|
None => {
|
||||||
|
let new_inlay_id = InlayId(post_inc(
|
||||||
|
&mut editor.next_inlay_id,
|
||||||
|
));
|
||||||
|
if editor
|
||||||
|
.inlay_hint_cache
|
||||||
|
.allowed_hint_kinds
|
||||||
|
.contains(&new_hint.kind)
|
||||||
{
|
{
|
||||||
v.insert(new_hint);
|
new_hints_splice.to_insert.push((
|
||||||
match cached_excerpt_hints.binary_search_by(|probe| {
|
new_inlay_id,
|
||||||
new_hint_position.cmp(&probe.0, &multi_buffer_snapshot)
|
new_hint_position,
|
||||||
}) {
|
new_hint.clone(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
new_inlay_id
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
editor
|
||||||
|
.inlay_hint_cache
|
||||||
|
.inlay_hints
|
||||||
|
.insert(new_inlay_id, new_hint);
|
||||||
|
match cached_excerpt_hints.binary_search_by(
|
||||||
|
|probe| {
|
||||||
|
new_hint_position.cmp(
|
||||||
|
&probe.0,
|
||||||
|
&multi_buffer_snapshot,
|
||||||
|
)
|
||||||
|
},
|
||||||
|
) {
|
||||||
Ok(ix) | Err(ix) => cached_excerpt_hints
|
Ok(ix) | Err(ix) => cached_excerpt_hints
|
||||||
.insert(ix, (new_hint_position, new_inlay_id)),
|
.insert(
|
||||||
|
ix,
|
||||||
|
(new_hint_position, new_inlay_id),
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Some((new_hints_splice, remove_from_cache))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
),
|
||||||
|
UpdateResult::Other {
|
||||||
|
new_allowed_hint_kinds,
|
||||||
|
splice,
|
||||||
|
remove_from_cache,
|
||||||
|
} => {
|
||||||
|
if let Some(new_allowed_hint_kinds) = new_allowed_hint_kinds {
|
||||||
|
editor.inlay_hint_cache.allowed_hint_kinds =
|
||||||
|
new_allowed_hint_kinds;
|
||||||
|
}
|
||||||
|
Some((splice, remove_from_cache))
|
||||||
|
}
|
||||||
|
} {
|
||||||
|
editor
|
||||||
|
.inlay_hint_cache
|
||||||
|
.hints_in_buffers
|
||||||
|
.retain(|_, buffer_hints| {
|
||||||
|
buffer_hints.hints_per_excerpt.retain(|_, excerpt_hints| {
|
||||||
|
excerpt_hints.retain(|(_, hint_id)| {
|
||||||
|
!remove_from_cache.contains(hint_id)
|
||||||
|
});
|
||||||
|
!excerpt_hints.is_empty()
|
||||||
|
});
|
||||||
|
!buffer_hints.hints_per_excerpt.is_empty()
|
||||||
|
});
|
||||||
|
editor
|
||||||
|
.inlay_hint_cache
|
||||||
|
.inlay_hints
|
||||||
|
.retain(|hint_id, _| !remove_from_cache.contains(hint_id));
|
||||||
|
|
||||||
let InlaySplice {
|
let InlaySplice {
|
||||||
to_remove,
|
to_remove,
|
||||||
to_insert,
|
to_insert,
|
||||||
} = update_result.splice;
|
} = splice;
|
||||||
editor.splice_inlay_hints(to_remove, to_insert, cx)
|
editor.splice_inlay_hints(to_remove, to_insert, cx)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.is_err();
|
.is_err();
|
||||||
if editor_absent {
|
if editor_absent {
|
||||||
|
@ -186,6 +249,7 @@ impl InlayHintCache {
|
||||||
queries: Vec<InlayHintQuery>,
|
queries: Vec<InlayHintQuery>,
|
||||||
current_inlays: Vec<Inlay>,
|
current_inlays: Vec<Inlay>,
|
||||||
conflicts_invalidate_cache: bool,
|
conflicts_invalidate_cache: bool,
|
||||||
|
cx: &mut ViewContext<Editor>,
|
||||||
) {
|
) {
|
||||||
let conflicts_with_cache = conflicts_invalidate_cache
|
let conflicts_with_cache = conflicts_invalidate_cache
|
||||||
&& queries.iter().any(|update_query| {
|
&& queries.iter().any(|update_query| {
|
||||||
|
@ -230,23 +294,36 @@ impl InlayHintCache {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.fold(
|
.fold(
|
||||||
HashMap::<u64, (Global, Vec<ExcerptId>)>::default(),
|
HashMap::<
|
||||||
|
u64,
|
||||||
|
(
|
||||||
|
Global,
|
||||||
|
HashMap<
|
||||||
|
ExcerptId,
|
||||||
|
Task<anyhow::Result<(InlayHintQuery, Option<Vec<InlayHint>>)>>,
|
||||||
|
>,
|
||||||
|
),
|
||||||
|
>::default(),
|
||||||
|mut queries_per_buffer, new_query| {
|
|mut queries_per_buffer, new_query| {
|
||||||
let (current_verison, excerpts_to_query) =
|
let (current_verison, excerpt_queries) =
|
||||||
queries_per_buffer.entry(new_query.buffer_id).or_default();
|
queries_per_buffer.entry(new_query.buffer_id).or_default();
|
||||||
|
|
||||||
if new_query.buffer_version.changed_since(current_verison) {
|
if new_query.buffer_version.changed_since(current_verison) {
|
||||||
*current_verison = new_query.buffer_version;
|
*current_verison = new_query.buffer_version.clone();
|
||||||
*excerpts_to_query = vec![new_query.excerpt_id];
|
*excerpt_queries = HashMap::from_iter([(
|
||||||
|
new_query.excerpt_id,
|
||||||
|
hints_fetch_task(new_query, cx),
|
||||||
|
)]);
|
||||||
} else if !current_verison.changed_since(&new_query.buffer_version) {
|
} else if !current_verison.changed_since(&new_query.buffer_version) {
|
||||||
excerpts_to_query.push(new_query.excerpt_id);
|
excerpt_queries
|
||||||
|
.insert(new_query.excerpt_id, hints_fetch_task(new_query, cx));
|
||||||
}
|
}
|
||||||
|
|
||||||
queries_per_buffer
|
queries_per_buffer
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
for (queried_buffer, (buffer_version, excerpts)) in queries_per_buffer {
|
for (queried_buffer, (buffer_version, excerpt_queries)) in queries_per_buffer {
|
||||||
self.hint_updates_tx
|
self.hint_updates_tx
|
||||||
.send_blocking(HintsUpdate {
|
.send_blocking(HintsUpdate {
|
||||||
multi_buffer_snapshot: multi_buffer_snapshot.clone(),
|
multi_buffer_snapshot: multi_buffer_snapshot.clone(),
|
||||||
|
@ -256,7 +333,7 @@ impl InlayHintCache {
|
||||||
invalidate_cache: conflicts_with_cache,
|
invalidate_cache: conflicts_with_cache,
|
||||||
buffer_id: queried_buffer,
|
buffer_id: queried_buffer,
|
||||||
buffer_version,
|
buffer_version,
|
||||||
excerpts,
|
excerpt_queries,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
.ok();
|
.ok();
|
||||||
|
@ -295,16 +372,24 @@ enum HintsUpdateKind {
|
||||||
BufferUpdate {
|
BufferUpdate {
|
||||||
buffer_id: u64,
|
buffer_id: u64,
|
||||||
buffer_version: Global,
|
buffer_version: Global,
|
||||||
excerpts: Vec<ExcerptId>,
|
excerpt_queries:
|
||||||
|
HashMap<ExcerptId, Task<anyhow::Result<(InlayHintQuery, Option<Vec<InlayHint>>)>>>,
|
||||||
invalidate_cache: bool,
|
invalidate_cache: bool,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
struct UpdateResult {
|
enum UpdateResult {
|
||||||
|
HintQuery {
|
||||||
|
query: InlayHintQuery,
|
||||||
|
remove_from_visible: Vec<InlayId>,
|
||||||
|
remove_from_cache: HashSet<InlayId>,
|
||||||
|
add_to_cache: HashMap<u64, BufferHints<(Option<InlayId>, Anchor, InlayHint)>>,
|
||||||
|
},
|
||||||
|
Other {
|
||||||
splice: InlaySplice,
|
splice: InlaySplice,
|
||||||
new_allowed_hint_kinds: Option<HashSet<Option<InlayHintKind>>>,
|
new_allowed_hint_kinds: Option<HashSet<Option<InlayHintKind>>>,
|
||||||
remove_from_cache: HashSet<InlayId>,
|
remove_from_cache: HashSet<InlayId>,
|
||||||
add_to_cache: HashMap<u64, BufferHints<(Anchor, InlayHint, InlayId)>>,
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HintsUpdate {
|
impl HintsUpdate {
|
||||||
|
@ -322,13 +407,13 @@ impl HintsUpdate {
|
||||||
HintsUpdateKind::BufferUpdate {
|
HintsUpdateKind::BufferUpdate {
|
||||||
buffer_id: old_buffer_id,
|
buffer_id: old_buffer_id,
|
||||||
buffer_version: old_buffer_version,
|
buffer_version: old_buffer_version,
|
||||||
excerpts: old_excerpts,
|
excerpt_queries: old_excerpt_queries,
|
||||||
..
|
..
|
||||||
},
|
},
|
||||||
HintsUpdateKind::BufferUpdate {
|
HintsUpdateKind::BufferUpdate {
|
||||||
buffer_id: new_buffer_id,
|
buffer_id: new_buffer_id,
|
||||||
buffer_version: new_buffer_version,
|
buffer_version: new_buffer_version,
|
||||||
excerpts: new_excerpts,
|
excerpt_queries: new_excerpt_queries,
|
||||||
invalidate_cache: new_invalidate_cache,
|
invalidate_cache: new_invalidate_cache,
|
||||||
},
|
},
|
||||||
) => {
|
) => {
|
||||||
|
@ -353,8 +438,7 @@ impl HintsUpdate {
|
||||||
.map(|inlay| inlay.id)
|
.map(|inlay| inlay.id)
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
if old_inlays == new_inlays {
|
if old_inlays == new_inlays {
|
||||||
old_excerpts.extend(new_excerpts.drain(..));
|
old_excerpt_queries.extend(new_excerpt_queries.drain());
|
||||||
old_excerpts.dedup();
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -371,7 +455,7 @@ impl HintsUpdate {
|
||||||
HintsUpdateKind::Clean => {
|
HintsUpdateKind::Clean => {
|
||||||
if !self.cache.inlay_hints.is_empty() || !self.visible_inlays.is_empty() {
|
if !self.cache.inlay_hints.is_empty() || !self.visible_inlays.is_empty() {
|
||||||
result_sender
|
result_sender
|
||||||
.send(UpdateResult {
|
.send(UpdateResult::Other {
|
||||||
splice: InlaySplice {
|
splice: InlaySplice {
|
||||||
to_remove: self
|
to_remove: self
|
||||||
.visible_inlays
|
.visible_inlays
|
||||||
|
@ -382,7 +466,6 @@ impl HintsUpdate {
|
||||||
},
|
},
|
||||||
new_allowed_hint_kinds: None,
|
new_allowed_hint_kinds: None,
|
||||||
remove_from_cache: self.cache.inlay_hints.keys().copied().collect(),
|
remove_from_cache: self.cache.inlay_hints.keys().copied().collect(),
|
||||||
add_to_cache: HashMap::default(),
|
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.ok();
|
.ok();
|
||||||
|
@ -396,11 +479,10 @@ impl HintsUpdate {
|
||||||
&new,
|
&new,
|
||||||
) {
|
) {
|
||||||
result_sender
|
result_sender
|
||||||
.send(UpdateResult {
|
.send(UpdateResult::Other {
|
||||||
splice,
|
splice,
|
||||||
new_allowed_hint_kinds: Some(new),
|
new_allowed_hint_kinds: Some(new),
|
||||||
remove_from_cache: HashSet::default(),
|
remove_from_cache: HashSet::default(),
|
||||||
add_to_cache: HashMap::default(),
|
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.ok();
|
.ok();
|
||||||
|
@ -408,19 +490,39 @@ impl HintsUpdate {
|
||||||
}
|
}
|
||||||
HintsUpdateKind::BufferUpdate {
|
HintsUpdateKind::BufferUpdate {
|
||||||
buffer_id,
|
buffer_id,
|
||||||
buffer_version,
|
excerpt_queries,
|
||||||
excerpts,
|
|
||||||
invalidate_cache,
|
invalidate_cache,
|
||||||
|
..
|
||||||
} => {
|
} => {
|
||||||
let mut tasks = excerpts
|
let mut task_query = excerpt_queries
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|excerpt_id| async move {
|
.map(|(excerpt_id, task)| async move {
|
||||||
//
|
let task = task.await;
|
||||||
todo!("TODO kb")
|
(excerpt_id, task)
|
||||||
})
|
})
|
||||||
.collect::<FuturesUnordered<_>>();
|
.collect::<FuturesUnordered<_>>();
|
||||||
while let Some(update) = tasks.next().await {
|
while let Some((excerpt_id, task_result)) = task_query.next().await {
|
||||||
todo!("TODO kb")
|
match task_result {
|
||||||
|
Ok((query, Some(new_hints))) => {
|
||||||
|
if !new_hints.is_empty() {
|
||||||
|
if let Some(hint_update_result) = new_excerpt_hints_update_result(
|
||||||
|
&self.multi_buffer_snapshot,
|
||||||
|
&self.visible_inlays,
|
||||||
|
&self.cache,
|
||||||
|
query,
|
||||||
|
new_hints,
|
||||||
|
invalidate_cache,
|
||||||
|
) {
|
||||||
|
result_sender
|
||||||
|
.send(hint_update_result)
|
||||||
|
.await
|
||||||
|
.ok();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Ok((_, None)) => {},
|
||||||
|
Err(e) => error!("Excerpt {excerpt_id:?} from buffer {buffer_id} failed to update its hints: {e:#}"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -514,7 +616,7 @@ fn new_allowed_hint_kinds_splice(
|
||||||
|
|
||||||
let mut to_remove = Vec::new();
|
let mut to_remove = Vec::new();
|
||||||
let mut to_insert = Vec::new();
|
let mut to_insert = Vec::new();
|
||||||
let mut shown_hints_to_remove = group_inlays(current_inlays);
|
let mut shown_hints_to_remove = group_inlays(¤t_inlays);
|
||||||
|
|
||||||
for (buffer_id, cached_buffer_hints) in &hints_cache.hints_in_buffers {
|
for (buffer_id, cached_buffer_hints) in &hints_cache.hints_in_buffers {
|
||||||
let shown_buffer_hints_to_remove = shown_hints_to_remove.entry(*buffer_id).or_default();
|
let shown_buffer_hints_to_remove = shown_hints_to_remove.entry(*buffer_id).or_default();
|
||||||
|
@ -589,6 +691,117 @@ fn new_allowed_hint_kinds_splice(
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn new_excerpt_hints_update_result(
|
||||||
|
multi_buffer_snapshot: &MultiBufferSnapshot,
|
||||||
|
current_inlays: &[Inlay],
|
||||||
|
inlay_hint_cache: &CacheSnapshot,
|
||||||
|
query: InlayHintQuery,
|
||||||
|
new_excerpt_hints: Vec<InlayHint>,
|
||||||
|
invalidate_cache: bool,
|
||||||
|
) -> Option<UpdateResult> {
|
||||||
|
let mut remove_from_visible = Vec::new();
|
||||||
|
let mut remove_from_cache = HashSet::default();
|
||||||
|
let mut add_to_cache: HashMap<u64, BufferHints<(Option<InlayId>, Anchor, InlayHint)>> =
|
||||||
|
HashMap::default();
|
||||||
|
let mut cache_hints_to_persist: HashSet<InlayId> = HashSet::default();
|
||||||
|
|
||||||
|
let currently_shown_hints = group_inlays(¤t_inlays);
|
||||||
|
let empty = Vec::new();
|
||||||
|
let cached_excerpt_hints = inlay_hint_cache
|
||||||
|
.hints_in_buffers
|
||||||
|
.get(&query.buffer_id)
|
||||||
|
.map(|buffer_hints| &buffer_hints.hints_per_excerpt)
|
||||||
|
.and_then(|excerpt_hints_hints| excerpt_hints_hints.get(&query.excerpt_id))
|
||||||
|
.unwrap_or(&empty);
|
||||||
|
let shown_excerpt_hints = currently_shown_hints
|
||||||
|
.get(&query.buffer_id)
|
||||||
|
.and_then(|hints| hints.get(&query.excerpt_id))
|
||||||
|
.unwrap_or(&empty);
|
||||||
|
for new_hint in new_excerpt_hints {
|
||||||
|
let new_hint_anchor =
|
||||||
|
multi_buffer_snapshot.anchor_in_excerpt(query.excerpt_id, new_hint.position);
|
||||||
|
let should_add_to_cache = match cached_excerpt_hints
|
||||||
|
.binary_search_by(|probe| new_hint_anchor.cmp(&probe.0, &multi_buffer_snapshot))
|
||||||
|
{
|
||||||
|
Ok(ix) => {
|
||||||
|
let (_, cached_inlay_id) = cached_excerpt_hints[ix];
|
||||||
|
let cache_hit = inlay_hint_cache
|
||||||
|
.inlay_hints
|
||||||
|
.get(&cached_inlay_id)
|
||||||
|
.filter(|cached_hint| cached_hint == &&new_hint)
|
||||||
|
.is_some();
|
||||||
|
if cache_hit {
|
||||||
|
cache_hints_to_persist.insert(cached_inlay_id);
|
||||||
|
false
|
||||||
|
} else {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => true,
|
||||||
|
};
|
||||||
|
|
||||||
|
let shown_inlay_id = match shown_excerpt_hints
|
||||||
|
.binary_search_by(|probe| probe.0.cmp(&new_hint_anchor, &multi_buffer_snapshot))
|
||||||
|
{
|
||||||
|
Ok(ix) => {
|
||||||
|
let (_, shown_inlay_id) = shown_excerpt_hints[ix];
|
||||||
|
let shown_hint_found = inlay_hint_cache
|
||||||
|
.inlay_hints
|
||||||
|
.get(&shown_inlay_id)
|
||||||
|
.filter(|cached_hint| cached_hint == &&new_hint)
|
||||||
|
.is_some();
|
||||||
|
if shown_hint_found {
|
||||||
|
Some(shown_inlay_id)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => None,
|
||||||
|
};
|
||||||
|
|
||||||
|
if should_add_to_cache {
|
||||||
|
let id_to_add = match shown_inlay_id {
|
||||||
|
Some(shown_inlay_id) => {
|
||||||
|
cache_hints_to_persist.insert(shown_inlay_id);
|
||||||
|
Some(shown_inlay_id)
|
||||||
|
}
|
||||||
|
None => None,
|
||||||
|
};
|
||||||
|
add_to_cache
|
||||||
|
.entry(query.buffer_id)
|
||||||
|
.or_insert_with(|| BufferHints::new(query.buffer_version.clone()))
|
||||||
|
.hints_per_excerpt
|
||||||
|
.entry(query.excerpt_id)
|
||||||
|
.or_default()
|
||||||
|
.push((id_to_add, new_hint_anchor, new_hint.clone()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if invalidate_cache {
|
||||||
|
remove_from_visible.extend(
|
||||||
|
shown_excerpt_hints
|
||||||
|
.iter()
|
||||||
|
.map(|(_, hint_id)| hint_id)
|
||||||
|
.filter(|hint_id| cache_hints_to_persist.contains(hint_id))
|
||||||
|
.copied(),
|
||||||
|
);
|
||||||
|
remove_from_cache.extend(
|
||||||
|
inlay_hint_cache
|
||||||
|
.inlay_hints
|
||||||
|
.keys()
|
||||||
|
.filter(|cached_inlay_id| cache_hints_to_persist.contains(cached_inlay_id))
|
||||||
|
.copied(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(UpdateResult::HintQuery {
|
||||||
|
query,
|
||||||
|
remove_from_visible,
|
||||||
|
remove_from_cache,
|
||||||
|
add_to_cache,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
fn allowed_hint_types(
|
fn allowed_hint_types(
|
||||||
inlay_hint_settings: editor_settings::InlayHints,
|
inlay_hint_settings: editor_settings::InlayHints,
|
||||||
) -> HashSet<Option<InlayHintKind>> {
|
) -> HashSet<Option<InlayHintKind>> {
|
||||||
|
@ -605,27 +818,21 @@ fn allowed_hint_types(
|
||||||
new_allowed_hint_types
|
new_allowed_hint_types
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO kb wrong, query and update the editor separately
|
fn hints_fetch_task(
|
||||||
fn fetch_queries(
|
query: InlayHintQuery,
|
||||||
multi_buffer: ModelHandle<MultiBuffer>,
|
|
||||||
queries: impl Iterator<Item = InlayHintQuery>,
|
|
||||||
cx: &mut ViewContext<'_, '_, Editor>,
|
cx: &mut ViewContext<'_, '_, Editor>,
|
||||||
) -> Task<anyhow::Result<HashMap<u64, BufferHints<InlayHint>>>> {
|
) -> Task<anyhow::Result<(InlayHintQuery, Option<Vec<InlayHint>>)>> {
|
||||||
let mut inlay_fetch_tasks = Vec::new();
|
cx.spawn(|editor, mut cx| async move {
|
||||||
for query in queries {
|
let Ok(task) = editor
|
||||||
let task_multi_buffer = multi_buffer.clone();
|
|
||||||
let task = cx.spawn(|editor, mut cx| async move {
|
|
||||||
let Some(buffer_handle) = cx.read(|cx| task_multi_buffer.read(cx).buffer(query.buffer_id))
|
|
||||||
else { return anyhow::Ok((query, Some(Vec::new()))) };
|
|
||||||
let task = editor
|
|
||||||
.update(&mut cx, |editor, cx| {
|
.update(&mut cx, |editor, cx| {
|
||||||
if let Some((_, excerpt_range)) = task_multi_buffer.read(cx)
|
Some({
|
||||||
|
let multi_buffer = editor.buffer().read(cx);
|
||||||
|
let buffer_handle = multi_buffer.buffer(query.buffer_id)?;
|
||||||
|
let (_, excerpt_range) = multi_buffer
|
||||||
.excerpts_for_buffer(&buffer_handle, cx)
|
.excerpts_for_buffer(&buffer_handle, cx)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.find(|(excerpt_id, _)| excerpt_id == &query.excerpt_id)
|
.find(|(excerpt_id, _)| excerpt_id == &query.excerpt_id)?;
|
||||||
{
|
editor.project.as_ref()?.update(cx, |project, cx| {
|
||||||
editor.project.as_ref().map(|project| {
|
|
||||||
project.update(cx, |project, cx| {
|
|
||||||
project.query_inlay_hints_for_buffer(
|
project.query_inlay_hints_for_buffer(
|
||||||
buffer_handle,
|
buffer_handle,
|
||||||
excerpt_range.context,
|
excerpt_range.context,
|
||||||
|
@ -633,11 +840,9 @@ fn fetch_queries(
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
} else {
|
}) else {
|
||||||
None
|
return Ok((query, None));
|
||||||
}
|
};
|
||||||
})
|
|
||||||
.context("inlays fetch task spawn")?;
|
|
||||||
Ok((
|
Ok((
|
||||||
query,
|
query,
|
||||||
match task {
|
match task {
|
||||||
|
@ -645,46 +850,10 @@ fn fetch_queries(
|
||||||
None => Some(Vec::new()),
|
None => Some(Vec::new()),
|
||||||
},
|
},
|
||||||
))
|
))
|
||||||
});
|
|
||||||
|
|
||||||
inlay_fetch_tasks.push(task);
|
|
||||||
}
|
|
||||||
|
|
||||||
cx.spawn(|editor, cx| async move {
|
|
||||||
let mut inlay_updates: HashMap<u64, BufferHints<InlayHint>> = HashMap::default();
|
|
||||||
for task_result in futures::future::join_all(inlay_fetch_tasks).await {
|
|
||||||
match task_result {
|
|
||||||
Ok((query, Some(response_hints))) => {
|
|
||||||
let Some(buffer_snapshot) = editor.read_with(&cx, |editor, cx| {
|
|
||||||
editor.buffer().read(cx).buffer(query.buffer_id).map(|buffer| buffer.read(cx).snapshot())
|
|
||||||
})? else { continue; };
|
|
||||||
let buffer_hints = inlay_updates
|
|
||||||
.entry(query.buffer_id)
|
|
||||||
.or_insert_with(|| BufferHints::new(query.buffer_version.clone()));
|
|
||||||
if buffer_snapshot.version().changed_since(&buffer_hints.buffer_version) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
let cached_excerpt_hints = buffer_hints
|
|
||||||
.hints_per_excerpt
|
|
||||||
.entry(query.excerpt_id)
|
|
||||||
.or_default();
|
|
||||||
for inlay in response_hints {
|
|
||||||
match cached_excerpt_hints.binary_search_by(|probe| {
|
|
||||||
inlay.position.cmp(&probe.position, &buffer_snapshot)
|
|
||||||
}) {
|
|
||||||
Ok(ix) | Err(ix) => cached_excerpt_hints.insert(ix, inlay),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok((_, None)) => {}
|
|
||||||
Err(e) => error!("Failed to update inlays for buffer: {e:#}"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(inlay_updates)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn group_inlays(inlays: Vec<Inlay>) -> HashMap<u64, HashMap<ExcerptId, Vec<(Anchor, InlayId)>>> {
|
fn group_inlays(inlays: &[Inlay]) -> HashMap<u64, HashMap<ExcerptId, Vec<(Anchor, InlayId)>>> {
|
||||||
inlays.into_iter().fold(
|
inlays.into_iter().fold(
|
||||||
HashMap::<u64, HashMap<ExcerptId, Vec<(Anchor, InlayId)>>>::default(),
|
HashMap::<u64, HashMap<ExcerptId, Vec<(Anchor, InlayId)>>>::default(),
|
||||||
|mut current_hints, inlay| {
|
|mut current_hints, inlay| {
|
||||||
|
@ -700,169 +869,3 @@ fn group_inlays(inlays: Vec<Inlay>) -> HashMap<u64, HashMap<ExcerptId, Vec<(Anch
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// async fn update_hints(
|
|
||||||
// multi_buffer: ModelHandle<MultiBuffer>,
|
|
||||||
// queries: Vec<InlayHintQuery>,
|
|
||||||
// current_inlays: Vec<Inlay>,
|
|
||||||
// invalidate_cache: bool,
|
|
||||||
// cx: &mut ViewContext<'_, '_, Editor>,
|
|
||||||
// ) -> Option<InlaySplice> {
|
|
||||||
// let fetch_queries_task = fetch_queries(multi_buffer, queries.into_iter(), cx);
|
|
||||||
// let new_hints = fetch_queries_task.await.context("inlay hints fetch")?;
|
|
||||||
|
|
||||||
// let mut to_remove = Vec::new();
|
|
||||||
// let mut to_insert = Vec::new();
|
|
||||||
// let mut cache_hints_to_persist: HashMap<u64, (Global, HashMap<ExcerptId, HashSet<InlayId>>)> =
|
|
||||||
// HashMap::default();
|
|
||||||
|
|
||||||
// editor.update(&mut cx, |editor, cx| {
|
|
||||||
// let multi_buffer_snapshot = task_multi_buffer.read(cx).snapshot(cx);
|
|
||||||
// for (new_buffer_id, new_hints_per_buffer) in new_hints {
|
|
||||||
// let cached_buffer_hints = editor
|
|
||||||
// .inlay_hint_cache
|
|
||||||
// .hints_in_buffers
|
|
||||||
// .entry(new_buffer_id)
|
|
||||||
// .or_insert_with(|| {
|
|
||||||
// BufferHints::new(new_hints_per_buffer.buffer_version.clone())
|
|
||||||
// });
|
|
||||||
|
|
||||||
// let buffer_cache_hints_to_persist =
|
|
||||||
// cache_hints_to_persist.entry(new_buffer_id).or_insert_with(|| (new_hints_per_buffer.buffer_version.clone(), HashMap::default()));
|
|
||||||
// if cached_buffer_hints
|
|
||||||
// .buffer_version
|
|
||||||
// .changed_since(&new_hints_per_buffer.buffer_version)
|
|
||||||
// {
|
|
||||||
// buffer_cache_hints_to_persist.0 = new_hints_per_buffer.buffer_version;
|
|
||||||
// buffer_cache_hints_to_persist.1.extend(
|
|
||||||
// cached_buffer_hints.hints_per_excerpt.iter().map(
|
|
||||||
// |(excerpt_id, excerpt_hints)| {
|
|
||||||
// (
|
|
||||||
// *excerpt_id,
|
|
||||||
// excerpt_hints.iter().map(|(_, id)| *id).collect(),
|
|
||||||
// )
|
|
||||||
// },
|
|
||||||
// ),
|
|
||||||
// );
|
|
||||||
// continue;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// let shown_buffer_hints = currently_shown_hints.get(&new_buffer_id);
|
|
||||||
// for (new_excerpt_id, new_hints_per_excerpt) in
|
|
||||||
// new_hints_per_buffer.hints_per_excerpt
|
|
||||||
// {
|
|
||||||
// let excerpt_cache_hints_to_persist = buffer_cache_hints_to_persist.1
|
|
||||||
// .entry(new_excerpt_id)
|
|
||||||
// .or_default();
|
|
||||||
// let cached_excerpt_hints = cached_buffer_hints
|
|
||||||
// .hints_per_excerpt
|
|
||||||
// .entry(new_excerpt_id)
|
|
||||||
// .or_default();
|
|
||||||
// let empty_shown_excerpt_hints = Vec::new();
|
|
||||||
// let shown_excerpt_hints = shown_buffer_hints.and_then(|hints| hints.get(&new_excerpt_id)).unwrap_or(&empty_shown_excerpt_hints);
|
|
||||||
// for new_hint in new_hints_per_excerpt {
|
|
||||||
// let new_hint_anchor = multi_buffer_snapshot
|
|
||||||
// .anchor_in_excerpt(new_excerpt_id, new_hint.position);
|
|
||||||
// let cache_insert_ix = match cached_excerpt_hints.binary_search_by(|probe| {
|
|
||||||
// new_hint_anchor.cmp(&probe.0, &multi_buffer_snapshot)
|
|
||||||
// }) {
|
|
||||||
// Ok(ix) => {
|
|
||||||
// let (_, cached_inlay_id) = cached_excerpt_hints[ix];
|
|
||||||
// let cache_hit = editor
|
|
||||||
// .inlay_hint_cache
|
|
||||||
// .inlay_hints
|
|
||||||
// .get(&cached_inlay_id)
|
|
||||||
// .filter(|cached_hint| cached_hint == &&new_hint)
|
|
||||||
// .is_some();
|
|
||||||
// if cache_hit {
|
|
||||||
// excerpt_cache_hints_to_persist
|
|
||||||
// .insert(cached_inlay_id);
|
|
||||||
// None
|
|
||||||
// } else {
|
|
||||||
// Some(ix)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// Err(ix) => Some(ix),
|
|
||||||
// };
|
|
||||||
|
|
||||||
// let shown_inlay_id = match shown_excerpt_hints.binary_search_by(|probe| {
|
|
||||||
// probe.0.cmp(&new_hint_anchor, &multi_buffer_snapshot)
|
|
||||||
// }) {
|
|
||||||
// Ok(ix) => {{
|
|
||||||
// let (_, shown_inlay_id) = shown_excerpt_hints[ix];
|
|
||||||
// let shown_hint_found = editor.inlay_hint_cache.inlay_hints.get(&shown_inlay_id)
|
|
||||||
// .filter(|cached_hint| cached_hint == &&new_hint).is_some();
|
|
||||||
// if shown_hint_found {
|
|
||||||
// Some(shown_inlay_id)
|
|
||||||
// } else {
|
|
||||||
// None
|
|
||||||
// }
|
|
||||||
// }},
|
|
||||||
// Err(_) => None,
|
|
||||||
// };
|
|
||||||
|
|
||||||
// if let Some(insert_ix) = cache_insert_ix {
|
|
||||||
// let hint_id = match shown_inlay_id {
|
|
||||||
// Some(shown_inlay_id) => shown_inlay_id,
|
|
||||||
// None => {
|
|
||||||
// let new_hint_id = InlayId(post_inc(&mut editor.next_inlay_id));
|
|
||||||
// if editor.inlay_hint_cache.allowed_hint_kinds.contains(&new_hint.kind)
|
|
||||||
// {
|
|
||||||
// to_insert.push((new_hint_id, new_hint_anchor, new_hint.clone()));
|
|
||||||
// }
|
|
||||||
// new_hint_id
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
// excerpt_cache_hints_to_persist.insert(hint_id);
|
|
||||||
// cached_excerpt_hints.insert(insert_ix, (new_hint_anchor, hint_id));
|
|
||||||
// editor
|
|
||||||
// .inlay_hint_cache
|
|
||||||
// .inlay_hints
|
|
||||||
// .insert(hint_id, new_hint);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if conflicts_with_cache {
|
|
||||||
// for (shown_buffer_id, mut shown_hints_to_clean) in currently_shown_hints {
|
|
||||||
// match cache_hints_to_persist.get(&shown_buffer_id) {
|
|
||||||
// Some(cached_buffer_hints) => {
|
|
||||||
// for (persisted_id, cached_hints) in &cached_buffer_hints.1 {
|
|
||||||
// shown_hints_to_clean.entry(*persisted_id).or_default()
|
|
||||||
// .retain(|(_, shown_id)| !cached_hints.contains(shown_id));
|
|
||||||
// }
|
|
||||||
// },
|
|
||||||
// None => {},
|
|
||||||
// }
|
|
||||||
// to_remove.extend(shown_hints_to_clean.into_iter()
|
|
||||||
// .flat_map(|(_, excerpt_hints)| excerpt_hints.into_iter().map(|(_, hint_id)| hint_id)));
|
|
||||||
// }
|
|
||||||
|
|
||||||
// editor.inlay_hint_cache.hints_in_buffers.retain(|buffer_id, buffer_hints| {
|
|
||||||
// let Some(mut buffer_hints_to_persist) = cache_hints_to_persist.remove(buffer_id) else { return false; };
|
|
||||||
// buffer_hints.buffer_version = buffer_hints_to_persist.0;
|
|
||||||
// buffer_hints.hints_per_excerpt.retain(|excerpt_id, excerpt_hints| {
|
|
||||||
// let Some(excerpt_hints_to_persist) = buffer_hints_to_persist.1.remove(&excerpt_id) else { return false; };
|
|
||||||
// excerpt_hints.retain(|(_, hint_id)| {
|
|
||||||
// let retain = excerpt_hints_to_persist.contains(hint_id);
|
|
||||||
// if !retain {
|
|
||||||
// editor
|
|
||||||
// .inlay_hint_cache
|
|
||||||
// .inlay_hints
|
|
||||||
// .remove(hint_id);
|
|
||||||
// }
|
|
||||||
// retain
|
|
||||||
// });
|
|
||||||
// !excerpt_hints.is_empty()
|
|
||||||
// });
|
|
||||||
// !buffer_hints.hints_per_excerpt.is_empty()
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Some(InlaySplice {
|
|
||||||
// to_remove,
|
|
||||||
// to_insert,
|
|
||||||
// })
|
|
||||||
// })
|
|
||||||
// }
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue