From b2482c01ee5f0c9d26c18bec25b05e07bef5e18b Mon Sep 17 00:00:00 2001 From: gwenn <gtreguier@gmail.com> Date: Wed, 15 Aug 2018 14:47:29 +0200 Subject: [PATCH] Rustfmt --- rustfmt.toml | 1 + src/completion.rs | 4 ++++ src/edit.rs | 4 ++++ src/history.rs | 4 +++- src/keymap.rs | 1 + src/kill_ring.rs | 2 ++ src/lib.rs | 7 +++++++ src/line_buffer.rs | 10 ++++++++++ src/tty/mod.rs | 9 +++++++++ src/tty/test.rs | 7 ++++++- src/tty/unix.rs | 3 ++- src/tty/windows.rs | 2 +- src/undo.rs | 4 ++++ 13 files changed, 54 insertions(+), 4 deletions(-) diff --git a/rustfmt.toml b/rustfmt.toml index 34977e48..83697e3d 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,3 +1,4 @@ wrap_comments = true format_strings = true error_on_unformatted = false +reorder_impl_items = true diff --git a/src/completion.rs b/src/completion.rs index 1d057532..aeb9a34a 100644 --- a/src/completion.rs +++ b/src/completion.rs @@ -24,6 +24,7 @@ impl Candidate for String { fn display(&self) -> &str { self.as_str() } + fn replacement(&self) -> &str { self.as_str() } @@ -38,6 +39,7 @@ impl Candidate for Pair { fn display(&self) -> &str { self.display.as_str() } + fn replacement(&self) -> &str { self.replacement.as_str() } @@ -66,6 +68,7 @@ impl Completer for () { fn complete(&self, _line: &str, _pos: usize) -> Result<(usize, Vec<String>)> { Ok((0, Vec::with_capacity(0))) } + fn update(&self, _line: &mut LineBuffer, _start: usize, _elected: &str) { unreachable!() } @@ -77,6 +80,7 @@ impl<'c, C: ?Sized + Completer> Completer for &'c C { fn complete(&self, line: &str, pos: usize) -> Result<(usize, Vec<Self::Candidate>)> { (**self).complete(line, pos) } + fn update(&self, line: &mut LineBuffer, start: usize, elected: &str) { (**self).update(line, start, elected) } diff --git a/src/edit.rs b/src/edit.rs index 7dae7669..6a82da6f 100644 --- a/src/edit.rs +++ b/src/edit.rs @@ -80,6 +80,7 @@ impl<'out, 'prompt> State<'out, 'prompt> { self.saved_line_for_history .update(self.line.as_str(), self.line.pos()); } + pub fn restore(&mut self) { self.line.update( self.saved_line_for_history.as_str(), @@ -136,12 +137,15 @@ impl<'out, 'prompt> Refresher for State<'out, 'prompt> { let hint = self.hint(); self.refresh(prompt, prompt_size, hint) } + fn doing_insert(&mut self) { self.changes.borrow_mut().begin(); } + fn done_inserting(&mut self) { self.changes.borrow_mut().end(); } + fn last_insert(&self) -> Option<String> { self.changes.borrow().last_insert() } diff --git a/src/history.rs b/src/history.rs index 60821d3b..76a5bcf4 100644 --- a/src/history.rs +++ b/src/history.rs @@ -32,6 +32,7 @@ impl History { pub fn new() -> History { Self::with_config(Config::default()) } + pub fn with_config(config: Config) -> History { History { entries: VecDeque::new(), @@ -83,6 +84,7 @@ impl History { pub fn len(&self) -> usize { self.entries.len() } + /// Return true if the history has no entry. pub fn is_empty(&self) -> bool { self.entries.is_empty() @@ -213,8 +215,8 @@ impl Index<usize> for History { } impl<'a> IntoIterator for &'a History { - type Item = &'a String; type IntoIter = Iter<'a>; + type Item = &'a String; fn into_iter(self) -> Iter<'a> { self.iter() diff --git a/src/keymap.rs b/src/keymap.rs index eb3d5608..877a12dd 100644 --- a/src/keymap.rs +++ b/src/keymap.rs @@ -111,6 +111,7 @@ impl Cmd { _ => false, } } + fn is_repeatable(&self) -> bool { match *self { Cmd::Move(_) => true, diff --git a/src/kill_ring.rs b/src/kill_ring.rs index 15b66494..b5d87780 100644 --- a/src/kill_ring.rs +++ b/src/kill_ring.rs @@ -109,6 +109,7 @@ impl DeleteListener for KillRing { fn start_killing(&mut self) { self.killing = true; } + fn delete(&mut self, _: usize, string: &str, dir: Direction) { if !self.killing { return; @@ -119,6 +120,7 @@ impl DeleteListener for KillRing { }; self.kill(string, mode); } + fn stop_killing(&mut self) { self.killing = false; } diff --git a/src/lib.rs b/src/lib.rs index 9d2f876a..9cd6a16c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -677,6 +677,7 @@ impl<H: Helper> Editor<H> { pub fn readline(&mut self, prompt: &str) -> Result<String> { self.readline_with(prompt, None) } + /// This function behaves in the exact same manner as `readline`, except /// that it pre-populates the input area. /// @@ -710,22 +711,27 @@ impl<H: Helper> Editor<H> { pub fn load_history<P: AsRef<Path> + ?Sized>(&mut self, path: &P) -> Result<()> { self.history.load(path) } + /// Save the history in the specified file. pub fn save_history<P: AsRef<Path> + ?Sized>(&self, path: &P) -> Result<()> { self.history.save(path) } + /// Add a new entry in the history. pub fn add_history_entry<S: AsRef<str> + Into<String>>(&mut self, line: S) -> bool { self.history.add(line) } + /// Clear history. pub fn clear_history(&mut self) { self.history.clear() } + /// Return a mutable reference to the history object. pub fn get_history(&mut self) -> &mut History { &mut self.history } + /// Return an immutable reference to the history object. pub fn get_history_const(&self) -> &History { &self.history @@ -747,6 +753,7 @@ impl<H: Helper> Editor<H> { let mut bindings = self.custom_bindings.write().unwrap(); bindings.insert(key_seq, cmd) } + /// Remove a binding for the given sequence. pub fn unbind_sequence(&mut self, key_seq: KeyPress) -> Option<Cmd> { let mut bindings = self.custom_bindings.write().unwrap(); diff --git a/src/line_buffer.rs b/src/line_buffer.rs index 1e7ddc26..1c220ca9 100644 --- a/src/line_buffer.rs +++ b/src/line_buffer.rs @@ -93,9 +93,11 @@ impl LineBuffer { pub(crate) fn set_delete_listener(&mut self, dl: Arc<Mutex<DeleteListener>>) { self.dl = Some(dl); } + pub(crate) fn set_change_listener(&mut self, dl: Rc<RefCell<ChangeListener>>) { self.cl = Some(dl); } + pub(crate) fn remove_change_listener(&mut self) { self.cl = None; } @@ -114,6 +116,7 @@ impl LineBuffer { pub fn pos(&self) -> usize { self.pos } + /// Set cursor position (byte position) pub fn set_pos(&mut self, pos: usize) { assert!(pos <= self.buf.len()); @@ -124,6 +127,7 @@ impl LineBuffer { pub fn len(&self) -> usize { self.buf.len() } + /// Returns `true` if this buffer has a length of zero. pub fn is_empty(&self) -> bool { self.buf.is_empty() @@ -169,6 +173,7 @@ impl LineBuffer { .last() .map(|(i, s)| i + self.pos + s.len()) } + /// Returns the position of the character just before the current cursor /// position. fn prev_pos(&self, n: RepeatCount) -> Option<usize> { @@ -583,6 +588,7 @@ impl LineBuffer { .next() .map(|i| i + self.pos) } + /// Alter the next word. pub fn edit_word(&mut self, a: WordAction) -> bool { if let Some(start) = self.skip_whitespace() { @@ -872,14 +878,18 @@ mod test { impl DeleteListener for Listener { fn start_killing(&mut self) {} + fn delete(&mut self, _: usize, string: &str, _: Direction) { self.deleted_str = Some(string.to_owned()); } + fn stop_killing(&mut self) {} } impl ChangeListener for Listener { fn insert_char(&mut self, _: usize, _: char) {} + fn insert_str(&mut self, _: usize, _: &str) {} + fn replace(&mut self, _: usize, _: &str, _: &str) {} } diff --git a/src/tty/mod.rs b/src/tty/mod.rs index 1047c421..e96f676a 100644 --- a/src/tty/mod.rs +++ b/src/tty/mod.rs @@ -77,6 +77,7 @@ impl<'a, R: Renderer + ?Sized> Renderer for &'a mut R { fn move_cursor(&mut self, old: Position, new: Position) -> Result<()> { (**self).move_cursor(old, new) } + fn refresh_line( &mut self, prompt: &str, @@ -88,27 +89,35 @@ impl<'a, R: Renderer + ?Sized> Renderer for &'a mut R { ) -> Result<(Position, Position)> { (**self).refresh_line(prompt, prompt_size, line, hint, current_row, old_rows) } + fn calculate_position(&self, s: &str, orig: Position) -> Position { (**self).calculate_position(s, orig) } + fn write_and_flush(&mut self, buf: &[u8]) -> Result<()> { (**self).write_and_flush(buf) } + fn beep(&mut self) -> Result<()> { (**self).beep() } + fn clear_screen(&mut self) -> Result<()> { (**self).clear_screen() } + fn sigwinch(&self) -> bool { (**self).sigwinch() } + fn update_size(&mut self) { (**self).update_size() } + fn get_columns(&self) -> usize { (**self).get_columns() } + fn get_rows(&self) -> usize { (**self).get_rows() } diff --git a/src/tty/test.rs b/src/tty/test.rs index b8e5df59..4166948e 100644 --- a/src/tty/test.rs +++ b/src/tty/test.rs @@ -25,6 +25,7 @@ impl<'a> RawReader for Iter<'a, KeyPress> { None => Err(ReadlineError::Eof), } } + #[cfg(unix)] fn next_char(&mut self) -> Result<char> { unimplemented!(); @@ -38,6 +39,7 @@ impl RawReader for IntoIter<KeyPress> { None => Err(ReadlineError::Eof), } } + #[cfg(unix)] fn next_char(&mut self) -> Result<char> { match self.next() { @@ -99,10 +101,13 @@ impl Renderer for Sink { fn sigwinch(&self) -> bool { false } + fn update_size(&mut self) {} + fn get_columns(&self) -> usize { 80 } + fn get_rows(&self) -> usize { 24 } @@ -117,9 +122,9 @@ pub struct DummyTerminal { } impl Term for DummyTerminal { + type Mode = Mode; type Reader = IntoIter<KeyPress>; type Writer = Sink; - type Mode = Mode; fn new(_color_mode: ColorMode) -> DummyTerminal { DummyTerminal { diff --git a/src/tty/unix.rs b/src/tty/unix.rs index fd366d5d..22dd6750 100644 --- a/src/tty/unix.rs +++ b/src/tty/unix.rs @@ -528,6 +528,7 @@ impl Renderer for PosixRenderer { fn get_columns(&self) -> usize { self.cols } + /// Try to get the number of rows in the current terminal, /// or assume 24 if it fails. fn get_rows(&self) -> usize { @@ -566,9 +567,9 @@ pub struct PosixTerminal { } impl Term for PosixTerminal { + type Mode = Mode; type Reader = PosixRawReader; type Writer = PosixRenderer; - type Mode = Mode; fn new(color_mode: ColorMode) -> PosixTerminal { let term = PosixTerminal { diff --git a/src/tty/windows.rs b/src/tty/windows.rs index 5618f8c4..a27b9fb7 100644 --- a/src/tty/windows.rs +++ b/src/tty/windows.rs @@ -407,9 +407,9 @@ pub struct Console { impl Console {} impl Term for Console { + type Mode = Mode; type Reader = ConsoleRawReader; type Writer = ConsoleRenderer; - type Mode = Mode; fn new(color_mode: ColorMode) -> Console { use std::ptr; diff --git a/src/undo.rs b/src/undo.rs index 333e5b49..7f7c9abd 100644 --- a/src/undo.rs +++ b/src/undo.rs @@ -329,18 +329,22 @@ impl Changeset { impl DeleteListener for Changeset { fn start_killing(&mut self) {} + fn delete(&mut self, idx: usize, string: &str, _: Direction) { self.delete(idx, string); } + fn stop_killing(&mut self) {} } impl ChangeListener for Changeset { fn insert_char(&mut self, idx: usize, c: char) { self.insert(idx, c); } + fn insert_str(&mut self, idx: usize, string: &str) { self.insert_str(idx, string); } + fn replace(&mut self, idx: usize, old: &str, new: &str) { self.replace(idx, old, new); } -- GitLab