Skip to content

Commit

Permalink
Revert "Refactor key into helix-view"
Browse files Browse the repository at this point in the history
Did not use defaults when custom keymap was used

This reverts commit ca806d4.
  • Loading branch information
archseer committed Jun 19, 2021
1 parent 11f20af commit 10f9f72
Show file tree
Hide file tree
Showing 9 changed files with 414 additions and 388 deletions.
4 changes: 2 additions & 2 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use helix_lsp::{lsp, LspProgressMap};
use helix_view::{document::Mode, Document, Editor, Theme, View};

use crate::{args::Args, compositor::Compositor, config::Config, ui};
use crate::{args::Args, compositor::Compositor, config::Config, keymap::Keymaps, ui};

use log::{error, info};

Expand Down Expand Up @@ -49,7 +49,7 @@ impl Application {
let size = compositor.size();
let mut editor = Editor::new(size);

let mut editor_view = Box::new(ui::EditorView::new(config.keys));
let mut editor_view = Box::new(ui::EditorView::new(config.keymaps));
compositor.push(editor_view);

if !args.files.is_empty() {
Expand Down
71 changes: 27 additions & 44 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use helix_core::{

use helix_view::{
document::{IndentStyle, Mode},
input::{KeyCode, KeyEvent},
view::{View, PADDING},
Document, DocumentId, Editor, ViewId,
};
Expand Down Expand Up @@ -39,8 +38,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,
Expand Down Expand Up @@ -253,48 +252,6 @@ 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);
Expand Down Expand Up @@ -3085,3 +3042,29 @@ 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()
}
}
76 changes: 31 additions & 45 deletions helix-term/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use serde::Deserialize;
use anyhow::{Error, Result};
use std::{collections::HashMap, str::FromStr};

use crate::commands::Command;
use crate::keymap::Keymaps;
use serde::{de::Error as SerdeError, Deserialize, Serialize};

use crate::keymap::{parse_keymaps, Keymaps};

#[derive(Debug, PartialEq, Deserialize)]
pub struct GlobalConfig {
pub lsp_progress: bool,
}
Expand All @@ -14,50 +15,35 @@ impl Default for GlobalConfig {
}
}

#[derive(Debug, Default, PartialEq, Deserialize)]
#[serde(default)]
#[derive(Default)]
pub struct Config {
pub global: GlobalConfig,
pub keys: Keymaps,
pub keymaps: Keymaps,
}

#[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"
"#;
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
struct TomlConfig {
lsp_progress: Option<bool>,
keys: Option<HashMap<String, HashMap<String, String>>>,
}

assert_eq!(
toml::from_str::<Config>(sample_keymaps).unwrap(),
Config {
global: Default::default(),
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,
},
})
}
);
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 {
global: GlobalConfig {
lsp_progress: config.lsp_progress.unwrap_or(true),
},
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),
})
}
}
Loading

0 comments on commit 10f9f72

Please sign in to comment.