From 6066c8392cd0e021c8d5b811ced8f3a084ac8f17 Mon Sep 17 00:00:00 2001 From: Muhammad Date: Mon, 30 Oct 2023 00:38:52 +0000 Subject: [PATCH] Add default value for `Config::indent`. --- helix-core/src/syntax.rs | 38 ++++++++++++++++++++++++++++++++------ helix-view/src/document.rs | 23 ++++++++++------------- helix-view/src/editor.rs | 5 ++--- 3 files changed, 44 insertions(+), 22 deletions(-) diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index 2a789fbd9553b..4d385ce484ec6 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -63,6 +63,21 @@ where }) } +fn deserialize_tab_width_option<'de, D>(deserializer: D) -> Result, D::Error> +where + D: serde::Deserializer<'de>, +{ + usize::deserialize(deserializer).and_then(|n| { + if n > 0 && n <= 16 { + Ok(Some(n)) + } else { + Err(serde::de::Error::custom( + "tab width must be a value from 1 to 16 inclusive", + )) + } + }) +} + pub fn deserialize_auto_pairs<'de, D>(deserializer: D) -> Result, D::Error> where D: serde::Deserializer<'de>, @@ -436,18 +451,20 @@ pub struct DebuggerQuirks { pub absolute_paths: bool, } -#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "kebab-case")] pub struct IndentationConfiguration { - #[serde(deserialize_with = "deserialize_tab_width")] - pub tab_width: usize, - pub unit: String, + #[serde(deserialize_with = "deserialize_tab_width_option")] + pub tab_width: Option, + pub unit: Option, } #[derive(Debug, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] pub struct LanguageIndentationConfiguration { - #[serde(flatten)] - pub indent: IndentationConfiguration, + #[serde(deserialize_with = "deserialize_tab_width")] + pub tab_width: usize, + pub unit: String, #[serde(default)] pub required: bool, } @@ -495,6 +512,15 @@ impl FromStr for AutoPairConfig { } } +impl Default for IndentationConfiguration { + fn default() -> Self { + IndentationConfiguration { + tab_width: None, + unit: None, + } + } +} + #[derive(Debug)] pub struct TextObjectQuery { pub query: Query, diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index 2ea1e48f31a2f..6a49cc1543889 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -960,7 +960,7 @@ impl Document { /// is likewise auto-detected, and will remain unchanged if no line endings were detected. pub fn detect_indent_and_line_ending(&mut self) { self.indent_style = auto_detect_indent_style(&self.text).unwrap_or_else(|| { - self.indent_config(DEFAULT_INDENT, |config| IndentStyle::from_str(&config.unit)) + self.indent_config(DEFAULT_INDENT, |config| config.unit.as_ref().map(|unit| IndentStyle::from_str(unit))) }); if let Some(line_ending) = auto_detect_line_ending(&self.text) { self.line_ending = line_ending; @@ -999,18 +999,15 @@ impl Document { }; } - fn indent_config T>(&self, default: T, mapper: F) -> T { - self.language_config() - .and_then(|config| config.indent.as_ref()) - .filter(|config| config.required) - .map(|config| mapper(&config.indent)) - .or_else(|| self.config.load().indent.as_ref().map(&mapper)) - .or_else(|| { - self.language_config() - .and_then(|config| config.indent.as_ref()) - .map(|config| mapper(&config.indent)) - }) - .unwrap_or(default) + fn indent_config Option>(&self, default: T, mapper: F) -> T { + let mut indent = self.config.load().indent.clone(); + + if let Some(c) = self.language_config().and_then(|config| config.indent.as_ref()) { + indent.tab_width = indent.tab_width.filter(|_| !c.required).or(Some(c.tab_width)); + indent.unit = indent.unit.filter(|_| !c.required).or(Some(c.unit.clone())); + } + + mapper(indent).unwrap_or(default) } /// Reload the document from its path. diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index ffcb684731b96..fdf7ff92ae40d 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -276,8 +276,7 @@ pub struct Config { pub rulers: Vec, #[serde(default)] pub whitespace: WhitespaceConfig, - #[serde(skip_serializing_if = "Option::is_none")] - pub indent: Option, + pub indent: IndentationConfiguration, /// Persistently display open buffers along the top pub bufferline: BufferLine, /// Vertical indent width guides. @@ -835,7 +834,7 @@ impl Default for Config { terminal: get_terminal_provider(), rulers: Vec::new(), whitespace: WhitespaceConfig::default(), - indent: None, + indent: IndentationConfiguration::default(), bufferline: BufferLine::default(), indent_guides: IndentGuidesConfig::default(), color_modes: false,