Backward compat format settings

This commit is contained in:
Julia 2022-09-23 16:52:00 -04:00
parent af5ad2d5ce
commit 879a0d8b12
3 changed files with 26 additions and 12 deletions

View File

@ -43,7 +43,7 @@
// "default_dock_anchor": "expanded"
"default_dock_anchor": "right",
// Whether or not to perform a buffer format before saving
"format_on_save": true,
"format_on_save": "off",
// How to perform a buffer format. This setting can take two values:
//
// 1. Format code using the current language server:

View File

@ -40,7 +40,7 @@ use postage::watch;
use rand::prelude::*;
use search::SearchQuery;
use serde::Serialize;
use settings::Settings;
use settings::{FormatOnSave, Formatter, Settings};
use sha2::{Digest, Sha256};
use similar::{ChangeTag, TextDiff};
use std::{
@ -3120,12 +3120,11 @@ impl Project {
)
});
if trigger == FormatTrigger::Save && !format_on_save {
continue;
}
let transaction = match (formatter, format_on_save) {
(_, FormatOnSave::Off) if trigger == FormatTrigger::Save => continue,
let transaction = match formatter {
settings::Formatter::LanguageServer => Self::format_via_lsp(
(Formatter::LanguageServer, FormatOnSave::On | FormatOnSave::Off)
| (_, FormatOnSave::LanguageServer) => Self::format_via_lsp(
&this,
&buffer,
&buffer_abs_path,
@ -3136,7 +3135,11 @@ impl Project {
.await
.context("failed to format via language server")?,
settings::Formatter::External { command, arguments } => {
(
Formatter::External { command, arguments },
FormatOnSave::On | FormatOnSave::Off,
)
| (_, FormatOnSave::External { command, arguments }) => {
Self::format_via_external_command(
&buffer,
&buffer_abs_path,

View File

@ -58,7 +58,7 @@ pub struct EditorSettings {
pub hard_tabs: Option<bool>,
pub soft_wrap: Option<SoftWrap>,
pub preferred_line_length: Option<u32>,
pub format_on_save: Option<bool>,
pub format_on_save: Option<FormatOnSave>,
pub formatter: Option<Formatter>,
pub enable_language_server: Option<bool>,
}
@ -70,6 +70,17 @@ pub enum SoftWrap {
EditorWidth,
PreferredLineLength,
}
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum FormatOnSave {
On,
Off,
LanguageServer,
External {
command: String,
arguments: Vec<String>,
},
}
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
@ -324,8 +335,8 @@ impl Settings {
self.language_setting(language, |settings| settings.preferred_line_length)
}
pub fn format_on_save(&self, language: Option<&str>) -> bool {
self.language_setting(language, |settings| settings.format_on_save)
pub fn format_on_save(&self, language: Option<&str>) -> FormatOnSave {
self.language_setting(language, |settings| settings.format_on_save.clone())
}
pub fn formatter(&self, language: Option<&str>) -> Formatter {
@ -364,7 +375,7 @@ impl Settings {
hard_tabs: Some(false),
soft_wrap: Some(SoftWrap::None),
preferred_line_length: Some(80),
format_on_save: Some(true),
format_on_save: Some(FormatOnSave::On),
formatter: Some(Formatter::LanguageServer),
enable_language_server: Some(true),
},