commands(toggle): use pattern matching on the Value enum (#7240)
This commit is contained in:
parent
ba691f4fb0
commit
204bac1706
1 changed files with 11 additions and 15 deletions
|
@ -8,6 +8,7 @@ use super::*;
|
|||
use helix_core::{encoding, shellwords::Shellwords};
|
||||
use helix_view::document::DEFAULT_LANGUAGE_NAME;
|
||||
use helix_view::editor::{Action, CloseError, ConfigEvent};
|
||||
use serde_json::Value;
|
||||
use ui::completers::{self, Completer};
|
||||
|
||||
#[derive(Clone)]
|
||||
|
@ -1764,7 +1765,7 @@ fn set_option(
|
|||
|
||||
*value = if value.is_string() {
|
||||
// JSON strings require quotes, so we can't .parse() directly
|
||||
serde_json::Value::String(arg.to_string())
|
||||
Value::String(arg.to_string())
|
||||
} else {
|
||||
arg.parse().map_err(field_error)?
|
||||
};
|
||||
|
@ -1800,29 +1801,21 @@ fn toggle_option(
|
|||
let pointer = format!("/{}", key.replace('.', "/"));
|
||||
let value = config.pointer_mut(&pointer).ok_or_else(key_error)?;
|
||||
|
||||
*value = match value.as_bool() {
|
||||
Some(value) => {
|
||||
*value = match value {
|
||||
Value::Bool(ref value) => {
|
||||
ensure!(
|
||||
args.len() == 1,
|
||||
"Bad arguments. For boolean configurations use: `:toggle key`"
|
||||
);
|
||||
serde_json::Value::Bool(!value)
|
||||
Value::Bool(!value)
|
||||
}
|
||||
None => {
|
||||
Value::String(ref value) => {
|
||||
ensure!(
|
||||
args.len() > 2,
|
||||
"Bad arguments. For non-boolean configurations use: `:toggle key val1 val2 ...`",
|
||||
);
|
||||
ensure!(
|
||||
value.is_string(),
|
||||
"Bad configuration. Cannot cycle non-string configurations"
|
||||
"Bad arguments. For string configurations use: `:toggle key val1 val2 ...`",
|
||||
);
|
||||
|
||||
let value = value
|
||||
.as_str()
|
||||
.expect("programming error: should have been ensured before");
|
||||
|
||||
serde_json::Value::String(
|
||||
Value::String(
|
||||
args[1..]
|
||||
.iter()
|
||||
.skip_while(|e| *e != value)
|
||||
|
@ -1831,6 +1824,9 @@ fn toggle_option(
|
|||
.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}");
|
||||
|
|
Loading…
Add table
Reference in a new issue