Simplify inlay map data

This commit is contained in:
Kirill Bulatov 2023-07-05 15:23:56 +03:00
parent 5505ebf4bc
commit 6ba1c3071a

View file

@ -2,7 +2,7 @@ use crate::{
multi_buffer::{MultiBufferChunks, MultiBufferRows}, multi_buffer::{MultiBufferChunks, MultiBufferRows},
Anchor, InlayId, MultiBufferSnapshot, ToOffset, Anchor, InlayId, MultiBufferSnapshot, ToOffset,
}; };
use collections::{BTreeMap, BTreeSet, HashMap}; use collections::{BTreeMap, BTreeSet};
use gpui::fonts::HighlightStyle; use gpui::fonts::HighlightStyle;
use language::{Chunk, Edit, Point, Rope, TextSummary}; use language::{Chunk, Edit, Point, Rope, TextSummary};
use std::{ use std::{
@ -19,7 +19,6 @@ use super::TextHighlights;
pub struct InlayMap { pub struct InlayMap {
snapshot: InlaySnapshot, snapshot: InlaySnapshot,
inlays_by_id: HashMap<InlayId, Inlay>,
inlays: Vec<Inlay>, inlays: Vec<Inlay>,
} }
@ -381,7 +380,6 @@ impl InlayMap {
( (
Self { Self {
snapshot: snapshot.clone(), snapshot: snapshot.clone(),
inlays_by_id: HashMap::default(),
inlays: Vec::new(), inlays: Vec::new(),
}, },
snapshot, snapshot,
@ -531,13 +529,14 @@ impl InlayMap {
let snapshot = &mut self.snapshot; let snapshot = &mut self.snapshot;
let mut edits = BTreeSet::new(); let mut edits = BTreeSet::new();
self.inlays.retain(|inlay| !to_remove.contains(&inlay.id)); self.inlays.retain(|inlay| {
for inlay_id in to_remove { let retain = !to_remove.contains(&inlay.id);
if let Some(inlay) = self.inlays_by_id.remove(&inlay_id) { if !retain {
let offset = inlay.position.to_offset(&snapshot.buffer); let offset = inlay.position.to_offset(&snapshot.buffer);
edits.insert(offset); edits.insert(offset);
} }
} retain
});
for (existing_id, properties) in to_insert { for (existing_id, properties) in to_insert {
let inlay = Inlay { let inlay = Inlay {
@ -551,7 +550,6 @@ impl InlayMap {
continue; continue;
} }
self.inlays_by_id.insert(inlay.id, inlay.clone());
match self match self
.inlays .inlays
.binary_search_by(|probe| probe.position.cmp(&inlay.position, &snapshot.buffer)) .binary_search_by(|probe| probe.position.cmp(&inlay.position, &snapshot.buffer))
@ -627,7 +625,13 @@ impl InlayMap {
}, },
)); ));
} else { } else {
to_remove.push(*self.inlays_by_id.keys().choose(rng).unwrap()); to_remove.push(
self.inlays
.iter()
.choose(rng)
.map(|inlay| inlay.id)
.unwrap(),
);
} }
} }
log::info!("removing inlays: {:?}", to_remove); log::info!("removing inlays: {:?}", to_remove);
@ -1478,8 +1482,10 @@ mod tests {
); );
// The inlays can be manually removed. // The inlays can be manually removed.
let (inlay_snapshot, _) = inlay_map let (inlay_snapshot, _) = inlay_map.splice::<String>(
.splice::<String>(inlay_map.inlays_by_id.keys().copied().collect(), Vec::new()); inlay_map.inlays.iter().map(|inlay| inlay.id).collect(),
Vec::new(),
);
assert_eq!(inlay_snapshot.text(), "abxJKLyDzefghi"); assert_eq!(inlay_snapshot.text(), "abxJKLyDzefghi");
} }