Skip to content

Commit

Permalink
Move ui, keymap & commands to helix-view
Browse files Browse the repository at this point in the history
  • Loading branch information
archseer committed May 1, 2022
1 parent 11b8f06 commit 1aa2b02
Show file tree
Hide file tree
Showing 29 changed files with 398 additions and 384 deletions.
15 changes: 8 additions & 7 deletions Cargo.lock

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

15 changes: 0 additions & 15 deletions helix-term/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,12 @@ fern = "0.6"
chrono = { version = "0.4", default-features = false, features = ["clock"] }
log = "0.4"

# File picker
fuzzy-matcher = "0.3"
ignore = "0.4"
# markdown doc rendering
pulldown-cmark = { version = "0.9", default-features = false }
# file type detection
content_inspector = "0.2.4"

# config
toml = "0.5"

serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }

# ripgrep for global search
grep-regex = "0.1.9"
grep-searcher = "0.1.8"

# Remove once retain_mut lands in stable rust
retain_mut = "0.1.7"

[target.'cfg(not(windows))'.dependencies] # https://github.com/vorner/signal-hook/issues/100
signal-hook-tokio = { version = "0.3", features = ["futures-v0_3"] }

Expand Down
18 changes: 10 additions & 8 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ use helix_core::{
pos_at_coords, syntax, Selection,
};

#[cfg(feature = "lsp")]
use crate::commands::apply_workspace_edit;
#[cfg(feature = "lsp")]
use helix_lsp::{lsp, util::lsp_pos_to_pos, LspProgressMap};
#[cfg(feature = "lsp")]
use helix_view::commands::apply_workspace_edit;
#[cfg(feature = "lsp")]
use serde_json::json;

use helix_view::{align_view, editor::ConfigEvent, graphics::Rect, theme, Align, Editor};
use helix_view::{
align_view, editor::ConfigEvent, graphics::Rect, theme, true_color, Align, Editor,
};

use crate::{args::Args, config::Config};

use crate::{
args::Args,
config::Config,
use helix_view::{
keymap::Keymaps,
ui::{self, overlay::overlayed},
};
Expand Down Expand Up @@ -96,7 +98,7 @@ impl Application {
&helix_loader::runtime_dir(),
));

let true_color = config.editor.true_color || crate::true_color();
let true_color = config.editor.true_color || true_color();
let theme = config
.theme
.as_ref()
Expand Down Expand Up @@ -358,7 +360,7 @@ impl Application {
}

fn true_color(&self) -> bool {
self.config.load().editor.true_color || crate::true_color()
self.config.load().editor.true_color || true_color()
}

#[cfg(windows)]
Expand Down
37 changes: 2 additions & 35 deletions helix-term/src/args.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Result;
use helix_core::Position;
use std::path::{Path, PathBuf};
use helix_view::args::parse_file;
use std::path::PathBuf;

#[derive(Default)]
pub struct Args {
Expand Down Expand Up @@ -65,37 +66,3 @@ impl Args {
Ok(args)
}
}

/// Parse arg into [`PathBuf`] and position.
pub(crate) fn parse_file(s: &str) -> (PathBuf, Position) {
let def = || (PathBuf::from(s), Position::default());
if Path::new(s).exists() {
return def();
}
split_path_row_col(s)
.or_else(|| split_path_row(s))
.unwrap_or_else(def)
}

/// Split file.rs:10:2 into [`PathBuf`], row and col.
///
/// Does not validate if file.rs is a file or directory.
fn split_path_row_col(s: &str) -> Option<(PathBuf, Position)> {
let mut s = s.rsplitn(3, ':');
let col: usize = s.next()?.parse().ok()?;
let row: usize = s.next()?.parse().ok()?;
let path = s.next()?.into();
let pos = Position::new(row.saturating_sub(1), col.saturating_sub(1));
Some((path, pos))
}

/// Split file.rs:10 into [`PathBuf`] and row.
///
/// Does not validate if file.rs is a file or directory.
fn split_path_row(s: &str) -> Option<(PathBuf, Position)> {
let (path, row) = s.rsplit_once(':')?;
let row: usize = row.parse().ok()?;
let path = path.into();
let pos = Position::new(row.saturating_sub(1), 0);
Some((path, pos))
}
114 changes: 111 additions & 3 deletions helix-term/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::keymap::{default::default, merge_keys, Keymap};
use helix_view::document::Mode;
use helix_view::keymap::{default::default, Keymap};
use serde::Deserialize;
use std::collections::HashMap;
use std::fmt::Display;
Expand Down Expand Up @@ -27,6 +27,15 @@ impl Default for Config {
}
}

/// Merge default config keys with user overwritten keys for custom user config.
pub fn merge_keys(mut config: Config) -> Config {
let mut delta = std::mem::replace(&mut config.keys, default());
for (mode, keys) in &mut config.keys {
keys.merge(delta.remove(mode).unwrap_or_default())
}
config
}

#[derive(Debug)]
pub enum ConfigLoadError {
BadConfig(TomlError),
Expand Down Expand Up @@ -63,10 +72,9 @@ mod tests {

#[test]
fn parsing_keymaps_config_file() {
use crate::keymap;
use crate::keymap::Keymap;
use helix_core::hashmap;
use helix_view::document::Mode;
use helix_view::keymap::{self, Keymap};

let sample_keymaps = r#"
[keys.insert]
Expand Down Expand Up @@ -104,4 +112,104 @@ mod tests {
let default_keys = Config::default().keys;
assert_eq!(default_keys, default());
}

use arc_swap::access::Constant;
use helix_core::hashmap;

#[test]
fn merge_partial_keys() {
let config = Config {
keys: hashmap! {
Mode::Normal => Keymap::new(
keymap!({ "Normal mode"
"i" => normal_mode,
"无" => insert_mode,
"z" => jump_backward,
"g" => { "Merge into goto mode"
"$" => goto_line_end,
"g" => delete_char_forward,
},
})
)
},
..Default::default()
};
let mut merged_config = merge_keys(config.clone());
assert_ne!(config, merged_config);

let mut keymap = Keymaps::new(Box::new(Constant(merged_config.keys.clone())));
assert_eq!(
keymap.get(Mode::Normal, key!('i')),
KeymapResult::Matched(MappableCommand::normal_mode),
"Leaf should replace leaf"
);
assert_eq!(
keymap.get(Mode::Normal, key!('无')),
KeymapResult::Matched(MappableCommand::insert_mode),
"New leaf should be present in merged keymap"
);
// Assumes that z is a node in the default keymap
assert_eq!(
keymap.get(Mode::Normal, key!('z')),
KeymapResult::Matched(MappableCommand::jump_backward),
"Leaf should replace node"
);

let keymap = merged_config.keys.get_mut(&Mode::Normal).unwrap();
// Assumes that `g` is a node in default keymap
assert_eq!(
keymap.root().search(&[key!('g'), key!('$')]).unwrap(),
&KeyTrie::Leaf(MappableCommand::goto_line_end),
"Leaf should be present in merged subnode"
);
// Assumes that `gg` is in default keymap
assert_eq!(
keymap.root().search(&[key!('g'), key!('g')]).unwrap(),
&KeyTrie::Leaf(MappableCommand::delete_char_forward),
"Leaf should replace old leaf in merged subnode"
);
// Assumes that `ge` is in default keymap
assert_eq!(
keymap.root().search(&[key!('g'), key!('e')]).unwrap(),
&KeyTrie::Leaf(MappableCommand::goto_last_line),
"Old leaves in subnode should be present in merged node"
);

assert!(merged_config.keys.get(&Mode::Normal).unwrap().len() > 1);
assert!(merged_config.keys.get(&Mode::Insert).unwrap().len() > 0);
}

#[test]
fn order_should_be_set() {
let config = Config {
keys: hashmap! {
Mode::Normal => Keymap::new(
keymap!({ "Normal mode"
"space" => { ""
"s" => { ""
"v" => vsplit,
"c" => hsplit,
},
},
})
)
},
..Default::default()
};
let mut merged_config = merge_keys(config.clone());
assert_ne!(config, merged_config);
let keymap = merged_config.keys.get_mut(&Mode::Normal).unwrap();
// Make sure mapping works
assert_eq!(
keymap
.root()
.search(&[key!(' '), key!('s'), key!('v')])
.unwrap(),
&KeyTrie::Leaf(MappableCommand::vsplit),
"Leaf should be present in merged subnode"
);
// Make sure an order was set during merge
let node = keymap.root().search(&[helix_view::key!(' ')]).unwrap();
assert!(!node.node().unwrap().order().is_empty())
}
}
Loading

0 comments on commit 1aa2b02

Please sign in to comment.