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 4, 2023
1 parent f552a50 commit 8b19d2c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 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 @@ -2287,7 +2287,7 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
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),
completer: Some(completers::boolean_setting),
},
TypableCommand {
name: "get-option",
Expand Down
35 changes: 31 additions & 4 deletions helix-term/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,15 +314,20 @@ 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);
}
}
Expand All @@ -333,7 +338,29 @@ pub mod completers {
static 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()
.filter_map(|name| matcher.fuzzy_match(name, input).map(|score| (name, score)))
.collect();

matches.sort_unstable_by_key(|(_file, score)| Reverse(*score));
matches
.into_iter()
.map(|(name, _)| ((0..), name.into()))
.collect()
}

pub fn boolean_setting(_editor: &Editor, input: &str) -> Vec<Completion> {
static 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
});

Expand Down

0 comments on commit 8b19d2c

Please sign in to comment.