Skip to content

Commit

Permalink
feat: Update settings at runtime
Browse files Browse the repository at this point in the history
fix the clippy warning
  • Loading branch information
irevoire committed Dec 20, 2021
1 parent 7438db6 commit b9cdccb
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
35 changes: 35 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2637,6 +2637,34 @@ pub mod cmd {
let (view, doc) = current!(cx.editor);

view.ensure_cursor_in_view(doc, line);
Ok(())
}

fn setting(
cx: &mut compositor::Context,
args: &[Cow<str>],
_event: PromptEvent,
) -> anyhow::Result<()> {
let runtime_config = &mut cx.editor.config;

if args.len() != 3 || args.get(1).map(|s| s.as_ref()) != Some("=") {
anyhow::bail!("Bad arguments. Usage: `:set key = field`");
}

match args[0].to_lowercase().as_ref() {
"scrolloff" => runtime_config.scrolloff = args[2].parse()?,
"scroll-lines" => runtime_config.scroll_lines = args[2].parse()?,
"mouse" => runtime_config.mouse = args[2].parse()?,
"line-number" => runtime_config.line_number = args[2].parse()?,
"middle-click_paste" => runtime_config.middle_click_paste = args[2].parse()?,
"smart-case" => runtime_config.smart_case = args[2].parse()?,
"auto-pairs" => runtime_config.auto_pairs = args[2].parse()?,
"auto-completion" => runtime_config.auto_completion = args[2].parse()?,
"completion-trigger-len" => runtime_config.completion_trigger_len = args[2].parse()?,
"auto-info" => runtime_config.auto_info = args[2].parse()?,
"true-color" => runtime_config.true_color = args[2].parse()?,
_ => anyhow::bail!("Unknown key `{}`.", args[0]),
}

Ok(())
}
Expand Down Expand Up @@ -2928,6 +2956,13 @@ pub mod cmd {
doc: "Go to line number.",
fun: goto_line_number,
completer: None,
},
TypableCommand {
name: "set-option",
aliases: &["set"],
doc: "Set a config option at runtime",
fun: setting,
completer: Some(completers::setting),
}
];

Expand Down
30 changes: 30 additions & 0 deletions helix-term/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ pub mod completers {
use crate::ui::prompt::Completion;
use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
use fuzzy_matcher::FuzzyMatcher;
use helix_view::editor::Config;
use helix_view::theme;
use once_cell::sync::Lazy;
use std::borrow::Cow;
use std::cmp::Reverse;

Expand Down Expand Up @@ -208,6 +210,34 @@ pub mod completers {
names
}

pub fn setting(input: &str) -> Vec<Completion> {
static KEYS: Lazy<Vec<Completion>> = Lazy::new(|| {
use serde_json::{Map, Value};
serde_json::de::from_slice::<Map<String, Value>>(
&serde_json::ser::to_vec(&Config::default()).unwrap(),
)
.unwrap()
.keys()
.map(|key| ((0..), Cow::from(key.to_string())))
.collect()
});

let matcher = Matcher::default();

let mut matches: Vec<_> = KEYS
.iter()
.filter_map(|(_range, 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.clone()))
.collect()
}

pub fn filename(input: &str) -> Vec<Completion> {
filename_impl(input, |entry| {
let is_dir = entry.file_type().map_or(false, |entry| entry.is_dir());
Expand Down
21 changes: 17 additions & 4 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub use helix_core::register::Registers;
use helix_core::syntax;
use helix_core::{Position, Selection};

use serde::Deserialize;
use serde::{Deserialize, Serialize};

fn deserialize_duration_millis<'de, D>(deserializer: D) -> Result<Duration, D::Error>
where
Expand All @@ -37,7 +37,7 @@ where
Ok(Duration::from_millis(millis))
}

#[derive(Debug, Clone, PartialEq, Deserialize)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case", default, deny_unknown_fields)]
pub struct FilePickerConfig {
/// IgnoreOptions
Expand Down Expand Up @@ -77,7 +77,8 @@ impl Default for FilePickerConfig {
}
}

#[derive(Debug, Clone, PartialEq, Deserialize)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
// #[serde(rename_all = "kebab-case", default)]
#[serde(rename_all = "kebab-case", default, deny_unknown_fields)]
pub struct Config {
/// Padding to keep between the edge of the screen and the cursor when scrolling. Defaults to 5.
Expand Down Expand Up @@ -109,7 +110,7 @@ pub struct Config {
pub true_color: bool,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Deserialize)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum LineNumber {
/// Show absolute line number
Expand All @@ -119,6 +120,18 @@ pub enum LineNumber {
Relative,
}

impl std::str::FromStr for LineNumber {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"absolute" | "abs" => Ok(Self::Absolute),
"relative" | "rel" => Ok(Self::Relative),
_ => anyhow::bail!("Line number can only be `absolute` or `relative`."),
}
}
}

impl Default for Config {
fn default() -> Self {
Self {
Expand Down

0 comments on commit b9cdccb

Please sign in to comment.