on master help sign in
Reduce calculation and improve pattern in infobox
5977b07Ivan Tham
helix-term/src/commands.rs+10−11
@@ -34,7 +34,6 @@ use movement::Movement;
34
34
35use crate::{
35use crate::{
36 compositor::{self, Component, Compositor},
36 compositor::{self, Component, Compositor},
37− key,
38 ui::{self, Picker, Popup, Prompt, PromptEvent},
37 ui::{self, Picker, Popup, Prompt, PromptEvent},
39};
38};
40
39
@@ -48,7 +47,7 @@ use std::{
48 path::{Path, PathBuf},
47 path::{Path, PathBuf},
49};
48};
50
49
51−use once_cell::sync::Lazy;
50+use once_cell::sync::{Lazy, OnceCell};
52use serde::de::{self, Deserialize, Deserializer};
51use serde::de::{self, Deserialize, Deserializer};
53
52
54pub struct Context<'a> {
53pub struct Context<'a> {
@@ -3414,13 +3413,11 @@ fn select_register(cx: &mut Context) {
3414}
3413}
3415
3414
3416macro_rules! mode_info {
3415macro_rules! mode_info {
3417− // TODO: how to use one expr for both pat and expr?
3416+ // TODO: reuse $mode for $stat
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
3420 (@join $first:expr $(,$rest:expr)*) => {
3417 (@join $first:expr $(,$rest:expr)*) => {
3421 concat!($first, $(", ", $rest),*)
3418 concat!($first, $(", ", $rest),*)
3422 };
3419 };
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),+,} => {
3424 #[doc = $name]
3421 #[doc = $name]
3425 #[doc = ""]
3422 #[doc = ""]
3426 #[doc = "<table><tr><th>key</th><th>desc</th></tr><tbody>"]
3423 #[doc = "<table><tr><th>key</th><th>desc</th></tr><tbody>"]
@@ -3439,12 +3436,14 @@ macro_rules! mode_info {
3439 )+
3436 )+
3440 #[doc = "</tbody></table>"]
3437 #[doc = "</tbody></table>"]
3441 pub fn $mode(cx: &mut Context) {
3438 pub fn $mode(cx: &mut Context) {
3439+ static $stat: OnceCell<Info> = OnceCell::new();
3442− cx.editor.autoinfo = Some(Info::key(
3440+ cx.editor.autoinfo = Some($stat.get_or_init(|| Info::key(
3443 $name,
3441 $name,
3444− vec![$((vec![$($key.parse().unwrap()),+], $desc)),+],
3442+ vec![$((&[$($key.parse().unwrap()),+], $desc)),+],
3445− ));
3443+ )));
3446 use helix_core::hashmap;
3444 use helix_core::hashmap;
3445+ // TODO: try and convert this to match later
3447− let mut map = hashmap! {
3446+ let map = hashmap! {
3448 $($($key.parse::<KeyEvent>().unwrap() => $func as for<'r, 's> fn(&'r mut Context<'s>)),+),*
3447 $($($key.parse::<KeyEvent>().unwrap() => $func as for<'r, 's> fn(&'r mut Context<'s>)),+),*
3449 };
3448 };
3450 cx.on_next_key_mode(map);
3449 cx.on_next_key_mode(map);
@@ -3453,7 +3452,7 @@ macro_rules! mode_info {
3453}
3452}
3454
3453
3455mode_info! {
3454mode_info! {
3456− space_mode, "space mode",
3455+ space_mode, SPACE_MODE, "space mode",
3457 /// file picker
3456 /// file picker
3458 "f" => file_picker,
3457 "f" => file_picker,
3459 /// buffer picker
3458 /// buffer picker
helix-term/src/keymap.rs+2−5
@@ -1,11 +1,7 @@
1pub use crate::commands::Command;
1pub use crate::commands::Command;
2use crate::config::Config;
2use crate::config::Config;
3use helix_core::hashmap;
3use helix_core::hashmap;
4−use helix_view::{
4+use helix_view::{document::Mode, input::KeyEvent};
5− document::Mode,
6− input::KeyEvent,
7− keyboard::{KeyCode, KeyModifiers},
8−};
9use serde::Deserialize;
5use serde::Deserialize;
10use std::{
6use std::{
11 collections::HashMap,
7 collections::HashMap,
@@ -352,6 +348,7 @@ pub fn merge_keys(mut config: Config) -> Config {
352
348
353#[test]
349#[test]
354fn merge_partial_keys() {
350fn merge_partial_keys() {
351+ use helix_view::keyboard::{KeyCode, KeyModifiers};
355 let config = Config {
352 let config = Config {
356 keys: Keymaps(hashmap! {
353 keys: Keymaps(hashmap! {
357 Mode::Normal => hashmap! {
354 Mode::Normal => hashmap! {
helix-term/src/ui/info.rs+1−1
@@ -1,5 +1,5 @@
1use crate::compositor::{Component, Context};
1use crate::compositor::{Component, Context};
2−use helix_view::graphics::{Margin, Rect, Style};
2+use helix_view::graphics::Rect;
3use helix_view::info::Info;
3use helix_view::info::Info;
4use tui::buffer::Buffer as Surface;
4use tui::buffer::Buffer as Surface;
5use tui::widgets::{Block, Borders, Widget};
5use tui::widgets::{Block, Borders, Widget};
helix-view/src/editor.rs+1−1
@@ -33,7 +33,7 @@ pub struct Editor {
33 pub syn_loader: Arc<syntax::Loader>,
33 pub syn_loader: Arc<syntax::Loader>,
34 pub theme_loader: Arc<theme::Loader>,
34 pub theme_loader: Arc<theme::Loader>,
35
35
36− pub autoinfo: Option<Info>,
36+ pub autoinfo: Option<&'static Info>,
37 pub status_msg: Option<(String, Severity)>,
37 pub status_msg: Option<(String, Severity)>,
38}
38}
39
39
helix-view/src/info.rs+4−4
@@ -16,7 +16,7 @@ pub struct Info {
16}
16}
17
17
18impl Info {
18impl 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 {
20 let keymaps_width: u16 = body
20 let keymaps_width: u16 = body
21 .iter()
21 .iter()
22 .map(|r| r.0.iter().map(|e| e.width() as u16 + 2).sum::<u16>() - 2)
22 .map(|r| r.0.iter().map(|e| e.width() as u16 + 2).sum::<u16>() - 2)
@@ -25,11 +25,11 @@ impl Info {
25 let mut text = String::new();
25 let mut text = String::new();
26 let mut width = 0;
26 let mut width = 0;
27 let height = body.len() as u16;
27 let height = body.len() as u16;
28− for (mut keyevents, desc) in body {
28+ for (keyevents, desc) in body {
29− let keyevent = keyevents.remove(0);
29+ let keyevent = keyevents[0];
30 let mut left = keymaps_width - keyevent.width() as u16;
30 let mut left = keymaps_width - keyevent.width() as u16;
31 write!(text, "{}", keyevent).ok();
31 write!(text, "{}", keyevent).ok();
32− for keyevent in keyevents {
32+ for keyevent in &keyevents[1..] {
33 write!(text, ", {}", keyevent).ok();
33 write!(text, ", {}", keyevent).ok();
34 left -= 2 + keyevent.width() as u16;
34 left -= 2 + keyevent.width() as u16;
35 }
35 }