diff options
Diffstat (limited to 'helix-core/src/syntax.rs')
-rw-r--r-- | helix-core/src/syntax.rs | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index ccf91100..ca06e2dd 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -1,4 +1,5 @@ use crate::{ + auto_pairs::AutoPairs, chars::char_is_line_ending, diagnostic::Severity, regex::Regex, @@ -17,6 +18,7 @@ use std::{ collections::{HashMap, HashSet, VecDeque}, fmt, path::Path, + str::FromStr, sync::Arc, }; @@ -41,6 +43,13 @@ where .transpose() } +pub fn deserialize_auto_pairs<'de, D>(deserializer: D) -> Result<Option<AutoPairs>, D::Error> +where + D: serde::Deserializer<'de>, +{ + Ok(Option::<AutoPairConfig>::deserialize(deserializer)?.and_then(AutoPairConfig::into)) +} + #[derive(Debug, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct Configuration { @@ -89,6 +98,13 @@ pub struct LanguageConfiguration { pub(crate) textobject_query: OnceCell<Option<TextObjectQuery>>, #[serde(skip_serializing_if = "Option::is_none")] pub debugger: Option<DebugAdapterConfig>, + + /// Automatic insertion of pairs to parentheses, brackets, + /// etc. Defaults to true. Optionally, this can be a list of 2-tuples + /// to specify a list of characters to pair. This overrides the + /// global setting. + #[serde(default, skip_serializing, deserialize_with = "deserialize_auto_pairs")] + pub auto_pairs: Option<AutoPairs>, } #[derive(Debug, Serialize, Deserialize)] @@ -162,6 +178,56 @@ pub struct IndentationConfiguration { pub unit: String, } +/// Configuration for auto pairs +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case", deny_unknown_fields, untagged)] +pub enum AutoPairConfig { + /// Enables or disables auto pairing. False means disabled. True means to use the default pairs. + Enable(bool), + + /// The mappings of pairs. + Pairs(HashMap<char, char>), +} + +impl Default for AutoPairConfig { + fn default() -> Self { + AutoPairConfig::Enable(true) + } +} + +impl From<&AutoPairConfig> for Option<AutoPairs> { + fn from(auto_pair_config: &AutoPairConfig) -> Self { + match auto_pair_config { + AutoPairConfig::Enable(false) => None, + AutoPairConfig::Enable(true) => Some(AutoPairs::default()), + AutoPairConfig::Pairs(pairs) => Some(AutoPairs::new(pairs.iter())), + } + } +} + +impl From<AutoPairConfig> for Option<AutoPairs> { + fn from(auto_pairs_config: AutoPairConfig) -> Self { + (&auto_pairs_config).into() + } +} + +impl FromStr for AutoPairConfig { + type Err = std::str::ParseBoolError; + + // only do bool parsing for runtime setting + fn from_str(s: &str) -> Result<Self, Self::Err> { + let enable: bool = s.parse()?; + + let enable = if enable { + AutoPairConfig::Enable(true) + } else { + AutoPairConfig::Enable(false) + }; + + Ok(enable) + } +} + #[derive(Debug, Serialize, Deserialize)] #[serde(rename_all = "kebab-case")] pub struct IndentQuery { |