commands(toggle): use pattern matching on the Value enum (#7240)

pull/7270/head
Alex Vinyals 1 year ago committed by GitHub
parent ba691f4fb0
commit 204bac1706
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -8,6 +8,7 @@ use super::*;
use helix_core::{encoding, shellwords::Shellwords}; use helix_core::{encoding, shellwords::Shellwords};
use helix_view::document::DEFAULT_LANGUAGE_NAME; use helix_view::document::DEFAULT_LANGUAGE_NAME;
use helix_view::editor::{Action, CloseError, ConfigEvent}; use helix_view::editor::{Action, CloseError, ConfigEvent};
use serde_json::Value;
use ui::completers::{self, Completer}; use ui::completers::{self, Completer};
#[derive(Clone)] #[derive(Clone)]
@ -1764,7 +1765,7 @@ fn set_option(
*value = if value.is_string() { *value = if value.is_string() {
// JSON strings require quotes, so we can't .parse() directly // JSON strings require quotes, so we can't .parse() directly
serde_json::Value::String(arg.to_string()) Value::String(arg.to_string())
} else { } else {
arg.parse().map_err(field_error)? arg.parse().map_err(field_error)?
}; };
@ -1800,29 +1801,21 @@ fn toggle_option(
let pointer = format!("/{}", key.replace('.', "/")); let pointer = format!("/{}", key.replace('.', "/"));
let value = config.pointer_mut(&pointer).ok_or_else(key_error)?; let value = config.pointer_mut(&pointer).ok_or_else(key_error)?;
*value = match value.as_bool() { *value = match value {
Some(value) => { Value::Bool(ref value) => {
ensure!( ensure!(
args.len() == 1, args.len() == 1,
"Bad arguments. For boolean configurations use: `:toggle key`" "Bad arguments. For boolean configurations use: `:toggle key`"
); );
serde_json::Value::Bool(!value) Value::Bool(!value)
} }
None => { Value::String(ref value) => {
ensure!( ensure!(
args.len() > 2, args.len() > 2,
"Bad arguments. For non-boolean configurations use: `:toggle key val1 val2 ...`", "Bad arguments. For string configurations use: `:toggle key val1 val2 ...`",
);
ensure!(
value.is_string(),
"Bad configuration. Cannot cycle non-string configurations"
); );
let value = value Value::String(
.as_str()
.expect("programming error: should have been ensured before");
serde_json::Value::String(
args[1..] args[1..]
.iter() .iter()
.skip_while(|e| *e != value) .skip_while(|e| *e != value)
@ -1831,6 +1824,9 @@ fn toggle_option(
.to_string(), .to_string(),
) )
} }
Value::Null | Value::Object(_) | Value::Array(_) | Value::Number(_) => {
anyhow::bail!("Configuration {key} does not support toggle yet")
}
}; };
let status = format!("'{key}' is now set to {value}"); let status = format!("'{key}' is now set to {value}");

Loading…
Cancel
Save