on master help sign in
Reduce calculation and improve pattern in infobox

- switch to use static OnceCell to calculate Info once - pass Vec<(&[KeyEvent], &str)> rather than Vec<(Vec<KeyEvent>, &str)> - expr -> tt to allow using | as separator, make it more like match

commit5977b07e197cc6ef9051dd34a28b9fe28e01e966 authorIvan Tham <pickfire@riseup.net> dateFri 2 Jul 2021, 09:46 (5 years ago) parent64f83df
5 files changed+18−22
helix-term/src/commands.rs+10−11 helix-term/src/keymap.rs+2−5 helix-term/src/ui/info.rs+1−1 helix-view/src/editor.rs+1−1 helix-view/src/info.rs+4−4
helix-term/src/commands.rs+10−11
@@ -34,7 +34,6 @@ use movement::Movement;
3434
3535use crate::{
3636 compositor::{self, Component, Compositor},
37− key,
3837 ui::{self, Picker, Popup, Prompt, PromptEvent},
3938};
4039
@@ -48,7 +47,7 @@ use std::{
4847 path::{Path, PathBuf},
4948};
5049
51−use once_cell::sync::Lazy;
50+use once_cell::sync::{Lazy, OnceCell};
5251use serde::de::{self, Deserialize, Deserializer};
5352
5453pub struct Context<'a> {
@@ -3414,13 +3413,11 @@ fn select_register(cx: &mut Context) {
34143413}
34153414
34163415macro_rules! mode_info {
3417− // TODO: how to use one expr for both pat and expr?
3418− // TODO: how to use replaced function name as str at compile time?
3419− // TODO: extend to support multiple keys, but first solve the other two
3416+ // TODO: reuse $mode for $stat
34203417 (@join $first:expr $(,$rest:expr)*) => {
34213418 concat!($first, $(", ", $rest),*)
34223419 };
3423− {$mode:ident, $name:literal, $(#[doc = $desc:literal] $($key:expr),+ => $func:expr),+,} => {
3420+ {$mode:ident, $stat:ident, $name:literal, $(#[doc = $desc:literal] $($key:tt)|+ => $func:expr),+,} => {
34243421 #[doc = $name]
34253422 #[doc = ""]
34263423 #[doc = "<table><tr><th>key</th><th>desc</th></tr><tbody>"]
@@ -3439,12 +3436,14 @@ macro_rules! mode_info {
34393436 )+
34403437 #[doc = "</tbody></table>"]
34413438 pub fn $mode(cx: &mut Context) {
3442− cx.editor.autoinfo = Some(Info::key(
3439+ static $stat: OnceCell<Info> = OnceCell::new();
3440+ cx.editor.autoinfo = Some($stat.get_or_init(|| Info::key(
34433441 $name,
3444− vec![$((vec![$($key.parse().unwrap()),+], $desc)),+],
3445− ));
3442+ vec![$((&[$($key.parse().unwrap()),+], $desc)),+],
3443+ )));
34463444 use helix_core::hashmap;
3447− let mut map = hashmap! {
3445+ // TODO: try and convert this to match later
3446+ let map = hashmap! {
34483447 $($($key.parse::<KeyEvent>().unwrap() => $func as for<'r, 's> fn(&'r mut Context<'s>)),+),*
34493448 };
34503449 cx.on_next_key_mode(map);
@@ -3453,7 +3452,7 @@ macro_rules! mode_info {
34533452}
34543453
34553454mode_info! {
3456− space_mode, "space mode",
3455+ space_mode, SPACE_MODE, "space mode",
34573456 /// file picker
34583457 "f" => file_picker,
34593458 /// buffer picker
helix-term/src/keymap.rs+2−5
@@ -1,11 +1,7 @@
11pub use crate::commands::Command;
22use crate::config::Config;
33use helix_core::hashmap;
4−use helix_view::{
5− document::Mode,
6− input::KeyEvent,
7− keyboard::{KeyCode, KeyModifiers},
8−};
4+use helix_view::{document::Mode, input::KeyEvent};
95use serde::Deserialize;
106use std::{
117 collections::HashMap,
@@ -352,6 +348,7 @@ pub fn merge_keys(mut config: Config) -> Config {
352348
353349#[test]
354350fn merge_partial_keys() {
351+ use helix_view::keyboard::{KeyCode, KeyModifiers};
355352 let config = Config {
356353 keys: Keymaps(hashmap! {
357354 Mode::Normal => hashmap! {
helix-term/src/ui/info.rs+1−1
@@ -1,5 +1,5 @@
11use crate::compositor::{Component, Context};
2−use helix_view::graphics::{Margin, Rect, Style};
2+use helix_view::graphics::Rect;
33use helix_view::info::Info;
44use tui::buffer::Buffer as Surface;
55use tui::widgets::{Block, Borders, Widget};
helix-view/src/editor.rs+1−1
@@ -33,7 +33,7 @@ pub struct Editor {
3333 pub syn_loader: Arc<syntax::Loader>,
3434 pub theme_loader: Arc<theme::Loader>,
3535
36− pub autoinfo: Option<Info>,
36+ pub autoinfo: Option<&'static Info>,
3737 pub status_msg: Option<(String, Severity)>,
3838}
3939
helix-view/src/info.rs+4−4
@@ -16,7 +16,7 @@ pub struct Info {
1616}
1717
1818impl Info {
19− pub fn key(title: &'static str, body: Vec<(Vec<KeyEvent>, &'static str)>) -> Info {
19+ pub fn key(title: &'static str, body: Vec<(&[KeyEvent], &'static str)>) -> Info {
2020 let keymaps_width: u16 = body
2121 .iter()
2222 .map(|r| r.0.iter().map(|e| e.width() as u16 + 2).sum::<u16>() - 2)
@@ -25,11 +25,11 @@ impl Info {
2525 let mut text = String::new();
2626 let mut width = 0;
2727 let height = body.len() as u16;
28− for (mut keyevents, desc) in body {
29− let keyevent = keyevents.remove(0);
28+ for (keyevents, desc) in body {
29+ let keyevent = keyevents[0];
3030 let mut left = keymaps_width - keyevent.width() as u16;
3131 write!(text, "{}", keyevent).ok();
32− for keyevent in keyevents {
32+ for keyevent in &keyevents[1..] {
3333 write!(text, ", {}", keyevent).ok();
3434 left -= 2 + keyevent.width() as u16;
3535 }