Simplify TextHighlights map (#21724)

Remove unnecessary `Option` wrapping.
This commit is contained in:
João Marcos 2024-12-09 00:27:54 -03:00 committed by GitHub
parent 2ce01ead93
commit 55ee72d84a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 12 deletions

View file

@ -82,7 +82,7 @@ pub trait ToDisplayPoint {
fn to_display_point(&self, map: &DisplaySnapshot) -> DisplayPoint;
}
type TextHighlights = TreeMap<Option<TypeId>, Arc<(HighlightStyle, Vec<Range<Anchor>>)>>;
type TextHighlights = TreeMap<TypeId, Arc<(HighlightStyle, Vec<Range<Anchor>>)>>;
type InlayHighlights = TreeMap<TypeId, TreeMap<InlayId, (HighlightStyle, InlayHighlight)>>;
/// Decides how text in a [`MultiBuffer`] should be displayed in a buffer, handling inlay hints,
@ -434,7 +434,7 @@ impl DisplayMap {
style: HighlightStyle,
) {
self.text_highlights
.insert(Some(type_id), Arc::new((style, ranges)));
.insert(type_id, Arc::new((style, ranges)));
}
pub(crate) fn highlight_inlays(
@ -457,11 +457,11 @@ impl DisplayMap {
}
pub fn text_highlights(&self, type_id: TypeId) -> Option<(HighlightStyle, &[Range<Anchor>])> {
let highlights = self.text_highlights.get(&Some(type_id))?;
let highlights = self.text_highlights.get(&type_id)?;
Some((highlights.0, &highlights.1))
}
pub fn clear_highlights(&mut self, type_id: TypeId) -> bool {
let mut cleared = self.text_highlights.remove(&Some(type_id)).is_some();
let mut cleared = self.text_highlights.remove(&type_id).is_some();
cleared |= self.inlay_highlights.remove(&type_id).is_some();
cleared
}
@ -1239,7 +1239,7 @@ impl DisplaySnapshot {
&self,
) -> Option<Arc<(HighlightStyle, Vec<Range<Anchor>>)>> {
let type_id = TypeId::of::<Tag>();
self.text_highlights.get(&Some(type_id)).cloned()
self.text_highlights.get(&type_id).cloned()
}
#[allow(unused)]

View file

@ -211,7 +211,7 @@ pub struct InlayBufferRows<'a> {
struct HighlightEndpoint {
offset: InlayOffset,
is_start: bool,
tag: Option<TypeId>,
tag: TypeId,
style: HighlightStyle,
}
@ -239,7 +239,7 @@ pub struct InlayChunks<'a> {
max_output_offset: InlayOffset,
highlight_styles: HighlightStyles,
highlight_endpoints: Peekable<vec::IntoIter<HighlightEndpoint>>,
active_highlights: BTreeMap<Option<TypeId>, HighlightStyle>,
active_highlights: BTreeMap<TypeId, HighlightStyle>,
highlights: Highlights<'a>,
snapshot: &'a InlaySnapshot,
}
@ -1096,7 +1096,7 @@ impl InlaySnapshot {
&self,
cursor: &mut Cursor<'_, Transform, (InlayOffset, usize)>,
range: &Range<InlayOffset>,
text_highlights: &TreeMap<Option<TypeId>, Arc<(HighlightStyle, Vec<Range<Anchor>>)>>,
text_highlights: &TreeMap<TypeId, Arc<(HighlightStyle, Vec<Range<Anchor>>)>>,
highlight_endpoints: &mut Vec<HighlightEndpoint>,
) {
while cursor.start().0 < range.end {
@ -1112,7 +1112,7 @@ impl InlaySnapshot {
)))
};
for (tag, text_highlights) in text_highlights.iter() {
for (&tag, text_highlights) in text_highlights.iter() {
let style = text_highlights.0;
let ranges = &text_highlights.1;
@ -1134,13 +1134,13 @@ impl InlaySnapshot {
highlight_endpoints.push(HighlightEndpoint {
offset: self.to_inlay_offset(range.start.to_offset(&self.buffer)),
is_start: true,
tag: *tag,
tag,
style,
});
highlight_endpoints.push(HighlightEndpoint {
offset: self.to_inlay_offset(range.end.to_offset(&self.buffer)),
is_start: false,
tag: *tag,
tag,
style,
});
}
@ -1708,7 +1708,7 @@ mod tests {
text_highlight_ranges.sort_by_key(|range| (range.start, Reverse(range.end)));
log::info!("highlighting text ranges {text_highlight_ranges:?}");
text_highlights.insert(
Some(TypeId::of::<()>()),
TypeId::of::<()>(),
Arc::new((
HighlightStyle::default(),
text_highlight_ranges