editor: Fix adjacent custom highlights interfering with each other (#37912)

Release Notes:

- Fixed matching bracket highlighting not highlighting closing brackets
when adjacent to each other

Co-authored-by: Finn Evers <finn@zed.dev>
This commit is contained in:
Lukas Wirth
2025-09-10 13:08:32 +00:00
committed by GitHub
co-authored by Finn Evers
parent fcdd427cf8
commit df2d097dc5
2 changed files with 15 additions and 13 deletions
@@ -25,9 +25,8 @@ pub struct CustomHighlightsChunks<'a> {
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
struct HighlightEndpoint {
offset: usize,
is_start: bool,
tag: HighlightKey,
style: HighlightStyle,
style: Option<HighlightStyle>,
}
impl<'a> CustomHighlightsChunks<'a> {
@@ -92,17 +91,20 @@ fn create_highlight_endpoints(
break;
}
let start = range.start.to_offset(buffer);
let end = range.end.to_offset(buffer);
if start == end {
continue;
}
highlight_endpoints.push(HighlightEndpoint {
offset: range.start.to_offset(buffer),
is_start: true,
offset: start,
tag,
style,
style: Some(style),
});
highlight_endpoints.push(HighlightEndpoint {
offset: range.end.to_offset(buffer),
is_start: false,
offset: end,
tag,
style,
style: None,
});
}
}
@@ -118,8 +120,8 @@ impl<'a> Iterator for CustomHighlightsChunks<'a> {
let mut next_highlight_endpoint = usize::MAX;
while let Some(endpoint) = self.highlight_endpoints.peek().copied() {
if endpoint.offset <= self.offset {
if endpoint.is_start {
self.active_highlights.insert(endpoint.tag, endpoint.style);
if let Some(style) = endpoint.style {
self.active_highlights.insert(endpoint.tag, style);
} else {
self.active_highlights.remove(&endpoint.tag);
}
@@ -168,6 +170,6 @@ impl Ord for HighlightEndpoint {
fn cmp(&self, other: &Self) -> cmp::Ordering {
self.offset
.cmp(&other.offset)
.then_with(|| other.is_start.cmp(&self.is_start))
.then_with(|| self.style.is_some().cmp(&other.style.is_some()))
}
}