Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion book/src/remapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ this:
> Within macros, wrap them in `<>`, e.g. `<A-X>` and `<C-X>` to distinguish from the `A` or `C` keys.

```toml
# At most one section each of 'keys.normal', 'keys.insert' and 'keys.select'
# At most one section each of 'keys.normal', 'keys.insert', 'keys.select' and 'keys.all_modes'
# Use 'keys.all_modes' for bindings that apply to all three modes.
[keys.normal]
C-s = ":w" # Maps Ctrl-s to the typable command :w which is an alias for :write (save file)
C-o = ":open ~/.config/helix/config.toml" # Maps Ctrl-o to opening of the helix config file
Expand Down
91 changes: 85 additions & 6 deletions helix-term/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use crate::keymap;
use crate::keymap::{merge_keys, KeyTrie};
use helix_loader::merge_toml_values;
use helix_view::{document::Mode, theme};
use helix_view::{
document::{ConfigMode, Mode},
theme,
};
use serde::Deserialize;
use std::collections::HashMap;
use std::collections::{hash_map::Entry, HashMap};
use std::fmt::Display;
use std::fs;
use std::io::Error as IOError;
Expand All @@ -20,10 +23,37 @@ pub struct Config {
#[serde(deny_unknown_fields)]
pub struct ConfigRaw {
pub theme: Option<theme::Config>,
pub keys: Option<HashMap<Mode, KeyTrie>>,
pub keys: Option<HashMap<ConfigMode, KeyTrie>>,
pub editor: Option<toml::Value>,
}

fn convert_config_mode(mut conf_m: HashMap<ConfigMode, KeyTrie>) -> HashMap<Mode, KeyTrie> {
let mut keys: HashMap<Mode, KeyTrie> = HashMap::new();
if conf_m.contains_key(&ConfigMode::All) {
let trie = conf_m.remove(&ConfigMode::All).unwrap();
keys.insert(Mode::Normal, trie.clone());
keys.insert(Mode::Select, trie.clone());
keys.insert(Mode::Insert, trie);
}

for (key, trie) in conf_m {
if let ConfigMode::Mode(mode) = key {
match keys.entry(mode) {
Entry::Occupied(entry) => {
let mut node = entry.remove();
node.merge_nodes(trie);
keys.insert(mode, node);
}
Entry::Vacant(entry) => {
entry.insert(trie);
}
}
}
}

keys
}

impl Default for Config {
fn default() -> Config {
Config {
Expand Down Expand Up @@ -68,10 +98,10 @@ impl Config {
(Ok(global), Ok(local)) => {
let mut keys = keymap::default();
if let Some(global_keys) = global.keys {
merge_keys(&mut keys, global_keys)
merge_keys(&mut keys, convert_config_mode(global_keys));
}
if let Some(local_keys) = local.keys {
merge_keys(&mut keys, local_keys)
merge_keys(&mut keys, convert_config_mode(local_keys));
}

let editor = match (global.editor, local.editor) {
Expand All @@ -98,7 +128,7 @@ impl Config {
(Ok(config), Err(_)) | (Err(_), Ok(config)) => {
let mut keys = keymap::default();
if let Some(keymap) = config.keys {
merge_keys(&mut keys, keymap);
merge_keys(&mut keys, convert_config_mode(keymap));
}
Config {
theme: config.theme,
Expand Down Expand Up @@ -174,6 +204,55 @@ mod tests {
);
}

#[test]
fn parsing_keymaps_all_modes() {
use crate::keymap;
use helix_core::hashmap;
use helix_view::document::Mode;

let sample_keymaps = r#"
[keys.normal]
A-F12 = "move_next_word_end"

[keys.all_modes]
y = "move_line_up"

[keys.select]
y = "move_line_down"
S-C-a = "delete_selection"

[keys.insert]
C-s = "goto_line_start"
"#;

let mut keys = keymap::default();
merge_keys(
&mut keys,
hashmap! {
Mode::Insert => keymap!({ "Insert mode"
"y" => move_line_up,
"C-s" => goto_line_start,
}),
Mode::Normal => keymap!({ "Normal mode"
"A-F12" => move_next_word_end,
"y" => move_line_up,
}),
Mode::Select => keymap!({ "Select mode"
"S-C-a" => delete_selection,
"y" => move_line_down,
}),
},
);

assert_eq!(
Config::load_test(sample_keymaps),
Config {
keys,
..Default::default()
}
);
}

#[test]
fn keys_resolve_to_correct_defaults() {
// From serde default
Expand Down
48 changes: 48 additions & 0 deletions helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,54 @@ impl Serialize for Mode {
serializer.collect_str(self)
}
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum ConfigMode {
Mode(Mode),
All,
}

impl Display for ConfigMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::All => f.write_str("all_modes"),
Self::Mode(mode) => mode.fmt(f),
}
}
}

impl FromStr for ConfigMode {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"all_modes" => Ok(Self::All),
_ => match Mode::from_str(s) {
Ok(mode) => Ok(ConfigMode::Mode(mode)),
Err(e) => Err(e),
},
}
}
}

impl<'de> Deserialize<'de> for ConfigMode {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
s.parse().map_err(de::Error::custom)
}
}

impl Serialize for ConfigMode {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.collect_str(self)
}
}

/// A snapshot of the text of a document that we want to write out to disk
#[derive(Debug, Clone)]
pub struct DocumentSavedEvent {
Expand Down