From 4c789cfbda5cd0e6a6319fd3dafff8020529b762 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Thu, 25 Aug 2022 12:54:31 -0500 Subject: 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. --- helix-core/src/syntax.rs | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) (limited to 'helix-core/src') 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 { - 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 { + 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? -- cgit v1.2.3-70-g09d2