diff --git a/examples/diy_hints.rs b/examples/diy_hints.rs index 4bd3438ca..af8fadc80 100644 --- a/examples/diy_hints.rs +++ b/examples/diy_hints.rs @@ -33,16 +33,16 @@ impl Hint for CommandHint { } impl CommandHint { - fn new(text: &str, complete_up_to: &str) -> CommandHint { + fn new(text: &str, complete_up_to: &str) -> Self { assert!(text.starts_with(complete_up_to)); - CommandHint { + Self { display: text.into(), complete_up_to: complete_up_to.len(), } } - fn suffix(&self, strip_chars: usize) -> CommandHint { - CommandHint { + fn suffix(&self, strip_chars: usize) -> Self { + Self { display: self.display[strip_chars..].to_owned(), complete_up_to: self.complete_up_to.saturating_sub(strip_chars), } diff --git a/src/binding.rs b/src/binding.rs index 7c083839d..902c09773 100644 --- a/src/binding.rs +++ b/src/binding.rs @@ -107,15 +107,15 @@ impl KeyEvent { impl TrieKey for Event { fn encode_bytes(&self) -> Vec { match self { - Event::Any => ANY.to_be_bytes().to_vec(), - Event::KeySeq(keys) => { + Self::Any => ANY.to_be_bytes().to_vec(), + Self::KeySeq(keys) => { let mut dst = Vec::with_capacity(keys.len() * 4); for key in keys { dst.extend_from_slice(&key.encode().to_be_bytes()); } dst } - Event::Mouse() => MOUSE.to_be_bytes().to_vec(), + Self::Mouse() => MOUSE.to_be_bytes().to_vec(), } } } @@ -132,8 +132,8 @@ pub enum EventHandler { } impl From for EventHandler { - fn from(c: Cmd) -> EventHandler { - EventHandler::Simple(c) + fn from(c: Cmd) -> Self { + Self::Simple(c) } } @@ -147,7 +147,7 @@ pub struct EventContext<'r> { impl<'r> EventContext<'r> { pub(crate) fn new(is: &InputState, wrt: &'r dyn Refresher) -> Self { - EventContext { + Self { mode: is.mode, input_mode: is.input_mode, wrt, diff --git a/src/completion.rs b/src/completion.rs index 477dfc475..bea93a2d7 100644 --- a/src/completion.rs +++ b/src/completion.rs @@ -375,7 +375,7 @@ fn filename_complete( dir_path.to_path_buf() }; - let mut entries: Vec = Vec::new(); + let mut entries: Vec = vec![]; // if dir doesn't exist, then don't offer any completions if !dir.exists() { diff --git a/src/config.rs b/src/config.rs index bb6ec65ae..a4c3d12bc 100644 --- a/src/config.rs +++ b/src/config.rs @@ -248,12 +248,12 @@ pub enum BellStyle { impl Default for BellStyle { #[cfg(any(windows, target_arch = "wasm32"))] fn default() -> Self { - BellStyle::None + Self::None } #[cfg(unix)] fn default() -> Self { - BellStyle::Audible + Self::Audible } } diff --git a/src/edit.rs b/src/edit.rs index e6881eb44..c231e4a42 100644 --- a/src/edit.rs +++ b/src/edit.rs @@ -51,9 +51,9 @@ impl<'out, 'prompt, H: Helper> State<'out, 'prompt, H> { prompt: &'prompt str, helper: Option<&'out H>, ctx: Context<'out>, - ) -> State<'out, 'prompt, H> { + ) -> Self { let prompt_size = out.calculate_position(prompt, Position::default()); - State { + Self { out, prompt, prompt_size, diff --git a/src/error.rs b/src/error.rs index ee7ea1c81..3007f85c8 100644 --- a/src/error.rs +++ b/src/error.rs @@ -37,18 +37,18 @@ pub enum ReadlineError { impl fmt::Display for ReadlineError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { - ReadlineError::Io(ref err) => err.fmt(f), - ReadlineError::Eof => write!(f, "EOF"), - ReadlineError::Interrupted => write!(f, "Interrupted"), + Self::Io(ref err) => err.fmt(f), + Self::Eof => write!(f, "EOF"), + Self::Interrupted => write!(f, "Interrupted"), #[cfg(unix)] - ReadlineError::Errno(ref err) => err.fmt(f), - ReadlineError::WindowResized => write!(f, "WindowResized"), + Self::Errno(ref err) => err.fmt(f), + Self::WindowResized => write!(f, "WindowResized"), #[cfg(windows)] - ReadlineError::Decode(ref err) => err.fmt(f), + Self::Decode(ref err) => err.fmt(f), #[cfg(windows)] - ReadlineError::SystemError(ref err) => err.fmt(f), + Self::SystemError(ref err) => err.fmt(f), #[cfg(feature = "with-sqlite-history")] - ReadlineError::SQLiteError(ref err) => err.fmt(f), + Self::SQLiteError(ref err) => err.fmt(f), } } } @@ -56,18 +56,18 @@ impl fmt::Display for ReadlineError { impl Error for ReadlineError { fn source(&self) -> Option<&(dyn Error + 'static)> { match *self { - ReadlineError::Io(ref err) => Some(err), - ReadlineError::Eof => None, - ReadlineError::Interrupted => None, + Self::Io(ref err) => Some(err), + Self::Eof => None, + Self::Interrupted => None, #[cfg(unix)] - ReadlineError::Errno(ref err) => Some(err), - ReadlineError::WindowResized => None, + Self::Errno(ref err) => Some(err), + Self::WindowResized => None, #[cfg(windows)] - ReadlineError::Decode(ref err) => Some(err), + Self::Decode(ref err) => Some(err), #[cfg(windows)] - ReadlineError::SystemError(_) => None, + Self::SystemError(_) => None, #[cfg(feature = "with-sqlite-history")] - ReadlineError::SQLiteError(ref err) => Some(err), + Self::SQLiteError(ref err) => Some(err), } } } diff --git a/src/keymap.rs b/src/keymap.rs index edaf1ef23..607767c70 100644 --- a/src/keymap.rs +++ b/src/keymap.rs @@ -133,14 +133,14 @@ impl Cmd { pub const fn should_reset_kill_ring(&self) -> bool { #[allow(clippy::match_same_arms)] match *self { - Cmd::Kill(Movement::BackwardChar(_) | Movement::ForwardChar(_)) => true, - Cmd::ClearScreen - | Cmd::Kill(_) - | Cmd::Replace(..) - | Cmd::Noop - | Cmd::Suspend - | Cmd::Yank(..) - | Cmd::YankPop => false, + Self::Kill(Movement::BackwardChar(_) | Movement::ForwardChar(_)) => true, + Self::ClearScreen + | Self::Kill(_) + | Self::Replace(..) + | Self::Noop + | Self::Suspend + | Self::Yank(..) + | Self::YankPop => false, _ => true, } } @@ -148,21 +148,21 @@ impl Cmd { const fn is_repeatable_change(&self) -> bool { matches!( *self, - Cmd::Dedent(..) - | Cmd::Indent(..) - | Cmd::Insert(..) - | Cmd::Kill(_) - | Cmd::ReplaceChar(..) - | Cmd::Replace(..) - | Cmd::SelfInsert(..) - | Cmd::ViYankTo(_) - | Cmd::Yank(..) // Cmd::TransposeChars | TODO Validate + Self::Dedent(..) + | Self::Indent(..) + | Self::Insert(..) + | Self::Kill(_) + | Self::ReplaceChar(..) + | Self::Replace(..) + | Self::SelfInsert(..) + | Self::ViYankTo(_) + | Self::Yank(..) // Cmd::TransposeChars | TODO Validate ) } const fn is_repeatable(&self) -> bool { match *self { - Cmd::Move(_) => true, + Self::Move(_) => true, _ => self.is_repeatable_change(), } } @@ -170,40 +170,40 @@ impl Cmd { // Replay this command with a possible different `RepeatCount`. fn redo(&self, new: Option, wrt: &dyn Refresher) -> Self { match *self { - Cmd::Dedent(ref mvt) => Cmd::Dedent(mvt.redo(new)), - Cmd::Indent(ref mvt) => Cmd::Indent(mvt.redo(new)), - Cmd::Insert(previous, ref text) => { - Cmd::Insert(repeat_count(previous, new), text.clone()) + Self::Dedent(ref mvt) => Self::Dedent(mvt.redo(new)), + Self::Indent(ref mvt) => Self::Indent(mvt.redo(new)), + Self::Insert(previous, ref text) => { + Self::Insert(repeat_count(previous, new), text.clone()) } - Cmd::Kill(ref mvt) => Cmd::Kill(mvt.redo(new)), - Cmd::Move(ref mvt) => Cmd::Move(mvt.redo(new)), - Cmd::ReplaceChar(previous, c) => Cmd::ReplaceChar(repeat_count(previous, new), c), - Cmd::Replace(ref mvt, ref text) => { + Self::Kill(ref mvt) => Self::Kill(mvt.redo(new)), + Self::Move(ref mvt) => Self::Move(mvt.redo(new)), + Self::ReplaceChar(previous, c) => Self::ReplaceChar(repeat_count(previous, new), c), + Self::Replace(ref mvt, ref text) => { if text.is_none() { let last_insert = wrt.last_insert(); if let Movement::ForwardChar(0) = mvt { - Cmd::Replace( + Self::Replace( Movement::ForwardChar(last_insert.as_ref().map_or(0, String::len)), last_insert, ) } else { - Cmd::Replace(mvt.redo(new), last_insert) + Self::Replace(mvt.redo(new), last_insert) } } else { - Cmd::Replace(mvt.redo(new), text.clone()) + Self::Replace(mvt.redo(new), text.clone()) } } - Cmd::SelfInsert(previous, c) => { + Self::SelfInsert(previous, c) => { // consecutive char inserts are repeatable not only the last one... if let Some(text) = wrt.last_insert() { - Cmd::Insert(repeat_count(previous, new), text) + Self::Insert(repeat_count(previous, new), text) } else { - Cmd::SelfInsert(repeat_count(previous, new), c) + Self::SelfInsert(repeat_count(previous, new), c) } } // Cmd::TransposeChars => Cmd::TransposeChars, - Cmd::ViYankTo(ref mvt) => Cmd::ViYankTo(mvt.redo(new)), - Cmd::Yank(previous, anchor) => Cmd::Yank(repeat_count(previous, new), anchor), + Self::ViYankTo(ref mvt) => Self::ViYankTo(mvt.redo(new)), + Self::Yank(previous, anchor) => Self::Yank(repeat_count(previous, new), anchor), _ => unreachable!(), } } @@ -263,10 +263,10 @@ pub enum CharSearch { impl CharSearch { const fn opposite(self) -> Self { match self { - CharSearch::Forward(c) => CharSearch::Backward(c), - CharSearch::ForwardBefore(c) => CharSearch::BackwardAfter(c), - CharSearch::Backward(c) => CharSearch::Forward(c), - CharSearch::BackwardAfter(c) => CharSearch::ForwardBefore(c), + Self::Forward(c) => Self::Backward(c), + Self::ForwardBefore(c) => Self::BackwardAfter(c), + Self::Backward(c) => Self::Forward(c), + Self::BackwardAfter(c) => Self::ForwardBefore(c), } } } @@ -309,26 +309,26 @@ impl Movement { // Replay this movement with a possible different `RepeatCount`. const fn redo(&self, new: Option) -> Self { match *self { - Movement::WholeLine => Movement::WholeLine, - Movement::BeginningOfLine => Movement::BeginningOfLine, - Movement::ViFirstPrint => Movement::ViFirstPrint, - Movement::EndOfLine => Movement::EndOfLine, - Movement::BackwardWord(previous, word) => { - Movement::BackwardWord(repeat_count(previous, new), word) + Self::WholeLine => Self::WholeLine, + Self::BeginningOfLine => Self::BeginningOfLine, + Self::ViFirstPrint => Self::ViFirstPrint, + Self::EndOfLine => Self::EndOfLine, + Self::BackwardWord(previous, word) => { + Self::BackwardWord(repeat_count(previous, new), word) } - Movement::ForwardWord(previous, at, word) => { - Movement::ForwardWord(repeat_count(previous, new), at, word) + Self::ForwardWord(previous, at, word) => { + Self::ForwardWord(repeat_count(previous, new), at, word) } - Movement::ViCharSearch(previous, char_search) => { - Movement::ViCharSearch(repeat_count(previous, new), char_search) + Self::ViCharSearch(previous, char_search) => { + Self::ViCharSearch(repeat_count(previous, new), char_search) } - Movement::BackwardChar(previous) => Movement::BackwardChar(repeat_count(previous, new)), - Movement::ForwardChar(previous) => Movement::ForwardChar(repeat_count(previous, new)), - Movement::LineUp(previous) => Movement::LineUp(repeat_count(previous, new)), - Movement::LineDown(previous) => Movement::LineDown(repeat_count(previous, new)), - Movement::WholeBuffer => Movement::WholeBuffer, - Movement::BeginningOfBuffer => Movement::BeginningOfBuffer, - Movement::EndOfBuffer => Movement::EndOfBuffer, + Self::BackwardChar(previous) => Self::BackwardChar(repeat_count(previous, new)), + Self::ForwardChar(previous) => Self::ForwardChar(repeat_count(previous, new)), + Self::LineUp(previous) => Self::LineUp(repeat_count(previous, new)), + Self::LineDown(previous) => Self::LineDown(repeat_count(previous, new)), + Self::WholeBuffer => Self::WholeBuffer, + Self::BeginningOfBuffer => Self::BeginningOfBuffer, + Self::EndOfBuffer => Self::EndOfBuffer, } } } @@ -1209,14 +1209,14 @@ enum Event { KeySeq([KeyEvent; 1]), } impl From for Event { - fn from(k: KeyEvent) -> Event { - Event::KeySeq([k]) + fn from(k: KeyEvent) -> Self { + Self::KeySeq([k]) } } pub struct Bindings {} impl Bindings { - pub fn new() -> Bindings { - Bindings {} + pub fn new() -> Self { + Self {} } } } diff --git a/src/lib.rs b/src/lib.rs index 9f0cee529..f05f720d9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -563,7 +563,7 @@ impl<'h> Context<'h> { /// Constructor. Visible for testing. #[must_use] pub fn new(history: &'h dyn History) -> Self { - Context { + Self { history, history_index: history.len(), } diff --git a/src/line_buffer.rs b/src/line_buffer.rs index 3c7b403c6..bc5e2e24e 100644 --- a/src/line_buffer.rs +++ b/src/line_buffer.rs @@ -160,11 +160,7 @@ impl LineBuffer { let max = self.buf.capacity(); if self.must_truncate(buf.len()) { self.insert_str(0, &buf[..max], cl); - if pos > max { - self.pos = max; - } else { - self.pos = pos; - } + self.pos = max.min(pos); } else { self.insert_str(0, buf, cl); self.pos = pos; diff --git a/src/tty/test.rs b/src/tty/test.rs index a7fa01fc8..56a37ccfc 100644 --- a/src/tty/test.rs +++ b/src/tty/test.rs @@ -189,7 +189,7 @@ impl Term for DummyTerminal { _enable_signals: bool, ) -> Result { Ok(DummyTerminal { - keys: Vec::new(), + keys: vec![], cursor: 0, color_mode, bell_style, diff --git a/src/tty/windows.rs b/src/tty/windows.rs index af1f0ccf5..8b74e87d5 100644 --- a/src/tty/windows.rs +++ b/src/tty/windows.rs @@ -665,7 +665,7 @@ impl Term for Console { bell_style: BellStyle, _enable_bracketed_paste: bool, _enable_signals: bool, - ) -> Result { + ) -> Result { let (conin, conout, close_on_drop) = if behavior == Behavior::PreferTerm { if let (Ok(conin), Ok(conout)) = ( OpenOptions::new().read(true).write(true).open("CONIN$"), @@ -706,7 +706,7 @@ impl Term for Console { Err(_) => false, }; - Ok(Console { + Ok(Self { conin_isatty, conin: conin.unwrap_or(ptr::null_mut()), conout_isatty, @@ -904,7 +904,7 @@ impl super::ExternalPrinter for ExternalPrinter { fn print(&mut self, msg: String) -> Result<()> { // write directly to stdout/stderr while not in raw mode if !self.raw_mode.load(Ordering::SeqCst) { - let mut utf16 = Vec::new(); + let mut utf16 = vec![]; write_to_console(self.conout, msg.as_str(), &mut utf16) } else { self.sender diff --git a/src/undo.rs b/src/undo.rs index a76776c1c..924319f24 100644 --- a/src/undo.rs +++ b/src/undo.rs @@ -30,17 +30,15 @@ enum Change { impl Change { fn undo(&self, line: &mut LineBuffer) { match *self { - Change::Begin | Change::End => { - unreachable!(); - } - Change::Insert { idx, ref text } => { + Self::Begin | Self::End => unreachable!(), + Self::Insert { idx, ref text } => { line.delete_range(idx..idx + text.len(), &mut NoListener); } - Change::Delete { idx, ref text } => { + Self::Delete { idx, ref text } => { line.insert_str(idx, text, &mut NoListener); line.set_pos(idx + text.len()); } - Change::Replace { + Self::Replace { idx, ref old, ref new, @@ -53,16 +51,14 @@ impl Change { #[cfg(test)] fn redo(&self, line: &mut LineBuffer) { match *self { - Change::Begin | Change::End => { - unreachable!(); - } - Change::Insert { idx, ref text } => { + Self::Begin | Self::End => unreachable!(), + Self::Insert { idx, ref text } => { line.insert_str(idx, text, &mut NoListener); } - Change::Delete { idx, ref text } => { + Self::Delete { idx, ref text } => { line.delete_range(idx..idx + text.len(), &mut NoListener); } - Change::Replace { + Self::Replace { idx, ref old, ref new, @@ -73,7 +69,7 @@ impl Change { } fn insert_seq(&self, indx: usize) -> bool { - if let Change::Insert { idx, ref text } = *self { + if let Self::Insert { idx, ref text } = *self { idx + text.len() == indx } else { false @@ -81,7 +77,7 @@ impl Change { } fn delete_seq(&self, indx: usize, len: usize) -> bool { - if let Change::Delete { idx, .. } = *self { + if let Self::Delete { idx, .. } = *self { // delete or backspace idx == indx || idx == indx + len } else { @@ -90,7 +86,7 @@ impl Change { } fn replace_seq(&self, indx: usize) -> bool { - if let Change::Replace { idx, ref new, .. } = *self { + if let Self::Replace { idx, ref new, .. } = *self { idx + new.len() == indx } else { false @@ -109,8 +105,8 @@ impl Changeset { pub(crate) fn new() -> Self { Self { undo_group_level: 0, - undos: Vec::new(), - redos: Vec::new(), + undos: vec![], + redos: vec![], } } diff --git a/src/validate.rs b/src/validate.rs index 8ea8acaf8..e3bf2e247 100644 --- a/src/validate.rs +++ b/src/validate.rs @@ -17,14 +17,11 @@ pub enum ValidationResult { impl ValidationResult { pub(crate) fn is_valid(&self) -> bool { - matches!(self, ValidationResult::Valid(_)) + matches!(self, Self::Valid(_)) } pub(crate) fn has_message(&self) -> bool { - matches!( - self, - ValidationResult::Valid(Some(_)) | ValidationResult::Invalid(Some(_)) - ) + matches!(self, Self::Valid(Some(_)) | Self::Invalid(Some(_))) } }