From 4a9d1163e04ba0fc29f8532afd35381b7df55e48 Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Fri, 14 May 2021 19:21:46 +0900 Subject: Hacky way to specify indent scopes per language via toml configs. Can't do it via a scm query nicely because it returns an iterator over all the matches, whereas we want to traverse the tree ourselves. Can't extract the pattern data from a parsed query either. Oh well, toml files for now. --- helix-core/src/syntax.rs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'helix-core/src/syntax.rs') diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index 06f3399e..1b8e832b 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -4,7 +4,7 @@ pub use helix_syntax::{get_language, get_language_name, Lang}; use std::{ borrow::Cow, cell::RefCell, - collections::HashMap, + collections::{HashMap, HashSet}, path::{Path, PathBuf}, sync::Arc, }; @@ -41,6 +41,9 @@ pub struct LanguageConfiguration { pub language_server: Option, #[serde(skip_serializing_if = "Option::is_none")] pub indent: Option, + + #[serde(skip)] + pub(crate) indent_query: OnceCell>, } #[derive(Serialize, Deserialize)] @@ -59,6 +62,17 @@ pub struct IndentationConfiguration { pub unit: String, } +#[derive(Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub struct IndentQuery { + #[serde(default)] + #[serde(skip_serializing_if = "HashSet::is_empty")] + pub indent: HashSet, + #[serde(default)] + #[serde(skip_serializing_if = "HashSet::is_empty")] + pub outdent: HashSet, +} + fn read_query(language: &str, filename: &str) -> String { static INHERITS_REGEX: Lazy = Lazy::new(|| Regex::new(r";+\s*inherits\s*:?\s*([a-z_,()]+)\s*").unwrap()); @@ -127,6 +141,20 @@ impl LanguageConfiguration { .clone() } + pub fn indent_query(&self) -> Option<&IndentQuery> { + self.indent_query + .get_or_init(|| { + let language = get_language_name(self.language_id).to_ascii_lowercase(); + + let root = crate::runtime_dir(); + let path = root.join("queries").join(language).join("indents.toml"); + + let toml = std::fs::read(&path).ok()?; + toml::from_slice(&toml).ok() + }) + .as_ref() + } + pub fn scope(&self) -> &str { &self.scope } -- cgit v1.2.3-70-g09d2