Skip to content

Commit

Permalink
Fix previous broken refactor key into helix-view
Browse files Browse the repository at this point in the history
Need to be used for autoinfo

Revert "Revert "Refactor key into helix-view""

This reverts commit 10f9f72.
  • Loading branch information
pickfire committed Jun 22, 2021
1 parent 39d5921 commit 72a8533
Show file tree
Hide file tree
Showing 9 changed files with 433 additions and 407 deletions.
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)));
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 @@ -14,6 +14,7 @@ use helix_core::{

use helix_view::{
document::{IndentStyle, Mode},
input::{KeyCode, KeyEvent},
view::{View, PADDING},
Document, DocumentId, Editor, ViewId,
};
Expand Down Expand Up @@ -41,8 +42,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 @@ -3394,29 +3437,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,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()
}
);
}
Loading

0 comments on commit 72a8533

Please sign in to comment.