Skip to content

Commit

Permalink
feature: add a toggle-option command
Browse files Browse the repository at this point in the history
This commands allows to toggle boolean options.
It does not support other enumerated types.
  • Loading branch information
divarvel committed Feb 1, 2023
1 parent 0f562df commit f552a50
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions book/src/generated/typable-cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
| `:goto`, `:g` | Goto line number. |
| `:set-language`, `:lang` | Set the language of current buffer. |
| `:set-option`, `:set` | Set a config option at runtime.<br>For example to disable smart case search, use `:set search.smart-case false`. |
| `:toggle-option`, `:toggle` | Toggle a config option at runtime.<br>For example to toggle smart case search, use `:toggle search.smart-case`. |
| `:get-option`, `:get` | Get the current value of a config option. |
| `:sort` | Sort ranges in selection. |
| `:rsort` | Sort ranges in selection in reverse order. |
Expand Down
43 changes: 43 additions & 0 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1479,6 +1479,42 @@ fn get_option(
Ok(())
}

/// Change config at runtime. Access nested values by dot syntax, for
/// example to toggle smart case search, use `:toggle search.smart-case`.
fn toggle_option(
cx: &mut compositor::Context,
args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}

if args.len() != 1 {
anyhow::bail!("Bad arguments. Usage: `:toggle key`");
}

let key = &args[0].to_lowercase();

let key_error = || anyhow!("Unknown key `{}`", key);

let mut config = serde_json::json!(&cx.editor.config().deref());
let pointer = format!("/{}", key.replace('.', "/"));
let value = config.pointer_mut(&pointer).ok_or_else(key_error)?;

*value = match value {
serde_json::Value::Bool(v) => serde_json::Value::Bool(!*v),
_ => anyhow::bail!("key {} must be a bool to be toggled", key),
};
let config = serde_json::from_value(config)?;

cx.editor
.config_events
.0
.send(ConfigEvent::Update(config))?;
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 set_option(
Expand Down Expand Up @@ -2246,6 +2282,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
fun: set_option,
completer: Some(completers::setting),
},
TypableCommand {
name: "toggle-option",
aliases: &["toggle"],
doc: "Toggle a config option at runtime.\nFor example to toggle smart case search, use `:toggle search.smart-case`.",
fun: toggle_option,
completer: Some(completers::setting),
},
TypableCommand {
name: "get-option",
aliases: &["get"],
Expand Down

0 comments on commit f552a50

Please sign in to comment.