diff --git a/crates/language/src/language_settings.rs b/crates/language/src/language_settings.rs index d19be7bd02ec5a..8b54d81c3cc259 100644 --- a/crates/language/src/language_settings.rs +++ b/crates/language/src/language_settings.rs @@ -58,56 +58,48 @@ fn editorconfig_settings( file: Option<&Arc>, cx: &AppContext, ) -> Option { - let path = file.and_then(|f| f.as_local()).map(|f| f.abs_path(cx)); - - if path.is_none() { - return None; - } - - let mut cfg = ec4rs::properties_of(path.unwrap()).unwrap_or_default(); - cfg.use_fallbacks(); - let max_line_length = cfg.get::().ok().and_then(|v| match v { - MaxLineLen::Value(u) => Some(u as u32), - MaxLineLen::Off => None, - }); - Some(EditorConfigContent { - tab_size: cfg - .get::() - .map(|v| match v { - IndentSize::Value(u) => NonZeroU32::new(u as u32), - IndentSize::UseTabWidth => cfg - .get::() - .map(|w| match w { + file.and_then(|f| f.as_local()) + // If we don't hit an `editorconfig` file, we don't update configs. This + // avoids a performance hit of cloning settings. We could consider showing + // the error to the user though (it may be a parsing error). + .and_then(|local_file| ec4rs::properties_of(local_file.abs_path(cx)).ok()) + .map(|mut cfg| { + cfg.use_fallbacks(); + let max_line_length = cfg.get::().ok().and_then(|v| match v { + MaxLineLen::Value(u) => Some(u as u32), + MaxLineLen::Off => None, + }); + EditorConfigContent { + tab_size: cfg.get::().ok().and_then(|v| match v { + IndentSize::Value(u) => NonZeroU32::new(u as u32), + IndentSize::UseTabWidth => cfg.get::().ok().and_then(|w| match w { TabWidth::Value(u) => NonZeroU32::new(u as u32), + }), + }), + hard_tabs: cfg + .get::() + .map(|v| v.eq(&IndentStyle::Tabs)) + .ok(), + ensure_final_newline_on_save: cfg + .get::() + .map(|v| match v { + FinalNewline::Value(b) => b, }) - .ok() - .flatten(), - }) - .ok() - .flatten(), - hard_tabs: cfg - .get::() - .map(|v| v.eq(&IndentStyle::Tabs)) - .ok(), - ensure_final_newline_on_save: cfg - .get::() - .map(|v| match v { - FinalNewline::Value(b) => b, - }) - .ok(), - remove_trailing_whitespace_on_save: cfg - .get::() - .map(|v| match v { - TrimTrailingWs::Value(b) => b, - }) - .ok(), - preferred_line_length: max_line_length, - soft_wrap: if max_line_length.is_some() { - Some(SoftWrap::PreferredLineLength) - } else { - None - }, - }) + .ok(), + remove_trailing_whitespace_on_save: cfg + .get::() + .map(|v| match v { + TrimTrailingWs::Value(b) => b, + }) + .ok(), + preferred_line_length: max_line_length, + soft_wrap: if max_line_length.is_some() { + Some(SoftWrap::PreferredLineLength) + } else { + None + }, + } + }) } /// Returns the settings for all languages from the provided file.