reovim_driver_syntax/cache.rs
1//! Syntax cache trait.
2//!
3//! This module defines the [`SyntaxCache`] trait for caching highlight results.
4
5use std::ops::Range;
6
7use crate::highlight::Annotation;
8
9/// Cache for annotation results.
10///
11/// Implementations can provide various caching strategies for
12/// annotation data to improve performance.
13///
14/// # Thread Safety
15///
16/// Implementations must be `Send + Sync` to allow use across threads.
17///
18/// # Example
19///
20/// ```ignore
21/// // Insert annotations for a range
22/// cache.insert(0..100, annotations);
23///
24/// // Later, retrieve cached annotations
25/// if let Some(cached) = cache.get(0..100) {
26/// // Use cached annotations
27/// }
28///
29/// // After an edit, invalidate affected ranges
30/// cache.invalidate_range(50..75);
31/// ```
32pub trait SyntaxCache: Send + Sync {
33 /// Get cached annotations for a byte range.
34 ///
35 /// Returns `None` if the range is not cached or cache is stale.
36 ///
37 /// # Arguments
38 ///
39 /// * `byte_range` - The byte range to look up
40 fn get(&self, byte_range: Range<usize>) -> Option<Vec<Annotation>>;
41
42 /// Insert annotations into the cache.
43 ///
44 /// # Arguments
45 ///
46 /// * `byte_range` - The byte range these annotations cover
47 /// * `highlights` - The annotations to cache
48 fn insert(&mut self, byte_range: Range<usize>, highlights: Vec<Annotation>);
49
50 /// Invalidate cache entries that overlap with a range.
51 ///
52 /// Called after edits to mark affected regions as stale.
53 ///
54 /// # Arguments
55 ///
56 /// * `byte_range` - The byte range that was modified
57 fn invalidate_range(&mut self, byte_range: Range<usize>);
58
59 /// Clear all cached data.
60 fn clear(&mut self);
61
62 /// Check if the cache is empty.
63 fn is_empty(&self) -> bool;
64}
65
66#[cfg(test)]
67#[path = "cache_tests.rs"]
68mod tests;