@@ -2,11 +2,12 @@ use crate::{Change, Rope, RopeSlice, Transaction};
2pub use helix_syntax::Lang;
2pub use helix_syntax::Lang;
3pub use helix_syntax::{get_language, get_language_name};
3pub use helix_syntax::{get_language, get_language_name};
5+use std::cell::RefCell;
5use std::collections::HashMap;
6use std::collections::HashMap;
6use std::path::{Path, PathBuf};
7use std::path::{Path, PathBuf};
7use std::sync::Arc;
8use std::sync::Arc;
9−use once_cell::sync::OnceCell;
10+use once_cell::sync::{Lazy, OnceCell};
11// largely based on tree-sitter/cli/src/loader.rs
12// largely based on tree-sitter/cli/src/loader.rs
12pub struct LanguageConfiguration {
13pub struct LanguageConfiguration {
@@ -65,8 +66,6 @@ impl LanguageConfiguration {
68−use once_cell::sync::Lazy;
70pub static LOADER: Lazy<Loader> = Lazy::new(Loader::init);
69pub static LOADER: Lazy<Loader> = Lazy::new(Loader::init);
72pub struct Loader {
71pub struct Loader {
@@ -145,13 +144,20 @@ impl Loader {
150−pub struct Syntax {
147+pub struct TSParser {
151− // grammar: Grammar,
152− parser: Parser,
148+ parser: tree_sitter::Parser,
153 cursors: Vec<QueryCursor>,
149 cursors: Vec<QueryCursor>,
152+// could also just use a pool, or a single instance?
154+ pub static PARSER: RefCell<TSParser> = RefCell::new(TSParser {
155+ parser: Parser::new(),
156+ cursors: Vec::new(),
155 config: Arc<HighlightConfiguration>,
161 config: Arc<HighlightConfiguration>,
157 pub(crate) root_layer: LanguageLayer,
163 pub(crate) root_layer: LanguageLayer,
@@ -163,10 +169,6 @@ impl Syntax {
163 /*language: Lang,*/ source: &Rope,
169 /*language: Lang,*/ source: &Rope,
164 config: Arc<HighlightConfiguration>,
170 config: Arc<HighlightConfiguration>,
165 ) -> Self {
171 ) -> Self {
166− // fetch grammar for parser based on language string
167− // let grammar = get_language(&language);
168− let parser = Parser::new();
170 let root_layer = LanguageLayer { tree: None };
172 let root_layer = LanguageLayer { tree: None };
172 // track markers of injections
174 // track markers of injections
@@ -174,25 +176,25 @@ impl Syntax {
175 let mut syntax = Self {
177 let mut syntax = Self {
176 // grammar,
178 // grammar,
178− cursors: Vec::new(),
180 root_layer,
180 root_layer,
183 // update root layer
183 // update root layer
184+ PARSER.with(|ts_parser| {
184− syntax.root_layer.parse(
185+ syntax.root_layer.parse(
185− &mut syntax.parser,
186+ &mut ts_parser.borrow_mut(),
186− &syntax.config,
187+ &syntax.config,
189− vec![Range {
190+ vec![Range {
190− start_byte: 0,
191+ start_byte: 0,
191− end_byte: usize::MAX,
192+ end_byte: usize::MAX,
192− start_point: Point::new(0, 0),
193+ start_point: Point::new(0, 0),
193− end_point: Point::new(usize::MAX, usize::MAX),
194+ end_point: Point::new(usize::MAX, usize::MAX),
@@ -202,13 +204,15 @@ impl Syntax {
202 source: &Rope,
204 source: &Rope,
203 changeset: &ChangeSet,
205 changeset: &ChangeSet,
204 ) -> Result<(), Error> {
206 ) -> Result<(), Error> {
207+ PARSER.with(|ts_parser| {
205− self.root_layer.update(
208+ self.root_layer.update(
206− &mut self.parser,
209+ &mut ts_parser.borrow_mut(),
207− &self.config,
210+ &self.config,
208− old_source,
211+ old_source,
210− changeset,
213+ changeset,
213 // TODO: deal with injections and update them too
217 // TODO: deal with injections and update them too
@@ -229,7 +233,8 @@ impl Syntax {
230 /// Iterate over the highlighted regions for a given slice of source code.
234 /// Iterate over the highlighted regions for a given slice of source code.
231 pub fn highlight_iter<'a>(
235 pub fn highlight_iter<'a>(
232− &'a mut self,
236+ &self,
237+ ts_parser: &'a mut TSParser,
233 source: &'a [u8],
238 source: &'a [u8],
234 range: Option<std::ops::Range<usize>>,
239 range: Option<std::ops::Range<usize>>,
235 cancellation_flag: Option<&'a AtomicUsize>,
240 cancellation_flag: Option<&'a AtomicUsize>,
@@ -283,7 +288,7 @@ impl Syntax {
283 byte_offset: range.map(|r| r.start).unwrap_or(0), // TODO: simplify
288 byte_offset: range.map(|r| r.start).unwrap_or(0), // TODO: simplify
284 injection_callback,
289 injection_callback,
285 cancellation_flag,
290 cancellation_flag,
286− highlighter: self,
291+ highlighter: ts_parser,
287 iter_count: 0,
292 iter_count: 0,
288 layers: vec![layer],
293 layers: vec![layer],
289 next_event: None,
294 next_event: None,
@@ -336,19 +341,21 @@ impl LanguageLayer {
337 fn parse(
342 fn parse(
338 &mut self,
343 &mut self,
339− parser: &mut Parser,
344+ ts_parser: &mut TSParser,
340 config: &HighlightConfiguration,
345 config: &HighlightConfiguration,
341 source: &Rope,
346 source: &Rope,
342 mut depth: usize,
347 mut depth: usize,
343 mut ranges: Vec<Range>,
348 mut ranges: Vec<Range>,
344 ) -> Result<(), Error> {
349 ) -> Result<(), Error> {
345− if parser.set_included_ranges(&ranges).is_ok() {
350+ if ts_parser.parser.set_included_ranges(&ranges).is_ok() {
347 .set_language(config.language)
353 .set_language(config.language)
348 .map_err(|_| Error::InvalidLanguage)?;
354 .map_err(|_| Error::InvalidLanguage)?;
350 // unsafe { syntax.parser.set_cancellation_flag(cancellation_flag) };
356 // unsafe { syntax.parser.set_cancellation_flag(cancellation_flag) };
357+ let tree = ts_parser
351− let tree = parser
358+ .parser
352 .parse_with(
359 .parse_with(
353 &mut |byte, _| {
360 &mut |byte, _| {
354 if byte <= source.len_bytes() {
361 if byte <= source.len_bytes() {
@@ -517,7 +524,7 @@ impl LanguageLayer {
518 fn update(
525 fn update(
519 &mut self,
526 &mut self,
520− parser: &mut Parser,
527+ ts_parser: &mut TSParser,
521 config: &HighlightConfiguration,
528 config: &HighlightConfiguration,
522 old_source: &Rope,
529 old_source: &Rope,
523 source: &Rope,
530 source: &Rope,
@@ -535,7 +542,7 @@ impl LanguageLayer {
537 self.parse(
544 self.parse(
538− parser,
545+ ts_parser,
@@ -652,7 +659,7 @@ where
653 source: &'a [u8],
660 source: &'a [u8],
654 byte_offset: usize,
661 byte_offset: usize,
655− highlighter: &'a mut Syntax,
662+ highlighter: &'a mut TSParser,
656 injection_callback: F,
663 injection_callback: F,
657 cancellation_flag: Option<&'a AtomicUsize>,
664 cancellation_flag: Option<&'a AtomicUsize>,
658 layers: Vec<HighlightIterLayer<'a>>,
665 layers: Vec<HighlightIterLayer<'a>>,
@@ -839,7 +846,7 @@ impl<'a> HighlightIterLayer<'a> {
839 /// added to the returned vector.
846 /// added to the returned vector.
840 fn new<F: FnMut(&str) -> Option<&'a HighlightConfiguration> + 'a>(
847 fn new<F: FnMut(&str) -> Option<&'a HighlightConfiguration> + 'a>(
841 source: &'a [u8],
848 source: &'a [u8],
842− highlighter: &mut Syntax,
849+ highlighter: &mut TSParser,
843 cancellation_flag: Option<&'a AtomicUsize>,
850 cancellation_flag: Option<&'a AtomicUsize>,
844 injection_callback: &mut F,
851 injection_callback: &mut F,
845 mut config: &'a HighlightConfiguration,
852 mut config: &'a HighlightConfiguration,