aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBob2022-05-20 01:27:59 +0000
committerGitHub2022-05-20 01:27:59 +0000
commit6462542fc50078e41074b70d0e70d52f351959fa (patch)
treed2ab7466f02acb2c8f6cc4eb2337724c1b402c14
parent8958bf0a926a6e6afc63f0c59f3fa6761f5da709 (diff)
support insert register in prompt (#2458)
* support insert register in prompt * use next_char_handler instead of a flag * Fix clippy issue * show autoinfo when inserting register * Revert "show autoinfo when inserting register" This reverts commit 5488344de1c607d44bdf8693287a85b92cb32518. * use completion instead of autoinfo autoinfo is overlapped when using prompt * recalculate_completion after inserting register * Update helix-term/src/ui/prompt.rs Co-authored-by: Ivan Tham <pickfire@riseup.net> Co-authored-by: Ivan Tham <pickfire@riseup.net>
-rw-r--r--book/src/keymap.md1
-rw-r--r--helix-term/src/ui/prompt.rs39
2 files changed, 40 insertions, 0 deletions
diff --git a/book/src/keymap.md b/book/src/keymap.md
index 9d5d0841..e56eeefc 100644
--- a/book/src/keymap.md
+++ b/book/src/keymap.md
@@ -367,6 +367,7 @@ Keys to use within prompt, Remapping currently not supported.
| `Ctrl-s` | Insert a word under doc cursor, may be changed to Ctrl-r Ctrl-w later |
| `Ctrl-p`, `Up` | Select previous history |
| `Ctrl-n`, `Down` | Select next history |
+| `Ctrl-r` | Insert the content of the register selected by following input char |
| `Tab` | Select next completion item |
| `BackTab` | Select previous completion item |
| `Enter` | Open selected |
diff --git a/helix-term/src/ui/prompt.rs b/helix-term/src/ui/prompt.rs
index ef08edf2..c3c502e0 100644
--- a/helix-term/src/ui/prompt.rs
+++ b/helix-term/src/ui/prompt.rs
@@ -16,6 +16,7 @@ use helix_view::{
};
pub type Completion = (RangeFrom<usize>, Cow<'static, str>);
+type PromptCharHandler = Box<dyn Fn(&mut Prompt, char, &Context)>;
pub struct Prompt {
prompt: Cow<'static, str>,
@@ -28,6 +29,7 @@ pub struct Prompt {
completion_fn: Box<dyn FnMut(&Editor, &str) -> Vec<Completion>>,
callback_fn: Box<dyn FnMut(&mut Context, &str, PromptEvent)>,
pub doc_fn: Box<dyn Fn(&str) -> Option<Cow<str>>>,
+ next_char_handler: Option<PromptCharHandler>,
}
#[derive(Clone, Copy, PartialEq)]
@@ -78,6 +80,7 @@ impl Prompt {
completion_fn: Box::new(completion_fn),
callback_fn: Box::new(callback_fn),
doc_fn: Box::new(|_| None),
+ next_char_handler: None,
}
}
@@ -191,6 +194,13 @@ impl Prompt {
}
pub fn insert_char(&mut self, c: char, cx: &Context) {
+ if let Some(handler) = &self.next_char_handler.take() {
+ handler(self, c, cx);
+
+ self.next_char_handler = None;
+ return;
+ }
+
self.line.insert(self.cursor, c);
let mut cursor = GraphemeCursor::new(self.cursor, self.line.len(), false);
if let Ok(Some(pos)) = cursor.next_boundary(&self.line, 0) {
@@ -538,6 +548,35 @@ impl Component for Prompt {
(self.callback_fn)(cx, &self.line, PromptEvent::Update)
}
ctrl!('q') => self.exit_selection(),
+ ctrl!('r') => {
+ self.completion = cx
+ .editor
+ .registers
+ .inner()
+ .iter()
+ .map(|(ch, reg)| {
+ let content = reg
+ .read()
+ .get(0)
+ .and_then(|s| s.lines().next().to_owned())
+ .unwrap_or_default();
+ (0.., format!("{} {}", ch, &content).into())
+ })
+ .collect();
+ self.next_char_handler = Some(Box::new(|prompt, c, context| {
+ prompt.insert_str(
+ context
+ .editor
+ .registers
+ .read(c)
+ .and_then(|r| r.first())
+ .map_or("", |r| r.as_str()),
+ );
+ prompt.recalculate_completion(context.editor);
+ }));
+ (self.callback_fn)(cx, &self.line, PromptEvent::Update);
+ return EventResult::Consumed(None);
+ }
// any char event that's not mapped to any other combo
KeyEvent {
code: KeyCode::Char(c),