diff options
author | Pascal Kuthe | 2023-05-12 14:49:39 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2023-05-18 06:23:37 +0000 |
commit | b0705337bec836604bdb97689d0a44940c6bddae (patch) | |
tree | 4f7e3607cb891f290aae853f938d1db17a1e3c95 /helix-core | |
parent | 2f2306475cac7ee9385b816424137421c13bf4c2 (diff) |
automatically disable TS when parsing takes longer than 500ms
Diffstat (limited to 'helix-core')
-rw-r--r-- | helix-core/src/syntax.rs | 23 | ||||
-rw-r--r-- | helix-core/tests/indent.rs | 2 |
2 files changed, 16 insertions, 9 deletions
diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index 005e985d..f36c985e 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -768,7 +768,11 @@ fn byte_range_to_str(range: std::ops::Range<usize>, source: RopeSlice) -> Cow<st } impl Syntax { - pub fn new(source: &Rope, config: Arc<HighlightConfiguration>, loader: Arc<Loader>) -> Self { + pub fn new( + source: &Rope, + config: Arc<HighlightConfiguration>, + loader: Arc<Loader>, + ) -> Option<Self> { let root_layer = LanguageLayer { tree: None, config, @@ -793,11 +797,13 @@ impl Syntax { loader, }; - syntax - .update(source, source, &ChangeSet::new(source)) - .unwrap(); + let res = syntax.update(source, source, &ChangeSet::new(source)); - syntax + if res.is_err() { + log::error!("TS parser failed, disabeling TS for the current buffer: {res:?}"); + return None; + } + Some(syntax) } pub fn update( @@ -925,6 +931,7 @@ impl Syntax { PARSER.with(|ts_parser| { let ts_parser = &mut ts_parser.borrow_mut(); + ts_parser.parser.set_timeout_micros(1000 * 500); // half a second is pretty generours let mut cursor = ts_parser.cursors.pop().unwrap_or_else(QueryCursor::new); // TODO: might need to set cursor range cursor.set_byte_range(0..usize::MAX); @@ -2371,7 +2378,7 @@ mod test { let mut cursor = QueryCursor::new(); let config = HighlightConfiguration::new(language, "", "", "").unwrap(); - let syntax = Syntax::new(&source, Arc::new(config), Arc::new(loader)); + let syntax = Syntax::new(&source, Arc::new(config), Arc::new(loader)).unwrap(); let root = syntax.tree().root_node(); let mut test = |capture, range| { @@ -2442,7 +2449,7 @@ mod test { fn main() {} ", ); - let syntax = Syntax::new(&source, Arc::new(config), Arc::new(loader)); + let syntax = Syntax::new(&source, Arc::new(config), Arc::new(loader)).unwrap(); let tree = syntax.tree(); let root = tree.root_node(); assert_eq!(root.kind(), "source_file"); @@ -2529,7 +2536,7 @@ mod test { let language = get_language(language_name).unwrap(); let config = HighlightConfiguration::new(language, "", "", "").unwrap(); - let syntax = Syntax::new(&source, Arc::new(config), Arc::new(loader)); + let syntax = Syntax::new(&source, Arc::new(config), Arc::new(loader)).unwrap(); let root = syntax .tree() diff --git a/helix-core/tests/indent.rs b/helix-core/tests/indent.rs index f558f86f..409706bb 100644 --- a/helix-core/tests/indent.rs +++ b/helix-core/tests/indent.rs @@ -72,7 +72,7 @@ fn test_treesitter_indent(file_name: &str, lang_scope: &str) { let language_config = loader.language_config_for_scope(lang_scope).unwrap(); let highlight_config = language_config.highlight_config(&[]).unwrap(); - let syntax = Syntax::new(&doc, highlight_config, std::sync::Arc::new(loader)); + let syntax = Syntax::new(&doc, highlight_config, std::sync::Arc::new(loader)).unwrap(); let indent_query = language_config.indent_query().unwrap(); let text = doc.slice(..); |