summaryrefslogtreecommitdiff
path: root/helix-view/src
diff options
context:
space:
mode:
authorBlaž Hrastnik2021-12-01 04:08:20 +0000
committerBlaž Hrastnik2021-12-01 04:08:20 +0000
commit259678585c3410044683bf4d2619b2d024a04514 (patch)
tree207a827dded75a761bdca62d1784f1fd3e21676b /helix-view/src
parent7bbf4c5b0675bb794bd88c070472773bd7d7e6bf (diff)
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.
Diffstat (limited to 'helix-view/src')
-rw-r--r--helix-view/src/theme.rs27
1 files changed, 23 insertions, 4 deletions
diff --git a/helix-view/src/theme.rs b/helix-view/src/theme.rs
index 757316bd..2366ff7d 100644
--- a/helix-view/src/theme.rs
+++ b/helix-view/src/theme.rs
@@ -78,8 +78,11 @@ impl Loader {
#[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 @@ impl<'de> Deserialize<'de> for Theme {
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 @@ impl<'de> Deserialize<'de> for Theme {
.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)))