on master help sign in
Add :get-option command (#2231)
commit15db6031bbbdf7a795c15050a551dee09e9d1248 authorDaniel <dmaydrum@gmail.com> dateSun 24 Apr 2022, 03:30 (5 years ago) parent3f2bd77
2 files changed+33−4
book/src/generated/typable-cmd.md+2−1 helix-term/src/commands/typed.rs+31−3
book/src/generated/typable-cmd.md+2−1
@@ -55,7 +55,8 @@
5555| `:tutor` | Open the tutorial. |
5656| `:goto`, `:g` | Go to line number. |
5757| `:set-language`, `:lang` | Set the language of current buffer. |
58−| `:set-option`, `:set` | Set a config option at runtime |
58+| `:set-option`, `:set` | Set a config option at runtime. |
59+| `:get-option`, `:get` | Get the current value of a config option. |
5960| `:sort` | Sort ranges in selection. |
6061| `:rsort` | Sort ranges in selection in reverse order. |
6162| `:tree-sitter-subtree`, `:ts-subtree` | Display tree sitter subtree under cursor, primarily for debugging queries. |
helix-term/src/commands/typed.rs+31−3
@@ -928,9 +928,30 @@ pub(super) fn goto_line_number(
928928 Ok(())
929929}
930930
931+// Fetch the current value of a config option and output as status.
932+fn get_option(
933+ cx: &mut compositor::Context,
934+ args: &[Cow<str>],
935+ _event: PromptEvent,
936+) -> anyhow::Result<()> {
937+ if args.len() != 1 {
938+ anyhow::bail!("Bad arguments. Usage: `:get key`");
939+ }
940+
941+ let key = &args[0].to_lowercase();
942+ let key_error = || anyhow::anyhow!("Unknown key `{}`", key);
943+
944+ let config = serde_json::to_value(&cx.editor.config().clone()).unwrap();
945+ let pointer = format!("/{}", key.replace('.', "/"));
946+ let value = config.pointer(&pointer).ok_or_else(key_error)?;
947+
948+ cx.editor.set_status(value.to_string());
949+ Ok(())
950+}
951+
931952/// Change config at runtime. Access nested values by dot syntax, for
932953/// example to disable smart case search, use `:set search.smart-case false`.
933−fn setting(
954+fn set_option(
934955 cx: &mut compositor::Context,
935956 args: &[Cow<str>],
936957 _event: PromptEvent,
@@ -1487,8 +1508,15 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
14871508 TypableCommand {
14881509 name: "set-option",
14891510 aliases: &["set"],
1490− doc: "Set a config option at runtime",
1491− fun: setting,
1511+ doc: "Set a config option at runtime.",
1512+ fun: set_option,
1513+ completer: Some(completers::setting),
1514+ },
1515+ TypableCommand {
1516+ name: "get-option",
1517+ aliases: &["get"],
1518+ doc: "Get the current value of a config option.",
1519+ fun: get_option,
14921520 completer: Some(completers::setting),
14931521 },
14941522 TypableCommand {