Convert query capture indices to style ids

* Introduce a Theme struct as a new part of the app's settings
* Store on each Language a ThemeMap, which converts the capture ids
  from that language's highlight query into StyleIds, which identify
  styles in the current Theme.
* Update `highlighted_chunks` methods to provide StyleIds instead of
  capture ids.
This commit is contained in:
Max Brunsfeld
2021-05-24 16:44:14 -07:00
parent bc8d2b2f1d
commit 8340958b33
11 changed files with 389 additions and 26 deletions
+22
View File
@@ -1,3 +1,5 @@
use crate::settings::{Theme, ThemeMap};
use parking_lot::Mutex;
use rust_embed::RustEmbed;
use std::{path::Path, sync::Arc};
use tree_sitter::{Language as Grammar, Query};
@@ -13,12 +15,23 @@ pub struct Language {
pub grammar: Grammar,
pub highlight_query: Query,
path_suffixes: Vec<String>,
theme_mapping: Mutex<ThemeMap>,
}
pub struct LanguageRegistry {
languages: Vec<Arc<Language>>,
}
impl Language {
pub fn theme_mapping(&self) -> ThemeMap {
self.theme_mapping.lock().clone()
}
fn set_theme(&self, theme: &Theme) {
*self.theme_mapping.lock() = ThemeMap::new(self.highlight_query.capture_names(), theme);
}
}
impl LanguageRegistry {
pub fn new() -> Self {
let grammar = tree_sitter_rust::language();
@@ -32,6 +45,7 @@ impl LanguageRegistry {
)
.unwrap(),
path_suffixes: vec!["rs".to_string()],
theme_mapping: Mutex::new(ThemeMap::default()),
};
Self {
@@ -39,6 +53,12 @@ impl LanguageRegistry {
}
}
pub fn set_theme(&self, theme: &Theme) {
for language in &self.languages {
language.set_theme(theme);
}
}
pub fn select_language(&self, path: impl AsRef<Path>) -> Option<&Arc<Language>> {
let path = path.as_ref();
let filename = path.file_name().and_then(|name| name.to_str());
@@ -67,12 +87,14 @@ mod tests {
grammar,
highlight_query: Query::new(grammar, "").unwrap(),
path_suffixes: vec!["rs".to_string()],
theme_mapping: Default::default(),
}),
Arc::new(Language {
name: "Make".to_string(),
grammar,
highlight_query: Query::new(grammar, "").unwrap(),
path_suffixes: vec!["Makefile".to_string(), "mk".to_string()],
theme_mapping: Default::default(),
}),
],
};