aboutsummaryrefslogtreecommitdiff
path: root/helix-core/src/syntax.rs
diff options
context:
space:
mode:
authorSkyler Hawthorne2022-02-25 08:36:54 +0000
committerGitHub2022-02-25 08:36:54 +0000
commita494f47a5df543a3ab8d6530a5acbc2a5bd04d44 (patch)
tree084ef1b24c593d58d6616b37e073073d93009ff3 /helix-core/src/syntax.rs
parentb935fac9576cf333e22b82e40da8c4d73c8e547d (diff)
Configurable auto pairs (#1624)
* impl auto pairs config Implements configuration for which pairs of tokens get auto completed. In order to help with this, the logic for when *not* to auto complete has been generalized from a specific hardcoded list of characters to simply testing if the next/prev char is alphanumeric. It is possible to configure a global list of pairs as well as at the language level. The language config will take precedence over the global config. * rename AutoPair -> Pair * clean up insert_char command * remove Rc * remove some explicit cloning with another impl * fix lint * review comments * global auto-pairs = false takes precedence over language settings * make clippy happy * print out editor config on startup * move auto pairs accessor into Document * rearrange auto pair doc comment * use pattern in Froms
Diffstat (limited to 'helix-core/src/syntax.rs')
-rw-r--r--helix-core/src/syntax.rs66
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 {