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.
pull/3612/head
Michael Davis 2 years ago committed by Blaž Hrastnik
parent 12ddd03d3b
commit 4c789cfbda

@ -399,28 +399,15 @@ impl LanguageConfiguration {
pub fn indent_query(&self) -> Option<&Query> { pub fn indent_query(&self) -> Option<&Query> {
self.indent_query self.indent_query
.get_or_init(|| { .get_or_init(|| self.load_query("indents.scm"))
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()
})
.as_ref() .as_ref()
} }
pub fn textobject_query(&self) -> Option<&TextObjectQuery> { pub fn textobject_query(&self) -> Option<&TextObjectQuery> {
self.textobject_query self.textobject_query
.get_or_init(|| -> Option<TextObjectQuery> { .get_or_init(|| {
let lang_name = self.language_id.to_ascii_lowercase(); self.load_query("textobjects.scm")
let query_text = read_query(&lang_name, "textobjects.scm"); .map(|query| TextObjectQuery { query })
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 })
}) })
.as_ref() .as_ref()
} }
@ -428,6 +415,18 @@ impl LanguageConfiguration {
pub fn scope(&self) -> &str { pub fn scope(&self) -> &str {
&self.scope &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? // Expose loader as Lazy<> global since it's always static?

Loading…
Cancel
Save