Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic support for C-r to paste register in prompt #1924

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions helix-term/src/ui/prompt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::compositor::{Component, Compositor, Context, EventResult};
use crate::{alt, ctrl, key, shift, ui};
use crossterm::event::Event;
use helix_view::info::Info;
use helix_view::input::KeyEvent;
use helix_view::keyboard::{KeyCode, KeyModifiers};
use std::{borrow::Cow, ops::RangeFrom};
Expand All @@ -25,6 +26,7 @@ pub struct Prompt {
selection: Option<usize>,
history_register: Option<char>,
history_pos: Option<usize>,
pending_register: bool,
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>>>,
Expand Down Expand Up @@ -71,6 +73,7 @@ impl Prompt {
selection: None,
history_register,
history_pos: None,
pending_register: false,
completion_fn: Box::new(completion_fn),
callback_fn: Box::new(callback_fn),
doc_fn: Box::new(|_| None),
Expand Down Expand Up @@ -450,6 +453,25 @@ impl Component for Prompt {
compositor.pop();
})));

if self.pending_register {
// C-r was pressed previously; this keystroke is the register
self.pending_register = false;
cx.editor.autoinfo = None;

if let KeyEvent {
code: KeyCode::Char(register),
modifiers: KeyModifiers::NONE,
} = event.into()
{
if let Some(text) = cx.editor.registers.get(register) {
self.insert_str(&text.read()[0]);
}
}

(self.callback_fn)(cx, &self.line, PromptEvent::Update);
return EventResult::Consumed(None);
}
Comment on lines +456 to +473
Copy link
Contributor

@pickfire pickfire Apr 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels weird to me and if more and more states are being added it might be less maintainable, I think it might be better to have some sort of state machine (like using enum) here to make sure we can't go wrong. But maybe someone else might have a better solution for this.

But sadly we can't hook on_next_key nicely here.


match event.into() {
ctrl!('c') | key!(Esc) => {
(self.callback_fn)(cx, &self.line, PromptEvent::Abort);
Expand Down Expand Up @@ -528,6 +550,11 @@ impl Component for Prompt {
self.change_completion_selection(CompletionDirection::Backward);
(self.callback_fn)(cx, &self.line, PromptEvent::Update)
}
ctrl!('r') => {
cx.editor.autoinfo = Some(Info::from_registers(&cx.editor.registers));
self.pending_register = true;
(self.callback_fn)(cx, &self.line, PromptEvent::Update)
}
ctrl!('q') => self.exit_selection(),
// any char event that's not combined with control or mapped to any other combo
KeyEvent {
Expand Down