@@ -16,6 +16,7 @@ use helix_view::{
18pub type Completion = (RangeFrom<usize>, Cow<'static, str>);
18pub type Completion = (RangeFrom<usize>, Cow<'static, str>);
19+type PromptCharHandler = Box<dyn Fn(&mut Prompt, char, &Context)>;
20pub struct Prompt {
21pub struct Prompt {
21 prompt: Cow<'static, str>,
22 prompt: Cow<'static, str>,
@@ -28,6 +29,7 @@ pub struct Prompt {
28 completion_fn: Box<dyn FnMut(&Editor, &str) -> Vec<Completion>>,
29 completion_fn: Box<dyn FnMut(&Editor, &str) -> Vec<Completion>>,
29 callback_fn: Box<dyn FnMut(&mut Context, &str, PromptEvent)>,
30 callback_fn: Box<dyn FnMut(&mut Context, &str, PromptEvent)>,
30 pub doc_fn: Box<dyn Fn(&str) -> Option<Cow<str>>>,
31 pub doc_fn: Box<dyn Fn(&str) -> Option<Cow<str>>>,
32+ next_char_handler: Option<PromptCharHandler>,
33#[derive(Clone, Copy, PartialEq)]
35#[derive(Clone, Copy, PartialEq)]
@@ -78,6 +80,7 @@ impl Prompt {
78 completion_fn: Box::new(completion_fn),
80 completion_fn: Box::new(completion_fn),
79 callback_fn: Box::new(callback_fn),
81 callback_fn: Box::new(callback_fn),
80 doc_fn: Box::new(|_| None),
82 doc_fn: Box::new(|_| None),
83+ next_char_handler: None,
@@ -191,6 +194,13 @@ impl Prompt {
193 pub fn insert_char(&mut self, c: char, cx: &Context) {
196 pub fn insert_char(&mut self, c: char, cx: &Context) {
197+ if let Some(handler) = &self.next_char_handler.take() {
198+ handler(self, c, cx);
200+ self.next_char_handler = None;
194 self.line.insert(self.cursor, c);
204 self.line.insert(self.cursor, c);
195 let mut cursor = GraphemeCursor::new(self.cursor, self.line.len(), false);
205 let mut cursor = GraphemeCursor::new(self.cursor, self.line.len(), false);
196 if let Ok(Some(pos)) = cursor.next_boundary(&self.line, 0) {
206 if let Ok(Some(pos)) = cursor.next_boundary(&self.line, 0) {
@@ -538,6 +548,35 @@ impl Component for Prompt {
538 (self.callback_fn)(cx, &self.line, PromptEvent::Update)
548 (self.callback_fn)(cx, &self.line, PromptEvent::Update)
540 ctrl!('q') => self.exit_selection(),
550 ctrl!('q') => self.exit_selection(),
552+ self.completion = cx
561+ .and_then(|s| s.lines().next().to_owned())
562+ .unwrap_or_default();
563+ (0.., format!("{} {}", ch, &content).into())
566+ self.next_char_handler = Some(Box::new(|prompt, c, context| {
572+ .and_then(|r| r.first())
573+ .map_or("", |r| r.as_str()),
575+ prompt.recalculate_completion(context.editor);
577+ (self.callback_fn)(cx, &self.line, PromptEvent::Update);
578+ return EventResult::Consumed(None);
541 // any char event that's not mapped to any other combo
580 // any char event that's not mapped to any other combo
542 KeyEvent {
581 KeyEvent {
543 code: KeyCode::Char(c),
582 code: KeyCode::Char(c),