-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix previous broken refactor key into helix-view
Need to be used for autoinfo Revert "Revert "Refactor key into helix-view"" This reverts commit 10f9f72.
- Loading branch information
Showing
9 changed files
with
448 additions
and
405 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,61 @@ | ||
use anyhow::{Error, Result}; | ||
use serde::Deserialize; | ||
use std::collections::HashMap; | ||
|
||
use serde::{de::Error as SerdeError, Deserialize, Serialize}; | ||
use crate::commands::Command; | ||
use crate::keymap::Keymaps; | ||
|
||
use crate::keymap::{parse_keymaps, Keymaps}; | ||
|
||
#[derive(Default)] | ||
#[derive(Debug, Default, Clone, PartialEq, Deserialize)] | ||
pub struct Config { | ||
pub theme: Option<String>, | ||
#[serde(default)] | ||
pub lsp: LspConfig, | ||
pub keymaps: Keymaps, | ||
#[serde(default)] | ||
pub keys: Keymaps, | ||
} | ||
|
||
#[derive(Default, Serialize, Deserialize)] | ||
#[derive(Debug, Default, Clone, PartialEq, Deserialize)] | ||
pub struct LspConfig { | ||
pub display_messages: bool, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
#[serde(rename_all = "kebab-case")] | ||
struct TomlConfig { | ||
theme: Option<String>, | ||
#[serde(default)] | ||
lsp: LspConfig, | ||
keys: Option<HashMap<String, HashMap<String, String>>>, | ||
} | ||
#[test] | ||
fn parsing_keymaps_config_file() { | ||
use helix_core::hashmap; | ||
use helix_view::document::Mode; | ||
use helix_view::input::{KeyCode, KeyEvent, KeyModifiers}; | ||
|
||
let sample_keymaps = r#" | ||
[keys.insert] | ||
y = "move_line_down" | ||
S-C-a = "delete_selection" | ||
[keys.normal] | ||
A-F12 = "move_next_word_end" | ||
"#; | ||
|
||
impl<'de> Deserialize<'de> for Config { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
{ | ||
let config = TomlConfig::deserialize(deserializer)?; | ||
Ok(Self { | ||
theme: config.theme, | ||
lsp: config.lsp, | ||
keymaps: config | ||
.keys | ||
.map(|r| parse_keymaps(&r)) | ||
.transpose() | ||
.map_err(|e| D::Error::custom(format!("Error deserializing keymap: {}", e)))? | ||
.unwrap_or_else(Keymaps::default), | ||
}) | ||
} | ||
assert_eq!( | ||
toml::from_str::<Config>(sample_keymaps).unwrap(), | ||
Config { | ||
keys: Keymaps(hashmap! { | ||
Mode::Insert => hashmap! { | ||
KeyEvent { | ||
code: KeyCode::Char('y'), | ||
modifiers: KeyModifiers::NONE, | ||
} => Command::move_line_down, | ||
KeyEvent { | ||
code: KeyCode::Char('a'), | ||
modifiers: KeyModifiers::SHIFT | KeyModifiers::CONTROL, | ||
} => Command::delete_selection, | ||
}, | ||
Mode::Normal => hashmap! { | ||
KeyEvent { | ||
code: KeyCode::F(12), | ||
modifiers: KeyModifiers::ALT, | ||
} => Command::move_next_word_end, | ||
}, | ||
}), | ||
..Default::default() | ||
} | ||
); | ||
} |
Oops, something went wrong.