Skip to content

Commit

Permalink
toggle-option: only suggest boolean options
Browse files Browse the repository at this point in the history
  • Loading branch information
divarvel committed Feb 16, 2023
1 parent ce0837d commit 0e26d80
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
2 changes: 1 addition & 1 deletion helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2438,7 +2438,7 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
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),
completer: Some(completers::boolean_setting),
},
TypableCommand {
name: "get-option",
Expand Down
42 changes: 35 additions & 7 deletions helix-term/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,33 +312,61 @@ pub mod completers {
}

/// Recursive function to get all keys from this value and add them to vec
fn get_keys(value: &serde_json::Value, vec: &mut Vec<String>, scope: Option<&str>) {
fn get_keys(
value: &serde_json::Value,
vec: &mut Vec<String>,
scope: Option<&str>,
only_booleans: bool,
) {
if let Some(map) = value.as_object() {
for (key, value) in map.iter() {
let key = match scope {
Some(scope) => format!("{}.{}", scope, key),
None => key.clone(),
};
get_keys(value, vec, Some(&key));
if !value.is_object() {
get_keys(value, vec, Some(&key), only_booleans);
if !value.is_object() && (!only_booleans || value.is_boolean()) {
vec.push(key);
}
}
}
}

pub fn setting(_editor: &Editor, input: &str) -> Vec<Completion> {
static KEYS: Lazy<Vec<String>> = Lazy::new(|| {
setting_with_filter(_editor, input, false)
}

pub fn boolean_setting(_editor: &Editor, input: &str) -> Vec<Completion> {
setting_with_filter(_editor, input, true)
}

pub fn setting_with_filter(
_editor: &Editor,
input: &str,
only_booleans: bool,
) -> Vec<Completion> {
static BOOL_KEYS: Lazy<Vec<String>> = Lazy::new(|| {
let mut keys = Vec::new();
let json = serde_json::json!(Config::default());
get_keys(&json, &mut keys, None, true);
keys
});
static ALL_KEYS: Lazy<Vec<String>> = Lazy::new(|| {
let mut keys = Vec::new();
let json = serde_json::json!(Config::default());
get_keys(&json, &mut keys, None);
get_keys(&json, &mut keys, None, false);
keys
});

let matcher = Matcher::default();

let mut matches: Vec<_> = KEYS
.iter()
let source = if only_booleans {
BOOL_KEYS.iter()
} else {
ALL_KEYS.iter()
};

let mut matches: Vec<_> = source
.filter_map(|name| matcher.fuzzy_match(name, input).map(|score| (name, score)))
.collect();

Expand Down

0 comments on commit 0e26d80

Please sign in to comment.