Skip to content

Commit

Permalink
commands(toggle): use pattern matching on the Value enum (helix-edito…
Browse files Browse the repository at this point in the history
  • Loading branch information
alevinval authored and mtoohey31 committed Jun 2, 2024
1 parent bb74287 commit 2d374dc
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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)?
};
Expand Down Expand Up @@ -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)
Expand All @@ -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}");
Expand Down

0 comments on commit 2d374dc

Please sign in to comment.