use serde::Deserialize; use std::collections::HashMap; use toml; use tree_sitter::Node; #[derive(Deserialize)] pub struct Config { #[serde(default)] filetype: HashMap, } #[derive(Clone, Deserialize, Default)] pub struct FiletypeConfig { blacklist: Option>, whitelist: Option>, #[serde(default)] group: HashMap>, } impl Default for Config { fn default() -> Self { let mut config = Config { filetype: HashMap::default(), }; config .filetype .insert("default".to_owned(), FiletypeConfig::default()); config } } impl Config { pub fn load>(path: P) -> Option { let config = std::fs::read_to_string(&path).map_err(|e| { eprintln!("Could not open config {}", path.as_ref().display()); e}).ok()?; let mut config: Config = toml::from_str(&config).map_err(|e| { eprintln!("Could not read config: {e}"); e}).ok()?; if config.filetype.get("default").is_none() { config .filetype .insert("default".to_owned(), FiletypeConfig::default()); } Some(config) } pub fn get_filetype_config<'a>(&'a self, filetype: &str) -> &'a FiletypeConfig { self.filetype .get(filetype) .or_else(|| self.filetype.get("default")) .unwrap() } } impl FiletypeConfig { pub fn is_node_visible(&self, node: Node) -> bool { let kind = node.kind(); if !node.is_named() { return false; } match &self.whitelist { Some(whitelist) => whitelist.iter().any(|x| x == kind), None => match &self.blacklist { Some(blacklist) => !blacklist.iter().any(|x| x == kind), None => true, }, } } pub fn resolve_alias<'a>(&'a self, kind: &str) -> Vec { self.group .get(kind) .cloned() .unwrap_or_else(|| vec![kind.to_string()]) } }