Skip to content

Commit

Permalink
more elegant static dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalkuthe committed Oct 3, 2022
1 parent ed3cdd3 commit 2c6f681
Show file tree
Hide file tree
Showing 5 changed files with 392 additions and 250 deletions.
4 changes: 3 additions & 1 deletion helix-core/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use crate::{
transaction::{ChangeSet, Operation},
Rope, RopeSlice, Tendril,
};
pub use overlay::{monotonic_overlay, overlapping_overlay, Span};
pub use overlay::{
monotonic_overlay, overlapping_overlay, MonotonicOverlay, OverlappingOverlay, Span,
};

use arc_swap::{ArcSwap, Guard};
use slotmap::{DefaultKey as LayerId, HopSlotMap};
Expand Down
35 changes: 25 additions & 10 deletions helix-core/src/syntax/overlay.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::iter::{self, Peekable};
use std::iter::Peekable;
use std::mem::replace;
use std::ops::Range;

Expand All @@ -8,6 +8,8 @@ use HighlightEvent::*;
#[cfg(test)]
mod test;

pub type MonotonicOverlay<Events, Spans> = Overlay<Events, Spans, false>;

/// Overlays multiple different highlights from `spans` onto the `HighlightEvent` stream `events`.
///
/// The [`Span`]s yielded by `spans` **must never overlap** or the iterator will produce incorrect results.
Expand All @@ -20,7 +22,7 @@ mod test;
pub fn monotonic_overlay<Events, Spans>(
events: Events,
spans: Spans,
) -> Overlay<Events, Spans, false>
) -> MonotonicOverlay<Events, Spans>
where
Events: Iterator<Item = HighlightEvent>,
Spans: Iterator<Item = Span>,
Expand All @@ -37,6 +39,25 @@ where
overlay
}

pub struct RangeToSpan<I: Iterator<Item = Range<usize>>> {
scope: Highlight,
ranges: I,
}

impl<I: Iterator<Item = Range<usize>>> Iterator for RangeToSpan<I> {
type Item = Span;

fn next(&mut self) -> Option<Self::Item> {
self.ranges.next().map(|range| Span {
start: range.start,
end: range.end,
scope: self.scope,
})
}
}

pub type OverlappingOverlay<Events, Ranges> = Overlay<Events, RangeToSpan<Ranges>, true>;

/// Overlays a `scope` highlight onto the `HighlightEvent` stream `events`
/// at the ranges specified in `ranges`.
///
Expand All @@ -49,20 +70,14 @@ pub fn overlapping_overlay<Events, Ranges>(
events: Events,
ranges: Ranges,
scope: Highlight,
) -> Overlay<Events, iter::Map<Ranges, impl FnMut(Range<usize>) -> Span>, true>
) -> OverlappingOverlay<Events, Ranges>
where
Events: Iterator<Item = HighlightEvent>,
Ranges: Iterator<Item = Range<usize>>,
{
let mut overlay = Overlay {
events,
spans: ranges
.map(move |span| Span {
start: span.start,
end: span.end,
scope,
})
.peekable(),
spans: RangeToSpan { scope, ranges }.peekable(),
next_event: None,
current_span: None,
queue: EventQueue::new(),
Expand Down
Loading

0 comments on commit 2c6f681

Please sign in to comment.