on master help sign in
Show infobox with register contents
commitbf773db45171b59a758b7dabf9f10ffb8852dcf0 authorGokul Soumya <gokulps15@gmail.com> dateFri 5 Nov 2021, 00:03 (5 years ago) parent5995568
6 files changed+56−27
helix-core/src/register.rs+4 helix-term/src/commands.rs+3 helix-term/src/keymap.rs+1−2 helix-term/src/ui/editor.rs+5−7 helix-view/src/editor.rs+3 helix-view/src/info.rs+40−18
helix-core/src/register.rs+4
@@ -68,4 +68,8 @@ impl Registers {
6868 pub fn read(&self, name: char) -> Option<&[String]> {
6969 self.get(name).map(|reg| reg.read())
7070 }
71+
72+ pub fn inner(&self) -> &HashMap<char, Register> {
73+ &self.inner
74+ }
7175}
helix-term/src/commands.rs+3
@@ -20,6 +20,7 @@ use helix_view::{
2020 clipboard::ClipboardType,
2121 document::{Mode, SCRATCH_BUFFER_NAME},
2222 editor::{Action, Motion},
23+ info::Info,
2324 input::KeyEvent,
2425 keyboard::KeyCode,
2526 view::View,
@@ -5745,8 +5746,10 @@ fn wonly(cx: &mut Context) {
57455746}
57465747
57475748fn select_register(cx: &mut Context) {
5749+ cx.editor.autoinfo = Some(Info::from_registers(&cx.editor.registers));
57485750 cx.on_next_key(move |cx, event| {
57495751 if let Some(ch) = event.char() {
5752+ cx.editor.autoinfo = None;
57505753 cx.editor.selected_register = Some(ch);
57515754 }
57525755 })
helix-term/src/keymap.rs+1−2
@@ -222,9 +222,8 @@ impl KeyTrieNode {
222222 .map(|(desc, keys)| (desc.strip_prefix(&prefix).unwrap(), keys))
223223 .collect();
224224 }
225− Info::new(self.name(), body)
225+ Info::from_keymap(self.name(), body)
226226 }
227−
228227 /// Get a reference to the key trie node's order.
229228 pub fn order(&self) -> &[KeyEvent] {
230229 self.order.as_slice()
helix-term/src/ui/editor.rs+5−7
@@ -21,7 +21,6 @@ use helix_view::{
2121 document::{Mode, SCRATCH_BUFFER_NAME},
2222 editor::CursorShapeConfig,
2323 graphics::{CursorKind, Modifier, Rect, Style},
24− info::Info,
2524 input::KeyEvent,
2625 keyboard::{KeyCode, KeyModifiers},
2726 Document, Editor, Theme, View,
@@ -37,7 +36,6 @@ pub struct EditorView {
3736 last_insert: (commands::MappableCommand, Vec<KeyEvent>),
3837 pub(crate) completion: Option<Completion>,
3938 spinners: ProgressSpinners,
40− autoinfo: Option<Info>,
4139}
4240
4341impl Default for EditorView {
@@ -54,7 +52,6 @@ impl EditorView {
5452 last_insert: (commands::MappableCommand::normal_mode, Vec::new()),
5553 completion: None,
5654 spinners: ProgressSpinners::default(),
57− autoinfo: None,
5855 }
5956 }
6057
@@ -678,13 +675,13 @@ impl EditorView {
678675 cxt: &mut commands::Context,
679676 event: KeyEvent,
680677 ) -> Option<KeymapResult> {
681− self.autoinfo = None;
678+ cxt.editor.autoinfo = None;
682679 let key_result = self.keymaps.get_mut(&mode).unwrap().get(event);
683− self.autoinfo = key_result.sticky.map(|node| node.infobox());
680+ cxt.editor.autoinfo = key_result.sticky.map(|node| node.infobox());
684681
685682 match &key_result.kind {
686683 KeymapResultKind::Matched(command) => command.execute(cxt),
687− KeymapResultKind::Pending(node) => self.autoinfo = Some(node.infobox()),
684+ KeymapResultKind::Pending(node) => cxt.editor.autoinfo = Some(node.infobox()),
688685 KeymapResultKind::MatchedSequence(commands) => {
689686 for command in commands {
690687 command.execute(cxt);
@@ -1093,8 +1090,9 @@ impl Component for EditorView {
10931090 }
10941091
10951092 if cx.editor.config.auto_info {
1096− if let Some(ref mut info) = self.autoinfo {
1093+ if let Some(mut info) = cx.editor.autoinfo.take() {
10971094 info.render(area, surface, cx);
1095+ cx.editor.autoinfo = Some(info)
10981096 }
10991097 }
11001098
helix-view/src/editor.rs+3
@@ -2,6 +2,7 @@ use crate::{
22 clipboard::{get_clipboard_provider, ClipboardProvider},
33 document::{Mode, SCRATCH_BUFFER_NAME},
44 graphics::{CursorKind, Rect},
5+ info::Info,
56 input::KeyEvent,
67 theme::{self, Theme},
78 tree::{self, Tree},
@@ -243,6 +244,7 @@ pub struct Editor {
243244 pub theme_loader: Arc<theme::Loader>,
244245
245246 pub status_msg: Option<(String, Severity)>,
247+ pub autoinfo: Option<Info>,
246248
247249 pub config: Config,
248250
@@ -286,6 +288,7 @@ impl Editor {
286288 registers: Registers::default(),
287289 clipboard_provider: get_clipboard_provider(),
288290 status_msg: None,
291+ autoinfo: None,
289292 idle_timer: Box::pin(sleep(config.idle_timeout)),
290293 last_motion: None,
291294 config,
helix-view/src/info.rs+40−18
@@ -1,5 +1,5 @@
11use crate::input::KeyEvent;
2−use helix_core::unicode::width::UnicodeWidthStr;
2+use helix_core::{register::Registers, unicode::width::UnicodeWidthStr};
33use std::{collections::BTreeSet, fmt::Write};
44
55#[derive(Debug)]
@@ -16,26 +16,21 @@ pub struct Info {
1616}
1717
1818impl Info {
19− pub fn new(title: &str, body: Vec<(&str, BTreeSet<KeyEvent>)>) -> Self {
20− let body = body
21− .into_iter()
22− .map(|(desc, events)| {
23− let events = events.iter().map(ToString::to_string).collect::<Vec<_>>();
24− (desc, events.join(", "))
25− })
26− .collect::<Vec<_>>();
19+ pub fn new(title: &str, body: Vec<(String, String)>) -> Self {
20+ if body.is_empty() {
21+ return Self {
22+ title: title.to_string(),
23+ height: 1,
24+ width: title.len() as u16,
25+ text: "".to_string(),
26+ };
27+ }
2728
28− let keymaps_width = body.iter().map(|r| r.1.len()).max().unwrap();
29+ let item_width = body.iter().map(|(item, _)| item.width()).max().unwrap();
2930 let mut text = String::new();
3031
31− for (desc, keyevents) in &body {
32− let _ = writeln!(
33− text,
34− "{:width$} {}",
35− keyevents,
36− desc,
37− width = keymaps_width
38− );
32+ for (item, desc) in &body {
33+ let _ = writeln!(text, "{:width$} {}", item, desc, width = item_width);
3934 }
4035
4136 Self {
@@ -45,4 +40,31 @@ impl Info {
4540 text,
4641 }
4742 }
43+
44+ pub fn from_keymap(title: &str, body: Vec<(&str, BTreeSet<KeyEvent>)>) -> Self {
45+ let body = body
46+ .into_iter()
47+ .map(|(desc, events)| {
48+ let events = events.iter().map(ToString::to_string).collect::<Vec<_>>();
49+ (events.join(", "), desc.to_string())
50+ })
51+ .collect();
52+
53+ Self::new(title, body)
54+ }
55+
56+ pub fn from_registers(registers: &Registers) -> Self {
57+ let body = registers
58+ .inner()
59+ .iter()
60+ .map(|(ch, reg)| {
61+ let content = reg.read().join(", ").trim_end().to_string();
62+ (ch.to_string(), content)
63+ })
64+ .collect();
65+
66+ let mut infobox = Self::new("Registers", body);
67+ infobox.width = 30; // copied content could be very long
68+ infobox
69+ }
4870}