From 5977b07e197cc6ef9051dd34a28b9fe28e01e966 Mon Sep 17 00:00:00 2001
From: Ivan Tham
Date: Fri, 2 Jul 2021 09:46:28 +0800
Subject: 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
---
 helix-term/src/commands.rs | 21 ++++++++++-----------
 helix-term/src/keymap.rs   |  7 ++-----
 helix-term/src/ui/info.rs  |  2 +-
 3 files changed, 13 insertions(+), 17 deletions(-)

(limited to 'helix-term')

diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs
index b6f3c11f..351ec1fb 100644
--- a/helix-term/src/commands.rs
+++ b/helix-term/src/commands.rs
@@ -34,7 +34,6 @@ use movement::Movement;
 
 use crate::{
     compositor::{self, Component, Compositor},
-    key,
     ui::{self, Picker, Popup, Prompt, PromptEvent},
 };
 
@@ -48,7 +47,7 @@ use std::{
     path::{Path, PathBuf},
 };
 
-use once_cell::sync::Lazy;
+use once_cell::sync::{Lazy, OnceCell};
 use serde::de::{self, Deserialize, Deserializer};
 
 pub struct Context<'a> {
@@ -3414,13 +3413,11 @@ fn select_register(cx: &mut Context) {
 }
 
 macro_rules! mode_info {
-    // TODO: how to use one expr for both pat and expr?
-    // TODO: how to use replaced function name as str at compile time?
-    // TODO: extend to support multiple keys, but first solve the other two
+    // TODO: reuse $mode for $stat
     (@join $first:expr $(,$rest:expr)*) => {
         concat!($first, $(", ", $rest),*)
     };
-    {$mode:ident, $name:literal, $(#[doc = $desc:literal] $($key:expr),+ => $func:expr),+,} => {
+    {$mode:ident, $stat:ident, $name:literal, $(#[doc = $desc:literal] $($key:tt)|+ => $func:expr),+,} => {
         #[doc = $name]
         #[doc = ""]
         #[doc = "<table><tr><th>key</th><th>desc</th></tr><tbody>"]
@@ -3439,12 +3436,14 @@ macro_rules! mode_info {
         )+
         #[doc = "</tbody></table>"]
         pub fn $mode(cx: &mut Context) {
-            cx.editor.autoinfo = Some(Info::key(
+            static $stat: OnceCell<Info> = OnceCell::new();
+            cx.editor.autoinfo = Some($stat.get_or_init(|| Info::key(
                 $name,
-                vec![$((vec![$($key.parse().unwrap()),+], $desc)),+],
-            ));
+                vec![$((&[$($key.parse().unwrap()),+], $desc)),+],
+            )));
             use helix_core::hashmap;
-            let mut map = hashmap! {
+            // TODO: try and convert this to match later
+            let map = hashmap! {
                 $($($key.parse::<KeyEvent>().unwrap() => $func as for<'r, 's> fn(&'r mut Context<'s>)),+),*
             };
             cx.on_next_key_mode(map);
@@ -3453,7 +3452,7 @@ macro_rules! mode_info {
 }
 
 mode_info! {
-    space_mode, "space mode",
+    space_mode, SPACE_MODE, "space mode",
     /// file picker
     "f" => file_picker,
     /// buffer picker
diff --git a/helix-term/src/keymap.rs b/helix-term/src/keymap.rs
index ef4a2138..3cd540ea 100644
--- a/helix-term/src/keymap.rs
+++ b/helix-term/src/keymap.rs
@@ -1,11 +1,7 @@
 pub use crate::commands::Command;
 use crate::config::Config;
 use helix_core::hashmap;
-use helix_view::{
-    document::Mode,
-    input::KeyEvent,
-    keyboard::{KeyCode, KeyModifiers},
-};
+use helix_view::{document::Mode, input::KeyEvent};
 use serde::Deserialize;
 use std::{
     collections::HashMap,
@@ -352,6 +348,7 @@ pub fn merge_keys(mut config: Config) -> Config {
 
 #[test]
 fn merge_partial_keys() {
+    use helix_view::keyboard::{KeyCode, KeyModifiers};
     let config = Config {
         keys: Keymaps(hashmap! {
             Mode::Normal => hashmap! {
diff --git a/helix-term/src/ui/info.rs b/helix-term/src/ui/info.rs
index 87c2c213..c6f8db43 100644
--- a/helix-term/src/ui/info.rs
+++ b/helix-term/src/ui/info.rs
@@ -1,5 +1,5 @@
 use crate::compositor::{Component, Context};
-use helix_view::graphics::{Margin, Rect, Style};
+use helix_view::graphics::Rect;
 use helix_view::info::Info;
 use tui::buffer::Buffer as Surface;
 use tui::widgets::{Block, Borders, Widget};
-- 
cgit v1.2.3-70-g09d2