ui: Optimize tree-sitter style lookups
Tree sitter returns an index referring to the position of the scope in the scopes array. We can use that same index to avoid a hashmap lookup and instead store the styles in an array. This currently stores the styles in both a map and an array because the UI still uses hashmap lookups, but it's a reasonable tradeoff.
This commit is contained in:
parent
7bbf4c5b06
commit
259678585c
@ -311,8 +311,7 @@ pub fn render_text_highlights<H: Iterator<Item = HighlightEvent>>(
|
||||
if LineEnding::from_rope_slice(&grapheme).is_some() {
|
||||
if !out_of_bounds {
|
||||
let style = spans.iter().fold(text_style, |acc, span| {
|
||||
let style = theme.get(theme.scopes()[span.0].as_str());
|
||||
acc.patch(style)
|
||||
acc.patch(theme.highlight(span.0))
|
||||
});
|
||||
|
||||
// we still want to render an empty cell with the style
|
||||
@ -346,8 +345,7 @@ pub fn render_text_highlights<H: Iterator<Item = HighlightEvent>>(
|
||||
|
||||
if !out_of_bounds {
|
||||
let style = spans.iter().fold(text_style, |acc, span| {
|
||||
let style = theme.get(theme.scopes()[span.0].as_str());
|
||||
acc.patch(style)
|
||||
acc.patch(theme.highlight(span.0))
|
||||
});
|
||||
|
||||
// if we're offscreen just keep going until we hit a new line
|
||||
|
@ -78,8 +78,11 @@ pub fn default(&self) -> Theme {
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Theme {
|
||||
scopes: Vec<String>,
|
||||
// UI styles are stored in a HashMap
|
||||
styles: HashMap<String, Style>,
|
||||
// tree-sitter highlight styles are stored in a Vec to optimize lookups
|
||||
scopes: Vec<String>,
|
||||
highlights: Vec<Style>,
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for Theme {
|
||||
@ -88,6 +91,8 @@ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let mut styles = HashMap::new();
|
||||
let mut scopes = Vec::new();
|
||||
let mut highlights = Vec::new();
|
||||
|
||||
if let Ok(mut colors) = HashMap::<String, Value>::deserialize(deserializer) {
|
||||
// TODO: alert user of parsing failures in editor
|
||||
@ -102,21 +107,35 @@ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
.unwrap_or_default();
|
||||
|
||||
styles.reserve(colors.len());
|
||||
scopes.reserve(colors.len());
|
||||
highlights.reserve(colors.len());
|
||||
|
||||
for (name, style_value) in colors {
|
||||
let mut style = Style::default();
|
||||
if let Err(err) = palette.parse_style(&mut style, style_value) {
|
||||
warn!("{}", err);
|
||||
}
|
||||
styles.insert(name, style);
|
||||
|
||||
// these are used both as UI and as highlights
|
||||
styles.insert(name.clone(), style);
|
||||
scopes.push(name);
|
||||
highlights.push(style);
|
||||
}
|
||||
}
|
||||
|
||||
let scopes = styles.keys().map(ToString::to_string).collect();
|
||||
Ok(Self { scopes, styles })
|
||||
Ok(Self {
|
||||
scopes,
|
||||
styles,
|
||||
highlights,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Theme {
|
||||
pub fn highlight(&self, index: usize) -> Style {
|
||||
self.highlights[index]
|
||||
}
|
||||
|
||||
pub fn get(&self, scope: &str) -> Style {
|
||||
self.try_get(scope)
|
||||
.unwrap_or_else(|| Style::default().fg(Color::Rgb(0, 0, 255)))
|
||||
|
Loading…
Reference in New Issue
Block a user