From 34f23ea52d86bf9990753104500250c51f4cca0c Mon Sep 17 00:00:00 2001 From: Ulyssa Date: Sun, 11 Aug 2024 20:48:15 -0700 Subject: [PATCH] Fix Clippy 1.80 warnings --- crates/modalkit-ratatui/src/windows/layout.rs | 6 +- crates/modalkit-ratatui/src/windows/tree.rs | 1 + crates/modalkit/src/editing/buffer/mod.rs | 2 +- crates/modalkit/src/editing/rope/diff.rs | 9 +- crates/modalkit/src/editing/rope/mod.rs | 8 +- crates/modalkit/src/editing/store/cursor.rs | 2 +- crates/modalkit/src/key/mod.rs | 92 +++++++++---------- 7 files changed, 58 insertions(+), 62 deletions(-) diff --git a/crates/modalkit-ratatui/src/windows/layout.rs b/crates/modalkit-ratatui/src/windows/layout.rs index 97e377c..be5f460 100644 --- a/crates/modalkit-ratatui/src/windows/layout.rs +++ b/crates/modalkit-ratatui/src/windows/layout.rs @@ -138,8 +138,7 @@ where let mut carea = rect_zero_height(area); let mut f = |value: &mut Value| { - let (_, h) = value.dimensions(); - let height = slopped_length(height, h as u16, &mut slop); + let height = slopped_length(height, value.height() as u16, &mut slop); carea = rect_down(carea, height); @@ -155,8 +154,7 @@ where let mut carea = rect_zero_width(area); let mut f = |value: &mut Value| { - let (w, _) = value.dimensions(); - let width = slopped_length(width, w as u16, &mut slop); + let width = slopped_length(width, value.width() as u16, &mut slop); carea = rect_right(carea, width); diff --git a/crates/modalkit-ratatui/src/windows/tree.rs b/crates/modalkit-ratatui/src/windows/tree.rs index 524c5a3..f2547c9 100644 --- a/crates/modalkit-ratatui/src/windows/tree.rs +++ b/crates/modalkit-ratatui/src/windows/tree.rs @@ -81,6 +81,7 @@ where fn weight(&self) -> usize; /// Fetch a mutable reference to the nth `Value` in the tree. + #[allow(unused)] fn val_mut(&mut self, at: usize) -> Option<&mut Value>; /// Iterate over immutable references to each [Value] in this tree. diff --git a/crates/modalkit/src/editing/buffer/mod.rs b/crates/modalkit/src/editing/buffer/mod.rs index d769dbe..13703e5 100644 --- a/crates/modalkit/src/editing/buffer/mod.rs +++ b/crates/modalkit/src/editing/buffer/mod.rs @@ -6,7 +6,7 @@ //! movements. For example: //! //! - Performing edit operations like text deletion or case changes using content-aware movements -//! (e.g., by word, up to a specific character or to the start of the line, etc.) +//! (e.g., by word, up to a specific character or to the start of the line, etc.) //! - Copying and pasting text between registers and the buffer //! - Visual selections //! - Cursor groups diff --git a/crates/modalkit/src/editing/rope/diff.rs b/crates/modalkit/src/editing/rope/diff.rs index e50e868..8568bf1 100644 --- a/crates/modalkit/src/editing/rope/diff.rs +++ b/crates/modalkit/src/editing/rope/diff.rs @@ -27,7 +27,6 @@ pub enum DeltaElement<'a> { #[derive(Clone, Debug)] pub struct Delta<'a> { pub els: Vec>, - pub base_len: usize, } fn find_ne_char_back( @@ -130,7 +129,7 @@ pub fn compute_delta<'a>(base: &Rope, target: &'a Rope) -> Delta<'a> { // if our preliminary scan finds no differences we're done if start_offset == base.len_chars() && target.len_chars() == base.len_chars() { - return builder.into_delta(base, target); + return builder.into_delta(target); } let line_hashes = make_line_hashes(base, MIN_SIZE); @@ -198,7 +197,7 @@ pub fn compute_delta<'a>(base: &Rope, target: &'a Rope) -> Delta<'a> { builder.copy(base.len_chars() - diff_end, target.len_chars() - diff_end, diff_end); } - builder.into_delta(base, target) + builder.into_delta(target) } /// Given two ropes and the offsets of two equal bytes, finds the largest @@ -295,7 +294,7 @@ impl DiffBuilder { self.ops.push(DiffOp { target_idx: target, base_idx: base, len }) } - fn into_delta<'a>(self, base: &Rope, target: &'a Rope) -> Delta<'a> { + fn into_delta(self, target: &Rope) -> Delta<'_> { let mut els = Vec::with_capacity(self.ops.len() * 2); let mut targ_pos = 0; for DiffOp { base_idx, target_idx, len } in self.ops { @@ -312,7 +311,7 @@ impl DiffBuilder { els.push(DeltaElement::Insert(slice)); } - Delta { els, base_len: base.len_chars() } + Delta { els } } } diff --git a/crates/modalkit/src/editing/rope/mod.rs b/crates/modalkit/src/editing/rope/mod.rs index 8729fc1..699fc27 100644 --- a/crates/modalkit/src/editing/rope/mod.rs +++ b/crates/modalkit/src/editing/rope/mod.rs @@ -9,7 +9,7 @@ //! [prelude]: crate::prelude use std::borrow::Cow; use std::cmp::{Ord, Ordering, PartialOrd}; -use std::fmt::Debug; +use std::fmt::{self, Debug, Display}; use std::io::Write; use std::ops::{Add, AddAssign, Bound, Range, RangeBounds}; @@ -2313,9 +2313,9 @@ impl PartialEq for EditRope { impl Eq for EditRope {} -impl ToString for EditRope { - fn to_string(&self) -> String { - self.rope.to_string() +impl Display for EditRope { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", &self.rope) } } diff --git a/crates/modalkit/src/editing/store/cursor.rs b/crates/modalkit/src/editing/store/cursor.rs index d0b5c98..d88d579 100644 --- a/crates/modalkit/src/editing/store/cursor.rs +++ b/crates/modalkit/src/editing/store/cursor.rs @@ -175,7 +175,7 @@ where Err(EditError::WrongBuffer(owner)) } } else if let Some(bstore) = self.buffer.get(&id) { - bstore.get(mark).map(Cursor::clone).ok_or(unset) + bstore.get(mark).cloned().ok_or(unset) } else { Err(unset) } diff --git a/crates/modalkit/src/key/mod.rs b/crates/modalkit/src/key/mod.rs index 58c84e2..843e486 100644 --- a/crates/modalkit/src/key/mod.rs +++ b/crates/modalkit/src/key/mod.rs @@ -4,6 +4,7 @@ //! //! This module contains code for representing and processing keys. //! +use std::fmt::{self, Display}; use std::hash::Hash; use std::str::FromStr; @@ -152,74 +153,72 @@ impl FromStr for TerminalKey { } } -impl ToString for TerminalKey { - fn to_string(&self) -> String { - let mut res = String::new(); - - let push_mods = |res: &mut String, mods: KeyModifiers| { +impl Display for TerminalKey { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let push_mods = |f: &mut fmt::Formatter, mods: KeyModifiers| -> fmt::Result { if mods.contains(KeyModifiers::CONTROL) { - res.push_str("C-"); + write!(f, "C-")?; } if mods.contains(KeyModifiers::ALT) { - res.push_str("A-"); + write!(f, "A-")?; } if mods.contains(KeyModifiers::SHIFT) { - res.push_str("S-"); + write!(f, "S-")?; } + + Ok(()) }; - let push_named_mods = |res: &mut String, name: &str, mods| { - res.push('<'); - push_mods(res, mods); - res.push_str(name); - res.push('>'); + let push_named_mods = |f: &mut fmt::Formatter, name: &str, mods| -> fmt::Result { + write!(f, "<") + .and_then(|()| push_mods(f, mods)) + .and_then(|()| write!(f, "{}>", name)) }; - let push_named = |res: &mut String, name: &str| push_named_mods(res, name, self.modifiers); + let push_named = + |f: &mut fmt::Formatter, name: &str| push_named_mods(f, name, self.modifiers); match self.code { - KeyCode::Left => push_named(&mut res, "Left"), - KeyCode::Right => push_named(&mut res, "Right"), - KeyCode::Up => push_named(&mut res, "Up"), - KeyCode::Down => push_named(&mut res, "Down"), - KeyCode::Backspace => push_named(&mut res, "BS"), - KeyCode::Enter => push_named(&mut res, "Enter"), - KeyCode::Home => push_named(&mut res, "Home"), - KeyCode::End => push_named(&mut res, "End"), - KeyCode::PageUp => push_named(&mut res, "PageUp"), - KeyCode::PageDown => push_named(&mut res, "PageDown"), - KeyCode::Null => push_named(&mut res, "Nul"), - KeyCode::Esc => push_named(&mut res, "Esc"), - KeyCode::Delete => push_named(&mut res, "Del"), - KeyCode::Insert => push_named(&mut res, "Insert"), - KeyCode::CapsLock => push_named(&mut res, "CapsLock"), - KeyCode::ScrollLock => push_named(&mut res, "ScrollLock"), - KeyCode::NumLock => push_named(&mut res, "NumLock"), - KeyCode::PrintScreen => push_named(&mut res, "PrintScreen"), - KeyCode::Pause => push_named(&mut res, "Pause"), - KeyCode::Menu => push_named(&mut res, "Menu"), - KeyCode::Tab => push_named(&mut res, "Tab"), - KeyCode::BackTab => { - push_named_mods(&mut res, "Tab", self.modifiers | KeyModifiers::SHIFT) - }, + KeyCode::Left => push_named(f, "Left"), + KeyCode::Right => push_named(f, "Right"), + KeyCode::Up => push_named(f, "Up"), + KeyCode::Down => push_named(f, "Down"), + KeyCode::Backspace => push_named(f, "BS"), + KeyCode::Enter => push_named(f, "Enter"), + KeyCode::Home => push_named(f, "Home"), + KeyCode::End => push_named(f, "End"), + KeyCode::PageUp => push_named(f, "PageUp"), + KeyCode::PageDown => push_named(f, "PageDown"), + KeyCode::Null => push_named(f, "Nul"), + KeyCode::Esc => push_named(f, "Esc"), + KeyCode::Delete => push_named(f, "Del"), + KeyCode::Insert => push_named(f, "Insert"), + KeyCode::CapsLock => push_named(f, "CapsLock"), + KeyCode::ScrollLock => push_named(f, "ScrollLock"), + KeyCode::NumLock => push_named(f, "NumLock"), + KeyCode::PrintScreen => push_named(f, "PrintScreen"), + KeyCode::Pause => push_named(f, "Pause"), + KeyCode::Menu => push_named(f, "Menu"), + KeyCode::Tab => push_named(f, "Tab"), + KeyCode::BackTab => push_named_mods(f, "Tab", self.modifiers | KeyModifiers::SHIFT), KeyCode::F(n) => { let n = n.to_string(); - push_named(&mut res, n.as_str()); + push_named(f, n.as_str()) }, KeyCode::Char(c) => { if c == ' ' { if self.modifiers.is_empty() { - res.push(c); + write!(f, "{c}") } else { - push_named(&mut res, "Space"); + push_named(f, "Space") } } else if c == '<' { - push_named(&mut res, "lt"); + push_named(f, "lt") } else if (self.modifiers - KeyModifiers::SHIFT).is_empty() { - res.push(c); + write!(f, "{c}") } else { let c = if self.modifiers.intersects(KeyModifiers::CONTROL | KeyModifiers::SHIFT) { @@ -228,7 +227,7 @@ impl ToString for TerminalKey { c.to_string() }; - push_named(&mut res, c.as_str()); + push_named(f, c.as_str()) } }, KeyCode::Media(mc) => { @@ -248,14 +247,13 @@ impl ToString for TerminalKey { MediaKeyCode::MuteVolume => "MediaVolumeMute", }; - push_named(&mut res, name); + push_named(f, name) }, KeyCode::Modifier(_) | KeyCode::KeypadBegin => { // Do nothing with these for now. + Ok(()) }, } - - return res; } }