Skip to content

Commit

Permalink
Reduce calculation and improve pattern in infobox
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
pickfire committed Jul 2, 2021
1 parent 85d2ff5 commit 7515053
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 18 deletions.
21 changes: 10 additions & 11 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ use movement::Movement;

use crate::{
compositor::{self, Component, Compositor},
key,
ui::{self, Picker, Popup, Prompt, PromptEvent},
};

Expand All @@ -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> {
Expand Down Expand Up @@ -3382,13 +3381,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>"]
Expand All @@ -3407,12 +3404,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);
Expand All @@ -3421,7 +3420,7 @@ macro_rules! mode_info {
}

mode_info! {
space_mode, "space mode",
space_mode, SPACE_MODE, "space mode",
/// file picker
"f" => file_picker,
/// buffer picker
Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use helix_core::hashmap;
use helix_view::{
document::Mode,
input::KeyEvent,
keyboard::{KeyCode, KeyModifiers},
};
use serde::Deserialize;
use std::{
Expand Down Expand Up @@ -352,6 +351,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! {
Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/ui/info.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
2 changes: 1 addition & 1 deletion helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct Editor {
pub syn_loader: Arc<syntax::Loader>,
pub theme_loader: Arc<theme::Loader>,

pub autoinfo: Option<Info>,
pub autoinfo: Option<&'static Info>,
pub status_msg: Option<(String, Severity)>,
}

Expand Down
8 changes: 4 additions & 4 deletions helix-view/src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct Info {
}

impl Info {
pub fn key(title: &'static str, body: Vec<(Vec<KeyEvent>, &'static str)>) -> Info {
pub fn key(title: &'static str, body: Vec<(&[KeyEvent], &'static str)>) -> Info {
let keymaps_width: u16 = body
.iter()
.map(|r| r.0.iter().map(|e| e.width() as u16 + 2).sum::<u16>() - 2)
Expand All @@ -25,11 +25,11 @@ impl Info {
let mut text = String::new();
let mut width = 0;
let height = body.len() as u16;
for (mut keyevents, desc) in body {
let keyevent = keyevents.remove(0);
for (keyevents, desc) in body {
let keyevent = keyevents[0];
let mut left = keymaps_width - keyevent.width() as u16;
write!(text, "{}", keyevent).ok();
for keyevent in keyevents {
for keyevent in &keyevents[1..] {
write!(text, ", {}", keyevent).ok();
left -= 2 + keyevent.width() as u16;
}
Expand Down

0 comments on commit 7515053

Please sign in to comment.