Commit c3e7cf35 authored by MovingtoMars's avatar MovingtoMars

pressing the right arrow key accepts autosuggestions

parent dc0c20bb
......@@ -536,19 +536,27 @@ impl<'a, W: Write> Editor<'a, W> {
/// Accept autosuggestion and copy its content into current buffer
pub fn accept_autosuggestion(&mut self) -> io::Result<()> {
if self.show_autosuggestions {
{
let hist_match = self.context
.history
.get_first_match(self.cur_history_loc, self.current_buffer())
.get_newest_match(self.cur_history_loc, self.current_buffer())
.cloned();
let mut buf = self.current_buffer_mut();
if let Some(hm) = hist_match {
buf.insert_from_buffer(&hm);
}
}
}
self.print_current_buffer(true)
}
pub fn is_currently_showing_autosuggestion(&self) -> bool {
self.show_autosuggestions && self.context
.history
.get_newest_match(self.cur_history_loc, self.current_buffer()).is_some()
}
/// Deletes the displayed prompt and buffer, replacing them with the current prompt and buffer
pub fn print_current_buffer(&mut self, move_cursor_to_end_of_line: bool) -> io::Result<()> {
let buf = cur_buf!(self);
......@@ -576,7 +584,7 @@ impl<'a, W: Write> Editor<'a, W> {
if self.show_autosuggestions {
if let Some(hist_match) = self.context
.history
.get_first_match(self.cur_history_loc, buf) {
.get_newest_match(self.cur_history_loc, buf) {
write!(self.out, "{}", color::Fg(color::Yellow))?;
let len = hist_match.print_rest(&mut self.out, buf.chars().len())?;
write!(self.out, "{}", color::Fg(color::Reset))?;
......
......@@ -61,7 +61,7 @@ impl History {
/// Go through the history and try to find a buffer which starts the same as the new buffer
/// given to this function as argument.
pub fn get_first_match<'a, 'b>(&'a self, curr_position: Option<usize>, new_buff: &'b Buffer) -> Option<&'a Buffer> {
pub fn get_newest_match<'a, 'b>(&'a self, curr_position: Option<usize>, new_buff: &'b Buffer) -> Option<&'a Buffer> {
let pos = curr_position.unwrap_or(self.buffers.len());
for iter in (0..pos).rev() {
if let Some(tested) = self.buffers.get(iter) {
......
......@@ -67,9 +67,13 @@ impl<'a, W: Write> KeyMap<'a, W, Emacs<'a, W>> for Emacs<'a, W> {
}
}
fn editor(&mut self) -> &mut Editor<'a, W> {
fn editor_mut(&mut self) -> &mut Editor<'a, W> {
&mut self.ed
}
fn editor(&self) -> &Editor<'a, W> {
&self.ed
}
}
impl<'a, W: Write> From<Emacs<'a, W>> for String {
......@@ -126,7 +130,7 @@ mod tests {
let out = Vec::new();
let ed = Editor::new(out, "prompt".to_owned(), &mut context).unwrap();
let mut map = Emacs::new(ed);
map.editor().insert_str_after_cursor("let").unwrap();
map.editor_mut().insert_str_after_cursor("let").unwrap();
assert_eq!(map.ed.cursor(), 3);
simulate_keys!(map, [Key::Left, Key::Char('f')]);
......
......@@ -5,42 +5,43 @@ use event::*;
pub trait KeyMap<'a, W: Write, T>: From<T> {
fn handle_key_core(&mut self, key: Key) -> io::Result<()>;
fn editor(&mut self) -> &mut Editor<'a, W>;
fn editor(&self) -> &Editor<'a, W>;
fn editor_mut(&mut self) -> &mut Editor<'a, W>;
fn handle_key(&mut self, key: Key, handler: &mut EventHandler<W>) -> io::Result<bool> {
let mut done = false;
handler(Event::new(self.editor(), EventKind::BeforeKey(key)));
handler(Event::new(self.editor_mut(), EventKind::BeforeKey(key)));
let is_empty = self.editor().current_buffer().is_empty();
match key {
Key::Ctrl('c') => {
try!(self.editor().handle_newline());
try!(self.editor_mut().handle_newline());
return Err(io::Error::new(ErrorKind::Interrupted, "ctrl-c"));
}
// if the current buffer is empty, treat ctrl-d as eof
Key::Ctrl('d') if is_empty => {
try!(self.editor().handle_newline());
try!(self.editor_mut().handle_newline());
return Err(io::Error::new(ErrorKind::UnexpectedEof, "ctrl-d"));
}
Key::Char('\t') => try!(self.editor().complete(handler)),
Key::Char('\t') => try!(self.editor_mut().complete(handler)),
Key::Char('\n') => {
try!(self.editor().handle_newline());
try!(self.editor_mut().handle_newline());
done = true;
}
Key::Ctrl('f') => {
try!(self.editor().accept_autosuggestion());
Key::Ctrl('f') | Key::Right if self.editor().is_currently_showing_autosuggestion() => {
try!(self.editor_mut().accept_autosuggestion());
}
_ => {
try!(self.handle_key_core(key));
self.editor().skip_completions_hint();
self.editor_mut().skip_completions_hint();
}
};
handler(Event::new(self.editor(), EventKind::AfterKey(key)));
handler(Event::new(self.editor_mut(), EventKind::AfterKey(key)));
try!(self.editor().flush());
try!(self.editor_mut().flush());
Ok(done)
}
......@@ -76,9 +77,13 @@ mod tests {
Ok(())
}
fn editor(&mut self) -> &mut Editor<'a, W> {
fn editor_mut(&mut self) -> &mut Editor<'a, W> {
&mut self.ed
}
fn editor(&self) -> &Editor<'a, W> {
&self.ed
}
}
#[test]
......
......@@ -966,9 +966,13 @@ impl<'a, W: Write> KeyMap<'a, W, Vi<'a, W>> for Vi<'a, W> {
}
}
fn editor(&mut self) -> &mut Editor<'a, W> {
fn editor_mut(&mut self) -> &mut Editor<'a, W> {
&mut self.ed
}
fn editor(&self) -> &Editor<'a, W> {
&self.ed
}
}
impl<'a, W: Write> From<Vi<'a, W>> for String {
......@@ -1029,7 +1033,7 @@ mod tests {
let out = Vec::new();
let ed = Editor::new(out, "prompt".to_owned(), &mut context).unwrap();
let mut map = Vi::new(ed);
map.editor().insert_str_after_cursor("let").unwrap();
map.editor_mut().insert_str_after_cursor("let").unwrap();
assert_eq!(map.ed.cursor(), 3);
simulate_keys!(map, [
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment