Skip to content

Commit 3608654

Browse files
committed
wip update settings at runtime
1 parent c4ae17d commit 3608654

3 files changed

Lines changed: 57 additions & 3 deletions

File tree

helix-term/src/commands.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,6 +2089,28 @@ mod cmd {
20892089
Ok(())
20902090
}
20912091

2092+
fn setting(
2093+
cx: &mut compositor::Context,
2094+
args: &[&str],
2095+
_event: PromptEvent,
2096+
) -> anyhow::Result<()> {
2097+
use toml::value::Map;
2098+
use toml::Value;
2099+
2100+
let mut current_conf: Map<String, Value> =
2101+
toml::de::from_slice(&toml::ser::to_vec(&cx.editor.config)?)?;
2102+
let conf = toml::from_str::<Map<String, Value>>(&args.join(" "))?;
2103+
for (key, value) in conf {
2104+
if let Some(current_value) = current_conf.get_mut(&key) {
2105+
*current_value = value;
2106+
} else {
2107+
bail!("Key `{}` does not exists", key)
2108+
}
2109+
}
2110+
cx.editor.config = toml::de::from_slice(&toml::ser::to_vec(&current_conf)?)?;
2111+
Ok(())
2112+
}
2113+
20922114
pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
20932115
TypableCommand {
20942116
name: "quit",
@@ -2341,6 +2363,13 @@ mod cmd {
23412363
doc: "Open the file in a horizontal split.",
23422364
fun: hsplit,
23432365
completer: Some(completers::filename),
2366+
},
2367+
TypableCommand {
2368+
name: "set-option",
2369+
aliases: &["set"],
2370+
doc: "Set a config option at runtime",
2371+
fun: setting,
2372+
completer: Some(completers::setting),
23442373
}
23452374
];
23462375

helix-term/src/ui/mod.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ pub mod completers {
146146
use crate::ui::prompt::Completion;
147147
use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
148148
use fuzzy_matcher::FuzzyMatcher;
149+
use helix_view::editor::Config;
149150
use helix_view::theme;
150151
use std::borrow::Cow;
151152
use std::cmp::Reverse;
@@ -179,6 +180,30 @@ pub mod completers {
179180
names
180181
}
181182

183+
pub fn setting(input: &str) -> Vec<Completion> {
184+
use toml::value::Map;
185+
use toml::Value;
186+
187+
let default_config: Map<String, Value> =
188+
toml::de::from_slice(&toml::ser::to_vec(&Config::default()).unwrap()).unwrap();
189+
let keys: Vec<_> = default_config
190+
.keys()
191+
.map(|key| ((0..), Cow::from(key.to_string())))
192+
.collect();
193+
194+
let matcher = Matcher::default();
195+
196+
let mut matches: Vec<_> = keys
197+
.into_iter()
198+
.filter_map(|(_range, name)| {
199+
matcher.fuzzy_match(&name, input).map(|score| (name, score))
200+
})
201+
.collect();
202+
203+
matches.sort_unstable_by_key(|(_file, score)| Reverse(*score));
204+
matches.into_iter().map(|(name, _)| ((0..), name)).collect()
205+
}
206+
182207
pub fn filename(input: &str) -> Vec<Completion> {
183208
filename_impl(input, |entry| {
184209
let is_dir = entry.file_type().map_or(false, |entry| entry.is_dir());

helix-view/src/editor.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ pub use helix_core::register::Registers;
2222
use helix_core::syntax;
2323
use helix_core::Position;
2424

25-
use serde::Deserialize;
25+
use serde::{Deserialize, Serialize};
2626

27-
#[derive(Debug, Clone, PartialEq, Deserialize)]
27+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
2828
#[serde(rename_all = "kebab-case", default)]
2929
pub struct Config {
3030
/// Padding to keep between the edge of the screen and the cursor when scrolling. Defaults to 5.
@@ -45,7 +45,7 @@ pub struct Config {
4545
pub auto_pairs: bool,
4646
}
4747

48-
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
48+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
4949
#[serde(rename_all = "kebab-case")]
5050
pub enum LineNumber {
5151
/// Show absolute line number

0 commit comments

Comments
 (0)