on master help sign in
Update settings at runtime (#798)

* feat: Update settings at runtime fix the clippy warning * update the documentation * use to_value instead of to_vec+from_value * drop the equal * remove an useless comment * apply suggestion

commita306a1052a51c686b24a6f339190878b8029a894 authorTamo <tamo@meilisearch.com> dateSun 26 Dec 2021, 02:04 (5 years ago) parent6af0d51
4 files changed+81−4
book/src/generated/typable-cmd.md+1 helix-term/src/commands.rs+37 helix-term/src/ui/mod.rs+27 helix-view/src/editor.rs+16−4
book/src/generated/typable-cmd.md+1
@@ -41,3 +41,4 @@
4141| `:hsplit`, `:hs`, `:sp` | Open the file in a horizontal split. |
4242| `:tutor` | Open the tutorial. |
4343| `:goto`, `:g` | Go to line number. |
44+| `:set-option`, `:set` | Set a config option at runtime |
helix-term/src/commands.rs+37
@@ -2637,6 +2637,36 @@ pub mod cmd {
26372637 let (view, doc) = current!(cx.editor);
26382638
26392639 view.ensure_cursor_in_view(doc, line);
2640+ Ok(())
2641+ }
2642+
2643+ fn setting(
2644+ cx: &mut compositor::Context,
2645+ args: &[Cow<str>],
2646+ _event: PromptEvent,
2647+ ) -> anyhow::Result<()> {
2648+ let runtime_config = &mut cx.editor.config;
2649+
2650+ if args.len() != 2 {
2651+ anyhow::bail!("Bad arguments. Usage: `:set key field`");
2652+ }
2653+
2654+ let (key, arg) = (&args[0].to_lowercase(), &args[1]);
2655+
2656+ match key.as_ref() {
2657+ "scrolloff" => runtime_config.scrolloff = arg.parse()?,
2658+ "scroll-lines" => runtime_config.scroll_lines = arg.parse()?,
2659+ "mouse" => runtime_config.mouse = arg.parse()?,
2660+ "line-number" => runtime_config.line_number = arg.parse()?,
2661+ "middle-click_paste" => runtime_config.middle_click_paste = arg.parse()?,
2662+ "smart-case" => runtime_config.smart_case = arg.parse()?,
2663+ "auto-pairs" => runtime_config.auto_pairs = arg.parse()?,
2664+ "auto-completion" => runtime_config.auto_completion = arg.parse()?,
2665+ "completion-trigger-len" => runtime_config.completion_trigger_len = arg.parse()?,
2666+ "auto-info" => runtime_config.auto_info = arg.parse()?,
2667+ "true-color" => runtime_config.true_color = arg.parse()?,
2668+ _ => anyhow::bail!("Unknown key `{}`.", args[0]),
2669+ }
26402670
26412671 Ok(())
26422672 }
@@ -2928,6 +2958,13 @@ pub mod cmd {
29282958 doc: "Go to line number.",
29292959 fun: goto_line_number,
29302960 completer: None,
2961+ },
2962+ TypableCommand {
2963+ name: "set-option",
2964+ aliases: &["set"],
2965+ doc: "Set a config option at runtime",
2966+ fun: setting,
2967+ completer: Some(completers::setting),
29312968 }
29322969 ];
29332970
helix-term/src/ui/mod.rs+27
@@ -174,7 +174,9 @@ pub mod completers {
174174 use crate::ui::prompt::Completion;
175175 use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
176176 use fuzzy_matcher::FuzzyMatcher;
177+ use helix_view::editor::Config;
177178 use helix_view::theme;
179+ use once_cell::sync::Lazy;
178180 use std::borrow::Cow;
179181 use std::cmp::Reverse;
180182
@@ -208,6 +210,31 @@ pub mod completers {
208210 names
209211 }
210212
213+ pub fn setting(input: &str) -> Vec<Completion> {
214+ static KEYS: Lazy<Vec<String>> = Lazy::new(|| {
215+ serde_json::to_value(Config::default())
216+ .unwrap()
217+ .as_object()
218+ .unwrap()
219+ .keys()
220+ .cloned()
221+ .collect()
222+ });
223+
224+ let matcher = Matcher::default();
225+
226+ let mut matches: Vec<_> = KEYS
227+ .iter()
228+ .filter_map(|name| matcher.fuzzy_match(name, input).map(|score| (name, score)))
229+ .collect();
230+
231+ matches.sort_unstable_by_key(|(_file, score)| Reverse(*score));
232+ matches
233+ .into_iter()
234+ .map(|(name, _)| ((0..), name.into()))
235+ .collect()
236+ }
237+
211238 pub fn filename(input: &str) -> Vec<Completion> {
212239 filename_impl(input, |entry| {
213240 let is_dir = entry.file_type().map_or(false, |entry| entry.is_dir());
helix-view/src/editor.rs+16−4
@@ -27,7 +27,7 @@ pub use helix_core::register::Registers;
2727use helix_core::syntax;
2828use helix_core::{Position, Selection};
2929
30−use serde::Deserialize;
30+use serde::{Deserialize, Serialize};
3131
3232fn deserialize_duration_millis<'de, D>(deserializer: D) -> Result<Duration, D::Error>
3333where
@@ -37,7 +37,7 @@ where
3737 Ok(Duration::from_millis(millis))
3838}
3939
40−#[derive(Debug, Clone, PartialEq, Deserialize)]
40+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
4141#[serde(rename_all = "kebab-case", default, deny_unknown_fields)]
4242pub struct FilePickerConfig {
4343 /// IgnoreOptions
@@ -77,7 +77,7 @@ impl Default for FilePickerConfig {
7777 }
7878}
7979
80−#[derive(Debug, Clone, PartialEq, Deserialize)]
80+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
8181#[serde(rename_all = "kebab-case", default, deny_unknown_fields)]
8282pub struct Config {
8383 /// Padding to keep between the edge of the screen and the cursor when scrolling. Defaults to 5.
@@ -109,7 +109,7 @@ pub struct Config {
109109 pub true_color: bool,
110110}
111111
112−#[derive(Debug, Copy, Clone, PartialEq, Eq, Deserialize)]
112+#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
113113#[serde(rename_all = "kebab-case")]
114114pub enum LineNumber {
115115 /// Show absolute line number
@@ -119,6 +119,18 @@ pub enum LineNumber {
119119 Relative,
120120}
121121
122+impl std::str::FromStr for LineNumber {
123+ type Err = anyhow::Error;
124+
125+ fn from_str(s: &str) -> Result<Self, Self::Err> {
126+ match s.to_lowercase().as_str() {
127+ "absolute" | "abs" => Ok(Self::Absolute),
128+ "relative" | "rel" => Ok(Self::Relative),
129+ _ => anyhow::bail!("Line number can only be `absolute` or `relative`."),
130+ }
131+ }
132+}
133+
122134impl Default for Config {
123135 fn default() -> Self {
124136 Self {