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

Add :toggle-option command #4085

Merged
merged 1 commit into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions book/src/generated/typable-cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,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 boolean 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
48 changes: 48 additions & 0 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::ops::Deref;
use super::*;

use helix_view::editor::{Action, CloseError, ConfigEvent};
use serde_json::Value;
use ui::completers::{self, Completer};

#[derive(Clone)]
Expand Down Expand Up @@ -1322,6 +1323,46 @@ fn set_option(
Ok(())
}

/// Toggle boolean config option 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::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)?;

if let Value::Bool(b) = *value {
*value = Value::Bool(!b);
} else {
anyhow::bail!("Key `{}` is not toggle-able", key)
}

// 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();

cx.editor
.config_events
.0
.send(ConfigEvent::Update(config))?;
Ok(())
}

/// Change the language of the current buffer at runtime.
fn language(
cx: &mut compositor::Context,
Expand Down Expand Up @@ -2009,6 +2050,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
fun: set_option,
completer: Some(completers::setting),
},
TypableCommand {
name: "toggle-option",
aliases: &["toggle"],
doc: "Toggle a boolean 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