Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(commands): allows cycling option values at runtime #4411

Merged
merged 3 commits into from
Jun 5, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 37 additions & 12 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ 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 @@ -1790,8 +1789,8 @@ fn toggle_option(
return Ok(());
}

if args.len() != 1 {
anyhow::bail!("Bad arguments. Usage: `:toggle key`");
if args.is_empty() {
anyhow::bail!("Bad arguments. Usage: `:toggle key [values]?`");
}
let key = &args[0].to_lowercase();

Expand All @@ -1801,22 +1800,48 @@ fn toggle_option(
let pointer = format!("/{}", key.replace('.', "/"));
let value = config.pointer_mut(&pointer).ok_or_else(key_error)?;

let Value::Bool(old_value) = *value else {
anyhow::bail!("Key `{}` is not toggle-able", key)
*value = match value.as_bool() {
Some(value) => serde_json::Value::Bool(!value),
pascalkuthe marked this conversation as resolved.
Show resolved Hide resolved
None => {
if args.len() != 2 {
anyhow::bail!(
"Bad arguments. For non-boolean configurations use: `:toggle key val1,val2`"
);
}
if !value.is_string() {
anyhow::bail!("Bad configuration. Cannot cycle non-string configurations");
}
pascalkuthe marked this conversation as resolved.
Show resolved Hide resolved

// The comma separated values to cycle
let first_arg = &args[1];
let field_error = |_| anyhow::anyhow!("Could not parse field `{}`", first_arg);

first_arg
.split_once(',')
.map(|(first, remaining)| {
Ok(serde_json::Value::String(
[first]
.into_iter()
.chain(remaining.split(','))
.skip_while(|e| e != value)
.nth(1)
.unwrap_or(first)
.to_string(),
))
})
.unwrap_or_else(|| first_arg.parse().map_err(field_error))?
pascalkuthe marked this conversation as resolved.
Show resolved Hide resolved
}
};

let new_value = !old_value;
*value = Value::Bool(new_value);
// This unwrap should never fail because we only replace one boolean value
// with another, maintaining a valid json config
let config = serde_json::from_value(config).unwrap();
let status = format!("Option `{}` is now set to `{}`", key, value);
pascalkuthe marked this conversation as resolved.
Show resolved Hide resolved
let config = serde_json::from_value(config)
.map_err(|_| anyhow::anyhow!("Could not parse field: `{:?}`", &args))?;

cx.editor
.config_events
.0
.send(ConfigEvent::Update(config))?;
cx.editor
.set_status(format!("Option `{}` is now set to `{}`", key, new_value));
cx.editor.set_status(status);
Ok(())
}

Expand Down