Skip to content

Commit

Permalink
Merge branch 'master' into config
Browse files Browse the repository at this point in the history
  • Loading branch information
vv9k authored Jun 19, 2021
2 parents 1e78618 + 2cbec2b commit 6685f4b
Show file tree
Hide file tree
Showing 10 changed files with 402 additions and 429 deletions.
26 changes: 13 additions & 13 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use helix_core::syntax;
use helix_lsp::{lsp, LspProgressMap};
use helix_view::{document::Mode, theme, Document, Editor, Theme, View};

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

use log::{error, info};

Expand Down Expand Up @@ -79,7 +79,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));
let mut editor_view = Box::new(ui::EditorView::new(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 @@ -11,6 +11,7 @@ use helix_core::{

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

use serde::{de::Error as SerdeError, Deserialize, Serialize};

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

#[derive(Debug, PartialEq, Deserialize)]
pub struct GlobalConfig {
pub theme: Option<String>,
pub lsp_progress: bool,
Expand All @@ -19,37 +18,50 @@ impl Default for GlobalConfig {
}
}

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

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

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 {
theme: config.theme,
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),
})
}
let sample_keymaps = r#"
[keys.insert]
y = "move_line_down"
S-C-a = "delete_selection"
[keys.normal]
A-F12 = "move_next_word_end"
"#;

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,
},
})
}
);
}
Loading

0 comments on commit 6685f4b

Please sign in to comment.