1
0
Fork 0
mirror of https://github.com/helix-editor/helix synced 2024-06-10 22:56:10 +02:00

Add default value for `Config::indent`.

This commit is contained in:
Muhammad 2023-10-30 00:38:52 +00:00
parent 8f35a50d02
commit c4d1d0908c
No known key found for this signature in database
GPG Key ID: 6667F8D7694B5CCF
3 changed files with 44 additions and 22 deletions

View File

@ -69,6 +69,21 @@ fn deserialize_tab_width<'de, D>(deserializer: D) -> Result<usize, D::Error>
}) })
} }
fn deserialize_tab_width_option<'de, D>(deserializer: D) -> Result<Option<usize>, 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<Option<AutoPairs>, D::Error> pub fn deserialize_auto_pairs<'de, D>(deserializer: D) -> Result<Option<AutoPairs>, D::Error>
where where
D: serde::Deserializer<'de>, D: serde::Deserializer<'de>,
@ -536,18 +551,20 @@ pub struct DebuggerQuirks {
pub absolute_paths: bool, pub absolute_paths: bool,
} }
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")] #[serde(rename_all = "kebab-case")]
pub struct IndentationConfiguration { pub struct IndentationConfiguration {
#[serde(deserialize_with = "deserialize_tab_width")] #[serde(deserialize_with = "deserialize_tab_width_option")]
pub tab_width: usize, pub tab_width: Option<usize>,
pub unit: String, pub unit: Option<String>,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct LanguageIndentationConfiguration { pub struct LanguageIndentationConfiguration {
#[serde(flatten)] #[serde(deserialize_with = "deserialize_tab_width")]
pub indent: IndentationConfiguration, pub tab_width: usize,
pub unit: String,
#[serde(default)] #[serde(default)]
pub required: bool, pub required: bool,
} }
@ -611,6 +628,15 @@ fn from_str(s: &str) -> Result<Self, Self::Err> {
} }
} }
impl Default for IndentationConfiguration {
fn default() -> Self {
IndentationConfiguration {
tab_width: None,
unit: None,
}
}
}
#[derive(Debug)] #[derive(Debug)]
pub struct TextObjectQuery { pub struct TextObjectQuery {
pub query: Query, pub query: Query,

View File

@ -957,7 +957,7 @@ pub fn detect_language_config(
/// is likewise auto-detected, and will remain unchanged if no line endings were detected. /// is likewise auto-detected, and will remain unchanged if no line endings were detected.
pub fn detect_indent_and_line_ending(&mut self) { pub fn detect_indent_and_line_ending(&mut self) {
self.indent_style = auto_detect_indent_style(&self.text).unwrap_or_else(|| { 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) { if let Some(line_ending) = auto_detect_line_ending(&self.text) {
self.line_ending = line_ending; self.line_ending = line_ending;
@ -996,18 +996,15 @@ pub fn detect_readonly(&mut self) {
}; };
} }
fn indent_config<T, F: Fn(&IndentationConfiguration) -> T>(&self, default: T, mapper: F) -> T { fn indent_config<T, F: Fn(IndentationConfiguration) -> Option<T>>(&self, default: T, mapper: F) -> T {
self.language_config() let mut indent = self.config.load().indent.clone();
.and_then(|config| config.indent.as_ref())
.filter(|config| config.required) if let Some(c) = self.language_config().and_then(|config| config.indent.as_ref()) {
.map(|config| mapper(&config.indent)) indent.tab_width = indent.tab_width.filter(|_| !c.required).or(Some(c.tab_width));
.or_else(|| self.config.load().indent.as_ref().map(&mapper)) indent.unit = indent.unit.filter(|_| !c.required).or(Some(c.unit.clone()));
.or_else(|| { }
self.language_config()
.and_then(|config| config.indent.as_ref()) mapper(indent).unwrap_or(default)
.map(|config| mapper(&config.indent))
})
.unwrap_or(default)
} }
/// Reload the document from its path. /// Reload the document from its path.

View File

@ -302,8 +302,7 @@ pub struct Config {
pub rulers: Vec<u16>, pub rulers: Vec<u16>,
#[serde(default)] #[serde(default)]
pub whitespace: WhitespaceConfig, pub whitespace: WhitespaceConfig,
#[serde(skip_serializing_if = "Option::is_none")] pub indent: IndentationConfiguration,
pub indent: Option<IndentationConfiguration>,
/// Persistently display open buffers along the top /// Persistently display open buffers along the top
pub bufferline: BufferLine, pub bufferline: BufferLine,
/// Vertical indent width guides. /// Vertical indent width guides.
@ -877,7 +876,7 @@ fn default() -> Self {
terminal: get_terminal_provider(), terminal: get_terminal_provider(),
rulers: Vec::new(), rulers: Vec::new(),
whitespace: WhitespaceConfig::default(), whitespace: WhitespaceConfig::default(),
indent: None, indent: IndentationConfiguration::default(),
bufferline: BufferLine::default(), bufferline: BufferLine::default(),
indent_guides: IndentGuidesConfig::default(), indent_guides: IndentGuidesConfig::default(),
color_modes: false, color_modes: false,