Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix previous broken refactor key into helix-view #345

Merged
merged 1 commit into from
Jun 24, 2021
Merged
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
5 changes: 3 additions & 2 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub struct Application {
compositor: Compositor,
editor: Editor,

// TODO should be separate to take only part of the config
config: Config,

theme_loader: Arc<theme::Loader>,
Expand All @@ -54,7 +55,7 @@ pub struct Application {
}

impl Application {
pub fn new(mut args: Args, config: Config) -> Result<Self, Error> {
pub fn new(mut args: Args, mut config: Config) -> Result<Self, Error> {
use helix_view::editor::Action;
let mut compositor = Compositor::new()?;
let size = compositor.size();
Expand Down Expand Up @@ -87,7 +88,7 @@ impl Application {

let mut editor = Editor::new(size, theme_loader.clone(), syn_loader.clone());

let mut editor_view = Box::new(ui::EditorView::new(config.keymaps.clone()));
let mut editor_view = Box::new(ui::EditorView::new(std::mem::take(&mut config.keys)));
archseer marked this conversation as resolved.
Show resolved Hide resolved
compositor.push(editor_view);

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

use helix_view::{
document::{IndentStyle, Mode},
input::{KeyCode, KeyEvent},
view::{View, PADDING},
Document, DocumentId, Editor, ViewId,
};
Expand Down Expand Up @@ -42,8 +43,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 @@ -260,6 +261,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);
Expand Down Expand Up @@ -3456,29 +3499,3 @@ fn surround_delete(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()
}
}
77 changes: 46 additions & 31 deletions helix-term/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,62 @@
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)]
#[serde(rename_all = "kebab-case")]
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()
}
);
}
Loading