perf: use a single loader instance for Treesitter
This commit is contained in:
parent
06ea361a3d
commit
6b3f7676b5
@ -26,12 +26,13 @@ use tree_sitter_highlight::{HighlightConfiguration, HighlightEvent, Highlighter}
|
||||
use tree_sitter_loader::{Config, LanguageConfiguration, Loader};
|
||||
|
||||
pub struct TreesitterSyntaxAdapter {
|
||||
parsers_directory: PathBuf,
|
||||
loader: Loader
|
||||
}
|
||||
|
||||
impl TreesitterSyntaxAdapter {
|
||||
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> {
|
||||
@ -93,8 +94,6 @@ impl TreesitterSyntaxAdapter {
|
||||
highlighter.highlight(highlight_config, code.as_bytes(), None, |lang| {
|
||||
loader.highlight_config_for_injection_string(lang)
|
||||
})?;
|
||||
// loader.configure_highlights(&highlight_config.names().to_vec());
|
||||
|
||||
Ok(highlights)
|
||||
}
|
||||
|
||||
@ -127,11 +126,10 @@ impl SyntaxHighlighterAdapter for TreesitterSyntaxAdapter {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
let loader = TreesitterSyntaxAdapter::get_loader(&self.parsers_directory).unwrap();
|
||||
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 {
|
||||
match event.unwrap() {
|
||||
|
Loading…
Reference in New Issue
Block a user