diff options
author | Michael Davis | 2022-08-25 17:54:31 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2022-08-31 08:45:23 +0000 |
commit | 4c789cfbda5cd0e6a6319fd3dafff8020529b762 (patch) | |
tree | 599c3259fb64524518e92e6eca784574d0b77579 /helix-core/src/syntax.rs | |
parent | 12ddd03d3b9f6be61008c6a57f3dda1ef1eee131 (diff) |
tree-sitter: Refactor lazy query loading
The code for loading queries can be shared between indent and
textobjects queries. In both cases we want to kick an error
message out to the logs.
Diffstat (limited to 'helix-core/src/syntax.rs')
-rw-r--r-- | helix-core/src/syntax.rs | 33 |
1 files changed, 16 insertions, 17 deletions
diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index 99922d37..7ce0f8d2 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -399,28 +399,15 @@ impl LanguageConfiguration { pub fn indent_query(&self) -> Option<&Query> { self.indent_query - .get_or_init(|| { - let lang_name = self.language_id.to_ascii_lowercase(); - let query_text = read_query(&lang_name, "indents.scm"); - if query_text.is_empty() { - return None; - } - let lang = self.highlight_config.get()?.as_ref()?.language; - Query::new(lang, &query_text).ok() - }) + .get_or_init(|| self.load_query("indents.scm")) .as_ref() } pub fn textobject_query(&self) -> Option<&TextObjectQuery> { self.textobject_query - .get_or_init(|| -> Option<TextObjectQuery> { - let lang_name = self.language_id.to_ascii_lowercase(); - let query_text = read_query(&lang_name, "textobjects.scm"); - let lang = self.highlight_config.get()?.as_ref()?.language; - let query = Query::new(lang, &query_text) - .map_err(|e| log::error!("Failed to parse textobjects.scm queries: {}", e)) - .ok()?; - Some(TextObjectQuery { query }) + .get_or_init(|| { + self.load_query("textobjects.scm") + .map(|query| TextObjectQuery { query }) }) .as_ref() } @@ -428,6 +415,18 @@ impl LanguageConfiguration { pub fn scope(&self) -> &str { &self.scope } + + fn load_query(&self, kind: &str) -> Option<Query> { + let lang_name = self.language_id.to_ascii_lowercase(); + let query_text = read_query(&lang_name, kind); + if query_text.is_empty() { + return None; + } + let lang = self.highlight_config.get()?.as_ref()?.language; + Query::new(lang, &query_text) + .map_err(|e| log::error!("Failed to parse {} queries for {}: {}", kind, lang_name, e)) + .ok() + } } // Expose loader as Lazy<> global since it's always static? |