Skip to content

Commit

Permalink
better cache hit
Browse files Browse the repository at this point in the history
  • Loading branch information
BuonOmo committed Aug 16, 2024
1 parent f0ba7ef commit 4c55d63
Showing 1 changed file with 40 additions and 48 deletions.
88 changes: 40 additions & 48 deletions crates/language/src/language_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,56 +58,48 @@ fn editorconfig_settings(
file: Option<&Arc<dyn File>>,
cx: &AppContext,
) -> Option<EditorConfigContent> {
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::<MaxLineLen>().ok().and_then(|v| match v {
MaxLineLen::Value(u) => Some(u as u32),
MaxLineLen::Off => None,
});
Some(EditorConfigContent {
tab_size: cfg
.get::<IndentSize>()
.map(|v| match v {
IndentSize::Value(u) => NonZeroU32::new(u as u32),
IndentSize::UseTabWidth => cfg
.get::<TabWidth>()
.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::<MaxLineLen>().ok().and_then(|v| match v {
MaxLineLen::Value(u) => Some(u as u32),
MaxLineLen::Off => None,
});
EditorConfigContent {
tab_size: cfg.get::<IndentSize>().ok().and_then(|v| match v {
IndentSize::Value(u) => NonZeroU32::new(u as u32),
IndentSize::UseTabWidth => cfg.get::<TabWidth>().ok().and_then(|w| match w {
TabWidth::Value(u) => NonZeroU32::new(u as u32),
}),
}),
hard_tabs: cfg
.get::<IndentStyle>()
.map(|v| v.eq(&IndentStyle::Tabs))
.ok(),
ensure_final_newline_on_save: cfg
.get::<FinalNewline>()
.map(|v| match v {
FinalNewline::Value(b) => b,
})
.ok()
.flatten(),
})
.ok()
.flatten(),
hard_tabs: cfg
.get::<IndentStyle>()
.map(|v| v.eq(&IndentStyle::Tabs))
.ok(),
ensure_final_newline_on_save: cfg
.get::<FinalNewline>()
.map(|v| match v {
FinalNewline::Value(b) => b,
})
.ok(),
remove_trailing_whitespace_on_save: cfg
.get::<TrimTrailingWs>()
.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::<TrimTrailingWs>()
.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.
Expand Down

0 comments on commit 4c55d63

Please sign in to comment.