perf: use a single loader instance for Treesitter

This commit is contained in:
Price Hiller 2024-04-15 16:09:53 -05:00
parent 06ea361a3d
commit 6b3f7676b5
Signed by: Price
GPG Key ID: C3FADDE7A8534BEB

View File

@ -26,12 +26,13 @@ use tree_sitter_highlight::{HighlightConfiguration, HighlightEvent, Highlighter}
use tree_sitter_loader::{Config, LanguageConfiguration, Loader}; use tree_sitter_loader::{Config, LanguageConfiguration, Loader};
pub struct TreesitterSyntaxAdapter { pub struct TreesitterSyntaxAdapter {
parsers_directory: PathBuf, loader: Loader
} }
impl TreesitterSyntaxAdapter { impl TreesitterSyntaxAdapter {
pub fn new(parsers_directory: PathBuf) -> anyhow::Result<Self> { pub fn new(parsers_directory: PathBuf) -> anyhow::Result<Self> {
Ok(TreesitterSyntaxAdapter { parsers_directory }) let loader = TreesitterSyntaxAdapter::get_loader(&parsers_directory)?;
Ok(TreesitterSyntaxAdapter { loader })
} }
pub fn get_loader(parsers_directory: &Path) -> anyhow::Result<Loader> { pub fn get_loader(parsers_directory: &Path) -> anyhow::Result<Loader> {
@ -93,8 +94,6 @@ impl TreesitterSyntaxAdapter {
highlighter.highlight(highlight_config, code.as_bytes(), None, |lang| { highlighter.highlight(highlight_config, code.as_bytes(), None, |lang| {
loader.highlight_config_for_injection_string(lang) loader.highlight_config_for_injection_string(lang)
})?; })?;
// loader.configure_highlights(&highlight_config.names().to_vec());
Ok(highlights) Ok(highlights)
} }
@ -127,11 +126,10 @@ impl SyntaxHighlighterAdapter for TreesitterSyntaxAdapter {
return Ok(()); return Ok(());
}; };
let loader = TreesitterSyntaxAdapter::get_loader(&self.parsers_directory).unwrap();
let mut highlighter = Highlighter::new(); let mut highlighter = Highlighter::new();
if let Ok(highlights) = self.get_highlights(&loader, &mut highlighter, code, scope.as_str()) if let Ok(highlights) = self.get_highlights(&self.loader, &mut highlighter, code, scope.as_str())
{ {
let highlight_names = loader.highlight_names(); let highlight_names = self.loader.highlight_names();
for event in highlights { for event in highlights {
match event.unwrap() { match event.unwrap() {