Refactor :set to parse by deserializing values (#1799)

* Refactor :set to parse by deserializing values

* Implement serialize for idle_timeout config
pull/1781/head
Gokul Soumya 3 years ago committed by GitHub
parent 0902ede7b1
commit 2b0835b295
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -216,14 +216,7 @@ impl FromStr for AutoPairConfig {
// only do bool parsing for runtime setting // only do bool parsing for runtime setting
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
let enable: bool = s.parse()?; let enable: bool = s.parse()?;
Ok(AutoPairConfig::Enable(enable))
let enable = if enable {
AutoPairConfig::Enable(true)
} else {
AutoPairConfig::Enable(false)
};
Ok(enable)
} }
} }

@ -872,34 +872,32 @@ pub(super) fn goto_line_number(
Ok(()) Ok(())
} }
/// Change config at runtime. Access nested values by dot syntax, for
/// example to disable smart case search, use `:set search.smart-case false`.
fn setting( fn setting(
cx: &mut compositor::Context, cx: &mut compositor::Context,
args: &[Cow<str>], args: &[Cow<str>],
_event: PromptEvent, _event: PromptEvent,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let runtime_config = &mut cx.editor.config;
if args.len() != 2 { if args.len() != 2 {
anyhow::bail!("Bad arguments. Usage: `:set key field`"); anyhow::bail!("Bad arguments. Usage: `:set key field`");
} }
let (key, arg) = (&args[0].to_lowercase(), &args[1]); let (key, arg) = (&args[0].to_lowercase(), &args[1]);
match key.as_ref() { let key_error = || anyhow::anyhow!("Unknown key `{key}`");
"scrolloff" => runtime_config.scrolloff = arg.parse()?, let field_error = |_| anyhow::anyhow!("Could not parse field `{arg}`");
"scroll-lines" => runtime_config.scroll_lines = arg.parse()?,
"mouse" => runtime_config.mouse = arg.parse()?, let mut config = serde_json::to_value(&cx.editor.config).unwrap();
"line-number" => runtime_config.line_number = arg.parse()?, let pointer = format!("/{}", key.replace('.', "/"));
"middle-click_paste" => runtime_config.middle_click_paste = arg.parse()?, let value = config.pointer_mut(&pointer).ok_or_else(key_error)?;
"auto-pairs" => runtime_config.auto_pairs = arg.parse()?,
"auto-completion" => runtime_config.auto_completion = arg.parse()?, *value = if value.is_string() {
"completion-trigger-len" => runtime_config.completion_trigger_len = arg.parse()?, // JSON strings require quotes, so we can't .parse() directly
"auto-info" => runtime_config.auto_info = arg.parse()?, serde_json::Value::String(arg.to_string())
"true-color" => runtime_config.true_color = arg.parse()?, } else {
"search.smart-case" => runtime_config.search.smart_case = arg.parse()?, arg.parse().map_err(field_error)?
"search.wrap-around" => runtime_config.search.wrap_around = arg.parse()?, };
_ => anyhow::bail!("Unknown key `{}`.", args[0]), cx.editor.config = serde_json::from_value(config).map_err(field_error)?;
}
Ok(()) Ok(())
} }

@ -38,7 +38,7 @@ use helix_core::{
use helix_core::{Position, Selection}; use helix_core::{Position, Selection};
use helix_dap as dap; use helix_dap as dap;
use serde::{ser::SerializeMap, Deserialize, Deserializer, Serialize}; use serde::{ser::SerializeMap, Deserialize, Deserializer, Serialize, Serializer};
fn deserialize_duration_millis<'de, D>(deserializer: D) -> Result<Duration, D::Error> fn deserialize_duration_millis<'de, D>(deserializer: D) -> Result<Duration, D::Error>
where where
@ -48,6 +48,18 @@ where
Ok(Duration::from_millis(millis)) Ok(Duration::from_millis(millis))
} }
fn serialize_duration_millis<S>(duration: &Duration, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_u64(
duration
.as_millis()
.try_into()
.map_err(|_| serde::ser::Error::custom("duration value overflowed u64"))?,
)
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case", default, deny_unknown_fields)] #[serde(rename_all = "kebab-case", default, deny_unknown_fields)]
pub struct FilePickerConfig { pub struct FilePickerConfig {
@ -109,8 +121,12 @@ pub struct Config {
pub auto_pairs: AutoPairConfig, pub auto_pairs: AutoPairConfig,
/// Automatic auto-completion, automatically pop up without user trigger. Defaults to true. /// Automatic auto-completion, automatically pop up without user trigger. Defaults to true.
pub auto_completion: bool, pub auto_completion: bool,
/// Time in milliseconds since last keypress before idle timers trigger. Used for autocompletion, set to 0 for instant. Defaults to 400ms. /// Time in milliseconds since last keypress before idle timers trigger.
#[serde(skip_serializing, deserialize_with = "deserialize_duration_millis")] /// Used for autocompletion, set to 0 for instant. Defaults to 400ms.
#[serde(
serialize_with = "serialize_duration_millis",
deserialize_with = "deserialize_duration_millis"
)]
pub idle_timeout: Duration, pub idle_timeout: Duration,
pub completion_trigger_len: u8, pub completion_trigger_len: u8,
/// Whether to display infoboxes. Defaults to true. /// Whether to display infoboxes. Defaults to true.

Loading…
Cancel
Save