aboutsummaryrefslogtreecommitdiff
path: root/helix-term/src/commands.rs
diff options
context:
space:
mode:
authorIvan Tham2021-06-17 16:52:41 +0000
committerBlaž Hrastnik2021-06-19 07:37:15 +0000
commitca806d4f852e934651132fc9570a6110e30f646d (patch)
tree66e3fcfd6c018f435e23aee30aafd367ec39d7f2 /helix-term/src/commands.rs
parent1c2585202145467f0fde7ad9c571e462081c3656 (diff)
Refactor key into helix-view
Now also make use of Deserialize for Config.
Diffstat (limited to 'helix-term/src/commands.rs')
-rw-r--r--helix-term/src/commands.rs71
1 files changed, 44 insertions, 27 deletions
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs
index a8610223..1243a86f 100644
--- a/helix-term/src/commands.rs
+++ b/helix-term/src/commands.rs
@@ -11,6 +11,7 @@ use helix_core::{
use helix_view::{
document::{IndentStyle, Mode},
+ input::{KeyCode, KeyEvent},
view::{View, PADDING},
Document, DocumentId, Editor, ViewId,
};
@@ -38,8 +39,8 @@ use std::{
path::{Path, PathBuf},
};
-use crossterm::event::{KeyCode, KeyEvent};
use once_cell::sync::Lazy;
+use serde::de::{self, Deserialize, Deserializer};
pub struct Context<'a> {
pub selected_register: helix_view::RegisterSelection,
@@ -252,6 +253,48 @@ impl Command {
);
}
+impl fmt::Debug for Command {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ let Command(name, _) = self;
+ f.debug_tuple("Command").field(name).finish()
+ }
+}
+
+impl fmt::Display for Command {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ let Command(name, _) = self;
+ f.write_str(name)
+ }
+}
+
+impl std::str::FromStr for Command {
+ type Err = anyhow::Error;
+
+ fn from_str(s: &str) -> Result<Self, Self::Err> {
+ Command::COMMAND_LIST
+ .iter()
+ .copied()
+ .find(|cmd| cmd.0 == s)
+ .ok_or_else(|| anyhow!("No command named '{}'", s))
+ }
+}
+
+impl<'de> Deserialize<'de> for Command {
+ 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 PartialEq for Command {
+ fn eq(&self, other: &Self) -> bool {
+ self.name() == other.name()
+ }
+}
+
fn move_char_left(cx: &mut Context) {
let count = cx.count();
let (view, doc) = current!(cx.editor);
@@ -3042,29 +3085,3 @@ fn right_bracket_mode(cx: &mut Context) {
}
})
}
-
-impl fmt::Display for Command {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- let Command(name, _) = self;
- f.write_str(name)
- }
-}
-
-impl std::str::FromStr for Command {
- type Err = anyhow::Error;
-
- fn from_str(s: &str) -> Result<Self, Self::Err> {
- Command::COMMAND_LIST
- .iter()
- .copied()
- .find(|cmd| cmd.0 == s)
- .ok_or_else(|| anyhow!("No command named '{}'", s))
- }
-}
-
-impl fmt::Debug for Command {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- let Command(name, _) = self;
- f.debug_tuple("Command").field(name).finish()
- }
-}