Add format_on_save setting
This lets you turn of formatting on save for specific languages.
This commit is contained in:
parent
79fad42424
commit
fa358c01cf
2 changed files with 21 additions and 1 deletions
|
@ -328,8 +328,13 @@ impl Item for Editor {
|
||||||
project: ModelHandle<Project>,
|
project: ModelHandle<Project>,
|
||||||
cx: &mut ViewContext<Self>,
|
cx: &mut ViewContext<Self>,
|
||||||
) -> Task<Result<()>> {
|
) -> Task<Result<()>> {
|
||||||
|
let settings = cx.global::<Settings>();
|
||||||
let buffer = self.buffer().clone();
|
let buffer = self.buffer().clone();
|
||||||
let buffers = buffer.read(cx).all_buffers();
|
let mut buffers = buffer.read(cx).all_buffers();
|
||||||
|
buffers.retain(|buffer| {
|
||||||
|
let language_name = buffer.read(cx).language().map(|l| l.name());
|
||||||
|
settings.format_on_save(language_name.as_deref())
|
||||||
|
});
|
||||||
let mut timeout = cx.background().timer(FORMAT_TIMEOUT).fuse();
|
let mut timeout = cx.background().timer(FORMAT_TIMEOUT).fuse();
|
||||||
let format = project.update(cx, |project, cx| project.format(buffers, true, cx));
|
let format = project.update(cx, |project, cx| project.format(buffers, true, cx));
|
||||||
cx.spawn(|this, mut cx| async move {
|
cx.spawn(|this, mut cx| async move {
|
||||||
|
|
|
@ -25,6 +25,7 @@ pub struct Settings {
|
||||||
pub tab_size: u32,
|
pub tab_size: u32,
|
||||||
pub soft_wrap: SoftWrap,
|
pub soft_wrap: SoftWrap,
|
||||||
pub preferred_line_length: u32,
|
pub preferred_line_length: u32,
|
||||||
|
pub format_on_save: bool,
|
||||||
pub language_overrides: HashMap<Arc<str>, LanguageOverride>,
|
pub language_overrides: HashMap<Arc<str>, LanguageOverride>,
|
||||||
pub theme: Arc<Theme>,
|
pub theme: Arc<Theme>,
|
||||||
}
|
}
|
||||||
|
@ -34,6 +35,7 @@ pub struct LanguageOverride {
|
||||||
pub tab_size: Option<u32>,
|
pub tab_size: Option<u32>,
|
||||||
pub soft_wrap: Option<SoftWrap>,
|
pub soft_wrap: Option<SoftWrap>,
|
||||||
pub preferred_line_length: Option<u32>,
|
pub preferred_line_length: Option<u32>,
|
||||||
|
pub format_on_save: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, Deserialize, PartialEq, Eq, JsonSchema)]
|
#[derive(Copy, Clone, Debug, Deserialize, PartialEq, Eq, JsonSchema)]
|
||||||
|
@ -52,6 +54,8 @@ pub struct SettingsFileContent {
|
||||||
pub buffer_font_size: Option<f32>,
|
pub buffer_font_size: Option<f32>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub vim_mode: Option<bool>,
|
pub vim_mode: Option<bool>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub format_on_save: Option<bool>,
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
pub editor: LanguageOverride,
|
pub editor: LanguageOverride,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
|
@ -74,6 +78,7 @@ impl Settings {
|
||||||
soft_wrap: SoftWrap::None,
|
soft_wrap: SoftWrap::None,
|
||||||
preferred_line_length: 80,
|
preferred_line_length: 80,
|
||||||
language_overrides: Default::default(),
|
language_overrides: Default::default(),
|
||||||
|
format_on_save: true,
|
||||||
theme,
|
theme,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -109,6 +114,13 @@ impl Settings {
|
||||||
.unwrap_or(self.preferred_line_length)
|
.unwrap_or(self.preferred_line_length)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn format_on_save(&self, language: Option<&str>) -> bool {
|
||||||
|
language
|
||||||
|
.and_then(|language| self.language_overrides.get(language))
|
||||||
|
.and_then(|settings| settings.format_on_save)
|
||||||
|
.unwrap_or(self.format_on_save)
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(any(test, feature = "test-support"))]
|
#[cfg(any(test, feature = "test-support"))]
|
||||||
pub fn test(cx: &gpui::AppContext) -> Settings {
|
pub fn test(cx: &gpui::AppContext) -> Settings {
|
||||||
Settings {
|
Settings {
|
||||||
|
@ -118,6 +130,7 @@ impl Settings {
|
||||||
tab_size: 4,
|
tab_size: 4,
|
||||||
soft_wrap: SoftWrap::None,
|
soft_wrap: SoftWrap::None,
|
||||||
preferred_line_length: 80,
|
preferred_line_length: 80,
|
||||||
|
format_on_save: true,
|
||||||
language_overrides: Default::default(),
|
language_overrides: Default::default(),
|
||||||
theme: gpui::fonts::with_font_cache(cx.font_cache().clone(), || Default::default()),
|
theme: gpui::fonts::with_font_cache(cx.font_cache().clone(), || Default::default()),
|
||||||
}
|
}
|
||||||
|
@ -142,6 +155,7 @@ impl Settings {
|
||||||
|
|
||||||
merge(&mut self.buffer_font_size, data.buffer_font_size);
|
merge(&mut self.buffer_font_size, data.buffer_font_size);
|
||||||
merge(&mut self.vim_mode, data.vim_mode);
|
merge(&mut self.vim_mode, data.vim_mode);
|
||||||
|
merge(&mut self.format_on_save, data.format_on_save);
|
||||||
merge(&mut self.soft_wrap, data.editor.soft_wrap);
|
merge(&mut self.soft_wrap, data.editor.soft_wrap);
|
||||||
merge(&mut self.tab_size, data.editor.tab_size);
|
merge(&mut self.tab_size, data.editor.tab_size);
|
||||||
merge(
|
merge(
|
||||||
|
@ -157,6 +171,7 @@ impl Settings {
|
||||||
|
|
||||||
merge_option(&mut target.tab_size, settings.tab_size);
|
merge_option(&mut target.tab_size, settings.tab_size);
|
||||||
merge_option(&mut target.soft_wrap, settings.soft_wrap);
|
merge_option(&mut target.soft_wrap, settings.soft_wrap);
|
||||||
|
merge_option(&mut target.format_on_save, settings.format_on_save);
|
||||||
merge_option(
|
merge_option(
|
||||||
&mut target.preferred_line_length,
|
&mut target.preferred_line_length,
|
||||||
settings.preferred_line_length,
|
settings.preferred_line_length,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue