From f05a69a9d97366300d57ac301b45ee5df3747710 Mon Sep 17 00:00:00 2001 From: dsgallups Date: Wed, 22 Jan 2025 16:29:02 -0500 Subject: [PATCH] resolve all lints, update MSRV --- Cargo.toml | 2 +- examples/rich-text/src/main.rs | 356 ++++++++++++++++----------------- src/attrs.rs | 1 + src/buffer.rs | 23 ++- src/buffer_line.rs | 2 + src/cached.rs | 1 + src/cursor.rs | 4 +- src/edit/editor.rs | 18 +- src/edit/mod.rs | 12 +- src/edit/syntect.rs | 8 +- src/edit/vi.rs | 77 +++---- src/font/fallback/mod.rs | 2 +- src/font/mod.rs | 4 +- src/font/system.rs | 6 +- src/layout.rs | 4 +- src/line_ending.rs | 4 +- src/shape.rs | 8 +- src/shape_run_cache.rs | 2 +- 18 files changed, 251 insertions(+), 283 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7dcfea8..edef8fb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" license = "MIT OR Apache-2.0" documentation = "https://docs.rs/cosmic-text/latest/cosmic_text/" repository = "https://github.com/pop-os/cosmic-text" -rust-version = "1.65" +rust-version = "1.66" [dependencies] bitflags = "2.4.1" diff --git a/examples/rich-text/src/main.rs b/examples/rich-text/src/main.rs index 9df0981..285cea9 100644 --- a/examples/rich-text/src/main.rs +++ b/examples/rich-text/src/main.rs @@ -19,7 +19,7 @@ use winit::{ window::WindowBuilder, }; -fn set_buffer_text<'a>(buffer: &mut BorrowedWithFontSystem<'a, Buffer>) { +fn set_buffer_text(buffer: &mut BorrowedWithFontSystem<'_, Buffer>) { let attrs = Attrs::new(); let serif_attrs = attrs.family(Family::Serif); let mono_attrs = attrs.family(Family::Monospace); @@ -126,7 +126,7 @@ fn main() { Some(window.inner_size().height as f32), ) }); - editor.with_buffer_mut(|buffer| set_buffer_text(buffer)); + editor.with_buffer_mut(set_buffer_text); let mut ctrl_pressed = false; let mut mouse_x = 0.0; @@ -144,207 +144,195 @@ fn main() { .run(|event, elwt| { elwt.set_control_flow(ControlFlow::Wait); + let Event::WindowEvent { window_id, event } = event else { + return; + }; + match event { - Event::WindowEvent { window_id, event } => { - match event { - WindowEvent::ScaleFactorChanged { scale_factor, .. } => { - log::info!("Updated scale factor for {window_id:?}"); + WindowEvent::ScaleFactorChanged { scale_factor, .. } => { + log::info!("Updated scale factor for {window_id:?}"); - display_scale = scale_factor as f32; - editor.with_buffer_mut(|buffer| { - buffer.set_metrics(metrics.scale(display_scale)) - }); + display_scale = scale_factor as f32; + editor + .with_buffer_mut(|buffer| buffer.set_metrics(metrics.scale(display_scale))); - window.request_redraw(); - } - WindowEvent::RedrawRequested => { - let (width, height) = { - let size = window.inner_size(); - (size.width, size.height) - }; + window.request_redraw(); + } + WindowEvent::RedrawRequested => { + let (width, height) = { + let size = window.inner_size(); + (size.width, size.height) + }; - surface - .resize( - NonZeroU32::new(width).unwrap(), - NonZeroU32::new(height).unwrap(), - ) - .unwrap(); + surface + .resize( + NonZeroU32::new(width).unwrap(), + NonZeroU32::new(height).unwrap(), + ) + .unwrap(); - let mut surface_buffer = surface.buffer_mut().unwrap(); - let surface_buffer_u8 = unsafe { - slice::from_raw_parts_mut( - surface_buffer.as_mut_ptr() as *mut u8, - surface_buffer.len() * 4, - ) - }; - let mut pixmap = - PixmapMut::from_bytes(surface_buffer_u8, width, height).unwrap(); - pixmap.fill(bg_color); + let mut surface_buffer = surface.buffer_mut().unwrap(); + let surface_buffer_u8 = unsafe { + slice::from_raw_parts_mut( + surface_buffer.as_mut_ptr() as *mut u8, + surface_buffer.len() * 4, + ) + }; + let mut pixmap = + PixmapMut::from_bytes(surface_buffer_u8, width, height).unwrap(); + pixmap.fill(bg_color); - editor.with_buffer_mut(|buffer| { - buffer.set_size(Some(width as f32), Some(height as f32)) - }); + editor.with_buffer_mut(|buffer| { + buffer.set_size(Some(width as f32), Some(height as f32)) + }); - let mut paint = Paint::default(); - paint.anti_alias = false; - editor.shape_as_needed(true); + let mut paint = Paint { + anti_alias: false, + ..Default::default() + }; + editor.shape_as_needed(true); - editor.draw( - &mut swash_cache, - font_color, - cursor_color, - selection_color, - selected_text_color, - |x, y, w, h, color| { - // Note: due to softbuffer and tiny_skia having incompatible internal color representations we swap - // the red and blue channels here - paint.set_color_rgba8( - color.b(), - color.g(), - color.r(), - color.a(), - ); - pixmap.fill_rect( - Rect::from_xywh(x as f32, y as f32, w as f32, h as f32) - .unwrap(), - &paint, - Transform::identity(), - None, - ); - }, + editor.draw( + &mut swash_cache, + font_color, + cursor_color, + selection_color, + selected_text_color, + |x, y, w, h, color| { + // Note: due to softbuffer and tiny_skia having incompatible internal color representations we swap + // the red and blue channels here + paint.set_color_rgba8(color.b(), color.g(), color.r(), color.a()); + pixmap.fill_rect( + Rect::from_xywh(x as f32, y as f32, w as f32, h as f32).unwrap(), + &paint, + Transform::identity(), + None, ); + }, + ); - surface_buffer.present().unwrap(); - } - WindowEvent::ModifiersChanged(modifiers) => { - ctrl_pressed = modifiers.state().control_key() - } - WindowEvent::KeyboardInput { event, .. } => { - let KeyEvent { - logical_key, state, .. - } = event; + surface_buffer.present().unwrap(); + } + WindowEvent::ModifiersChanged(modifiers) => { + ctrl_pressed = modifiers.state().control_key() + } + WindowEvent::KeyboardInput { event, .. } => { + let KeyEvent { + logical_key, state, .. + } = event; - if state.is_pressed() { - match logical_key { - Key::Named(NamedKey::ArrowLeft) => { - editor.action(Action::Motion(Motion::Left)) - } - Key::Named(NamedKey::ArrowRight) => { - editor.action(Action::Motion(Motion::Right)) - } - Key::Named(NamedKey::ArrowUp) => { - editor.action(Action::Motion(Motion::Up)) - } - Key::Named(NamedKey::ArrowDown) => { - editor.action(Action::Motion(Motion::Down)) - } - Key::Named(NamedKey::Home) => { - editor.action(Action::Motion(Motion::Home)) - } - Key::Named(NamedKey::End) => { - editor.action(Action::Motion(Motion::End)) - } - Key::Named(NamedKey::PageUp) => { - editor.action(Action::Motion(Motion::PageUp)) - } - Key::Named(NamedKey::PageDown) => { - editor.action(Action::Motion(Motion::PageDown)) - } - Key::Named(NamedKey::Escape) => editor.action(Action::Escape), - Key::Named(NamedKey::Enter) => editor.action(Action::Enter), - Key::Named(NamedKey::Backspace) => { - editor.action(Action::Backspace) - } - Key::Named(NamedKey::Delete) => editor.action(Action::Delete), - Key::Named(key) => { - if let Some(text) = key.to_text() { - for c in text.chars() { - editor.action(Action::Insert(c)); - } - } - } - Key::Character(text) => { - if !ctrl_pressed { - for c in text.chars() { - editor.action(Action::Insert(c)); - } - } - } - _ => {} - } - window.request_redraw(); + if state.is_pressed() { + match logical_key { + Key::Named(NamedKey::ArrowLeft) => { + editor.action(Action::Motion(Motion::Left)) } - } - WindowEvent::CursorMoved { - device_id: _, - position, - } => { - // Update saved mouse position for use when handling click events - mouse_x = position.x; - mouse_y = position.y; - - // Implement dragging - if mouse_left.is_pressed() { - // Execute Drag editor action (update selection) - editor.action(Action::Drag { - x: position.x as i32, - y: position.y as i32, - }); - - // Scroll if cursor is near edge of window while dragging - if mouse_y <= 5.0 { - editor.action(Action::Scroll { lines: -1 }); - } else if mouse_y - 5.0 >= window.inner_size().height as f64 { - editor.action(Action::Scroll { lines: 1 }); - } - - window.request_redraw(); + Key::Named(NamedKey::ArrowRight) => { + editor.action(Action::Motion(Motion::Right)) } - } - WindowEvent::MouseInput { - device_id: _, - state, - button, - } => { - if button == MouseButton::Left { - if state == ElementState::Pressed - && mouse_left == ElementState::Released - { - editor.action(Action::Click { - x: mouse_x /*- line_x*/ as i32, - y: mouse_y as i32, - }); - window.request_redraw(); - } - mouse_left = state; + Key::Named(NamedKey::ArrowUp) => { + editor.action(Action::Motion(Motion::Up)) } - } - WindowEvent::MouseWheel { - device_id: _, - delta, - phase: _, - } => { - let line_delta = match delta { - MouseScrollDelta::LineDelta(_x, y) => y as i32, - MouseScrollDelta::PixelDelta(PhysicalPosition { x: _, y }) => { - unapplied_scroll_delta += y; - let line_delta = (unapplied_scroll_delta / 20.0).floor(); - unapplied_scroll_delta -= line_delta * 20.0; - line_delta as i32 - } - }; - if line_delta != 0 { - editor.action(Action::Scroll { lines: -line_delta }); + Key::Named(NamedKey::ArrowDown) => { + editor.action(Action::Motion(Motion::Down)) } + Key::Named(NamedKey::Home) => { + editor.action(Action::Motion(Motion::Home)) + } + Key::Named(NamedKey::End) => editor.action(Action::Motion(Motion::End)), + Key::Named(NamedKey::PageUp) => { + editor.action(Action::Motion(Motion::PageUp)) + } + Key::Named(NamedKey::PageDown) => { + editor.action(Action::Motion(Motion::PageDown)) + } + Key::Named(NamedKey::Escape) => editor.action(Action::Escape), + Key::Named(NamedKey::Enter) => editor.action(Action::Enter), + Key::Named(NamedKey::Backspace) => editor.action(Action::Backspace), + Key::Named(NamedKey::Delete) => editor.action(Action::Delete), + Key::Named(key) => { + if let Some(text) = key.to_text() { + for c in text.chars() { + editor.action(Action::Insert(c)); + } + } + } + Key::Character(text) => { + if !ctrl_pressed { + for c in text.chars() { + editor.action(Action::Insert(c)); + } + } + } + _ => {} + } + window.request_redraw(); + } + } + WindowEvent::CursorMoved { + device_id: _, + position, + } => { + // Update saved mouse position for use when handling click events + mouse_x = position.x; + mouse_y = position.y; + + // Implement dragging + if mouse_left.is_pressed() { + // Execute Drag editor action (update selection) + editor.action(Action::Drag { + x: position.x as i32, + y: position.y as i32, + }); + + // Scroll if cursor is near edge of window while dragging + if mouse_y <= 5.0 { + editor.action(Action::Scroll { lines: -1 }); + } else if mouse_y - 5.0 >= window.inner_size().height as f64 { + editor.action(Action::Scroll { lines: 1 }); + } + + window.request_redraw(); + } + } + WindowEvent::MouseInput { + device_id: _, + state, + button, + } => { + if button == MouseButton::Left { + if state == ElementState::Pressed && mouse_left == ElementState::Released { + editor.action(Action::Click { + x: mouse_x /*- line_x*/ as i32, + y: mouse_y as i32, + }); window.request_redraw(); } - WindowEvent::CloseRequested => { - //TODO: just close one window - elwt.exit(); - } - _ => {} + mouse_left = state; } } + WindowEvent::MouseWheel { + device_id: _, + delta, + phase: _, + } => { + let line_delta = match delta { + MouseScrollDelta::LineDelta(_x, y) => y as i32, + MouseScrollDelta::PixelDelta(PhysicalPosition { x: _, y }) => { + unapplied_scroll_delta += y; + let line_delta = (unapplied_scroll_delta / 20.0).floor(); + unapplied_scroll_delta -= line_delta * 20.0; + line_delta as i32 + } + }; + if line_delta != 0 { + editor.action(Action::Scroll { lines: -line_delta }); + } + window.request_redraw(); + } + WindowEvent::CloseRequested => { + //TODO: just close one window + elwt.exit(); + } _ => {} } }) diff --git a/src/attrs.rs b/src/attrs.rs index 782985b..d7acc1c 100644 --- a/src/attrs.rs +++ b/src/attrs.rs @@ -340,6 +340,7 @@ impl AttrsList { } /// Split attributes list at an offset + #[allow(clippy::missing_panics_doc)] pub fn split_off(&mut self, index: usize) -> Self { let mut new = Self::new(self.defaults.as_attrs()); let mut removes = Vec::new(); diff --git a/src/buffer.rs b/src/buffer.rs index e40a3eb..f9c9792 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -32,11 +32,12 @@ pub struct LayoutRun<'a> { pub line_w: f32, } -impl<'a> LayoutRun<'a> { +impl LayoutRun<'_> { /// Return the pixel span `Some((x_left, x_width))` of the highlighted area between `cursor_start` /// and `cursor_end` within this run, or None if the cursor range does not intersect this run. /// This may return widths of zero if `cursor_start == cursor_end`, if the run is empty, or if the /// region's left start boundary is the same as the cursor's end boundary or vice versa. + #[allow(clippy::missing_panics_doc)] pub fn highlight(&self, cursor_start: Cursor, cursor_end: Cursor) -> Option<(f32, f32)> { let mut x_start = None; let mut x_end = None; @@ -202,7 +203,7 @@ impl fmt::Display for Metrics { /// A buffer of text that is shaped and laid out #[derive(Debug)] pub struct Buffer { - /// [BufferLine]s (or paragraphs) of text in the buffer + /// [`BufferLine`]s (or paragraphs) of text in the buffer pub lines: Vec, metrics: Metrics, width_opt: Option, @@ -305,6 +306,7 @@ impl Buffer { } /// Shape lines until cursor, also scrolling to include cursor in view + #[allow(clippy::missing_panics_doc)] pub fn shape_until_cursor( &mut self, font_system: &mut FontSystem, @@ -323,11 +325,11 @@ impl Buffer { let layout = self .line_layout(font_system, layout_cursor.line) .expect("shape_until_cursor failed to scroll forwards"); - for layout_i in 0..layout_cursor.layout { + (0..layout_cursor.layout).for_each(|layout_i| { layout_y += layout[layout_i] .line_height_opt .unwrap_or(metrics.line_height); - } + }); layout_y + layout[layout_cursor.layout] .line_height_opt @@ -403,6 +405,7 @@ impl Buffer { } /// Shape lines until scroll + #[allow(clippy::missing_panics_doc)] pub fn shape_until_scroll(&mut self, font_system: &mut FontSystem, prune: bool) { let metrics = self.metrics; let old_scroll = self.scroll; @@ -603,10 +606,8 @@ impl Buffer { self.tab_width = tab_width; // Shaping must be reset when tab width is changed for line in self.lines.iter_mut() { - if line.shape_opt().is_some() { - if line.text().contains('\t') { - line.reset_shaping(); - } + if line.shape_opt().is_some() && line.text().contains('\t') { + line.reset_shaping(); } } self.redraw = true; @@ -1095,7 +1096,7 @@ impl Buffer { layout_cursor.layout -= 1; } else if layout_cursor.line > 0 { layout_cursor.line -= 1; - layout_cursor.layout = usize::max_value(); + layout_cursor.layout = usize::MAX; } if let Some(cursor_x) = cursor_x_opt { @@ -1164,7 +1165,7 @@ impl Buffer { } Motion::End => { let mut layout_cursor = self.layout_cursor(font_system, cursor)?; - layout_cursor.glyph = usize::max_value(); + layout_cursor.glyph = usize::MAX; #[allow(unused_assignments)] { (cursor, cursor_x_opt) = self.cursor_motion( @@ -1365,7 +1366,7 @@ impl Buffer { } } -impl<'a> BorrowedWithFontSystem<'a, Buffer> { +impl BorrowedWithFontSystem<'_, Buffer> { /// Shape lines until cursor, also scrolling to include cursor in view pub fn shape_until_cursor(&mut self, cursor: Cursor, prune: bool) { self.inner diff --git a/src/buffer_line.rs b/src/buffer_line.rs index 015c5e8..36e7195 100644 --- a/src/buffer_line.rs +++ b/src/buffer_line.rs @@ -203,6 +203,7 @@ impl BufferLine { } /// Shape line, will cache results + #[allow(clippy::missing_panics_doc)] pub fn shape(&mut self, font_system: &mut FontSystem, tab_width: u16) -> &ShapeLine { if self.shape_opt.is_unused() { let mut line = self @@ -228,6 +229,7 @@ impl BufferLine { } /// Layout line, will cache results + #[allow(clippy::missing_panics_doc)] pub fn layout( &mut self, font_system: &mut FontSystem, diff --git a/src/cached.rs b/src/cached.rs index 7e26612..cf7e1ac 100644 --- a/src/cached.rs +++ b/src/cached.rs @@ -67,6 +67,7 @@ impl Cached { } /// Moves the value from `Self::Used` to `Self::Unused`. + #[allow(clippy::missing_panics_doc)] pub fn set_unused(&mut self) { if matches!(*self, Self::Used(_)) { *self = Self::Unused(self.take_used().expect("cached value should be used")); diff --git a/src/cursor.rs b/src/cursor.rs index b1608fb..e6358c6 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -87,9 +87,9 @@ impl LayoutCursor { pub enum Motion { /// Apply specific [`LayoutCursor`] LayoutCursor(LayoutCursor), - /// Move cursor to previous character ([Self::Left] in LTR, [Self::Right] in RTL) + /// Move cursor to previous character ([`Self::Left`] in LTR, [`Self::Right`] in RTL) Previous, - /// Move cursor to next character ([Self::Right] in LTR, [Self::Left] in RTL) + /// Move cursor to next character ([`Self::Right`] in LTR, [`Self::Left`] in RTL) Next, /// Move cursor left Left, diff --git a/src/edit/editor.rs b/src/edit/editor.rs index 1231002..a3e85da 100644 --- a/src/edit/editor.rs +++ b/src/edit/editor.rs @@ -109,6 +109,7 @@ impl<'buffer> Editor<'buffer> { /// Draw the editor #[cfg(feature = "swash")] + #[allow(clippy::too_many_arguments)] pub fn draw( &self, font_system: &mut FontSystem, @@ -527,16 +528,13 @@ impl<'buffer> Edit<'buffer> for Editor<'buffer> { fn apply_change(&mut self, change: &Change) -> bool { // Cannot apply changes if there is a pending change - match self.change.take() { - Some(pending) => { - if !pending.items.is_empty() { - //TODO: is this a good idea? - log::warn!("pending change caused apply_change to be ignored!"); - self.change = Some(pending); - return false; - } + if let Some(pending) = self.change.take() { + if !pending.items.is_empty() { + //TODO: is this a good idea? + log::warn!("pending change caused apply_change to be ignored!"); + self.change = Some(pending); + return false; } - None => {} } for item in change.items.iter() { @@ -898,7 +896,7 @@ impl<'buffer> Edit<'buffer> for Editor<'buffer> { } } -impl<'font_system, 'buffer> BorrowedWithFontSystem<'font_system, Editor<'buffer>> { +impl BorrowedWithFontSystem<'_, Editor<'_>> { #[cfg(feature = "swash")] pub fn draw( &mut self, diff --git a/src/edit/mod.rs b/src/edit/mod.rs index d9d4fd1..a27fa41 100644 --- a/src/edit/mod.rs +++ b/src/edit/mod.rs @@ -71,7 +71,7 @@ pub enum BufferRef<'buffer> { Arc(Arc), } -impl<'buffer> Clone for BufferRef<'buffer> { +impl Clone for BufferRef<'_> { fn clone(&self) -> Self { match self { Self::Owned(buffer) => Self::Owned(buffer.clone()), @@ -81,7 +81,7 @@ impl<'buffer> Clone for BufferRef<'buffer> { } } -impl<'buffer> From for BufferRef<'buffer> { +impl From for BufferRef<'_> { fn from(buffer: Buffer) -> Self { Self::Owned(buffer) } @@ -93,7 +93,7 @@ impl<'buffer> From<&'buffer mut Buffer> for BufferRef<'buffer> { } } -impl<'buffer> From> for BufferRef<'buffer> { +impl From> for BufferRef<'_> { fn from(arc: Arc) -> Self { Self::Arc(arc) } @@ -197,7 +197,7 @@ pub trait Edit<'buffer> { /// Set the [`Buffer`] redraw flag fn set_redraw(&mut self, redraw: bool) { - self.with_buffer_mut(|buffer| buffer.set_redraw(redraw)) + self.with_buffer_mut(|buffer| buffer.set_redraw(redraw)); } /// Get the current cursor @@ -300,7 +300,7 @@ pub trait Edit<'buffer> { /// Delete text starting at start Cursor and ending at end Cursor fn delete_range(&mut self, start: Cursor, end: Cursor); - /// Insert text at specified cursor with specified attrs_list + /// Insert text at specified cursor with specified `attrs_list` fn insert_at(&mut self, cursor: Cursor, data: &str, attrs_list: Option) -> Cursor; /// Copy selection @@ -334,7 +334,7 @@ pub trait Edit<'buffer> { fn cursor_position(&self) -> Option<(i32, i32)>; } -impl<'font_system, 'buffer, E: Edit<'buffer>> BorrowedWithFontSystem<'font_system, E> { +impl<'buffer, E: Edit<'buffer>> BorrowedWithFontSystem<'_, E> { /// Get the internal [`Buffer`], mutably pub fn with_buffer_mut) -> T, T>( &mut self, diff --git a/src/edit/syntect.rs b/src/edit/syntect.rs index d6ef2d8..b532c96 100644 --- a/src/edit/syntect.rs +++ b/src/edit/syntect.rs @@ -125,7 +125,7 @@ impl<'syntax_system, 'buffer> SyntaxEditor<'syntax_system, 'buffer> { let text = fs::read_to_string(path)?; self.editor.with_buffer_mut(|buffer| { - buffer.set_text(font_system, &text, attrs, Shaping::Advanced) + buffer.set_text(font_system, &text, attrs, Shaping::Advanced); }); //TODO: re-use text @@ -235,7 +235,7 @@ impl<'syntax_system, 'buffer> SyntaxEditor<'syntax_system, 'buffer> { } } -impl<'syntax_system, 'buffer> Edit<'buffer> for SyntaxEditor<'syntax_system, 'buffer> { +impl<'buffer> Edit<'buffer> for SyntaxEditor<'_, 'buffer> { fn buffer_ref(&self) -> &BufferRef<'buffer> { self.editor.buffer_ref() } @@ -440,9 +440,7 @@ impl<'syntax_system, 'buffer> Edit<'buffer> for SyntaxEditor<'syntax_system, 'bu } } -impl<'font_system, 'syntax_system, 'buffer> - BorrowedWithFontSystem<'font_system, SyntaxEditor<'syntax_system, 'buffer>> -{ +impl BorrowedWithFontSystem<'_, SyntaxEditor<'_, '_>> { /// Load text from a file, and also set syntax to the best option /// /// ## Errors diff --git a/src/edit/vi.rs b/src/edit/vi.rs index 08dfd48..0719e7b 100644 --- a/src/edit/vi.rs +++ b/src/edit/vi.rs @@ -209,6 +209,9 @@ impl<'syntax_system, 'buffer> ViEditor<'syntax_system, 'buffer> { } /// Load text from a file, and also set syntax to the best option + /// + /// ## Errors + /// Returns an `io::Error` if reading the file fails #[cfg(feature = "std")] pub fn load_text>( &mut self, @@ -517,7 +520,7 @@ impl<'syntax_system, 'buffer> ViEditor<'syntax_system, 'buffer> { } } -impl<'syntax_system, 'buffer> Edit<'buffer> for ViEditor<'syntax_system, 'buffer> { +impl<'buffer> Edit<'buffer> for ViEditor<'_, 'buffer> { fn buffer_ref(&self) -> &BufferRef<'buffer> { self.editor.buffer_ref() } @@ -646,10 +649,7 @@ impl<'syntax_system, 'buffer> Edit<'buffer> for ViEditor<'syntax_system, 'buffer } }; - let has_selection = match editor.selection() { - Selection::None => false, - _ => true, - }; + let has_selection = !matches!(editor.selection(), Selection::None); self.parser.parse(key, has_selection, |event| { log::debug!(" Event {:?}", event); @@ -778,17 +778,14 @@ impl<'syntax_system, 'buffer> Edit<'buffer> for ViEditor<'syntax_system, 'buffer TextObject::DoubleQuotes => select_in(editor, '"', '"', include), TextObject::Parentheses => select_in(editor, '(', ')', include), TextObject::Search { forwards } => { - match &self.search_opt { - Some((value, _)) => { - if search(editor, value, forwards) { - let mut cursor = editor.cursor(); - editor.set_selection(Selection::Normal(cursor)); - //TODO: traverse lines if necessary - cursor.index += value.len(); - editor.set_cursor(cursor); - } + if let Some((value, _)) = &self.search_opt { + if search(editor, value, forwards) { + let mut cursor = editor.cursor(); + editor.set_selection(Selection::Normal(cursor)); + //TODO: traverse lines if necessary + cursor.index += value.len(); + editor.set_cursor(cursor); } - None => {} } } TextObject::SingleQuotes => select_in(editor, '\'', '\'', include), @@ -880,15 +877,11 @@ impl<'syntax_system, 'buffer> Edit<'buffer> for ViEditor<'syntax_system, 'buffer editor.with_buffer(|buffer| { let text = buffer.lines[cursor.line].text(); if cursor.index < text.len() { - match text[cursor.index..] + if let Some((i, _)) = text[cursor.index..] .char_indices() - .filter(|&(i, c)| i > 0 && c == find_c) - .next() + .find(|&(i, c)| i > 0 && c == find_c) { - Some((i, _)) => { - cursor.index += i; - } - None => {} + cursor.index += i; } } }); @@ -986,15 +979,11 @@ impl<'syntax_system, 'buffer> Edit<'buffer> for ViEditor<'syntax_system, 'buffer editor.with_buffer(|buffer| { let text = buffer.lines[cursor.line].text(); if cursor.index > 0 { - match text[..cursor.index] + if let Some((i, _)) = text[..cursor.index] .char_indices() - .filter(|&(_, c)| c == find_c) - .last() + .rfind(|&(_, c)| c == find_c) { - Some((i, _)) => { - cursor.index = i; - } - None => {} + cursor.index = i; } } }); @@ -1006,7 +995,7 @@ impl<'syntax_system, 'buffer> Edit<'buffer> for ViEditor<'syntax_system, 'buffer editor.with_buffer(|buffer| { let text = buffer.lines[cursor.line].text(); if cursor.index > 0 { - match text[..cursor.index] + if let Some(i) = text[..cursor.index] .char_indices() .filter_map(|(i, c)| { if c == find_c { @@ -1019,10 +1008,7 @@ impl<'syntax_system, 'buffer> Edit<'buffer> for ViEditor<'syntax_system, 'buffer }) .last() { - Some(i) => { - cursor.index = i; - } - None => {} + cursor.index = i; } } }); @@ -1132,16 +1118,12 @@ impl<'syntax_system, 'buffer> Edit<'buffer> for ViEditor<'syntax_system, 'buffer //TODO: is this efficient? let action_opt = editor.with_buffer(|buffer| { let mut layout_runs = buffer.layout_runs(); - if let Some(first) = layout_runs.next() { - if let Some(last) = layout_runs.last() { - Some(Action::Motion(Motion::GotoLine( - (last.line_i + first.line_i) / 2, - ))) - } else { - None - } - } else { - None + + match (layout_runs.next(), layout_runs.last()) { + (Some(first), Some(last)) => Some(Action::Motion( + Motion::GotoLine((last.line_i + first.line_i) / 2), + )), + _ => None, } }); match action_opt { @@ -1167,10 +1149,11 @@ impl<'syntax_system, 'buffer> Edit<'buffer> for ViEditor<'syntax_system, 'buffer } } -impl<'font_system, 'syntax_system, 'buffer> - BorrowedWithFontSystem<'font_system, ViEditor<'syntax_system, 'buffer>> -{ +impl BorrowedWithFontSystem<'_, ViEditor<'_, '_>> { /// Load text from a file, and also set syntax to the best option + /// + /// ## Errors + /// Returns an `io::Error` if reading the file fails #[cfg(feature = "std")] pub fn load_text>( &mut self, diff --git a/src/font/fallback/mod.rs b/src/font/fallback/mod.rs index 048b2c3..af67f46 100644 --- a/src/font/fallback/mod.rs +++ b/src/font/fallback/mod.rs @@ -140,7 +140,7 @@ impl<'a> FontFallbackIter<'a> { } } -impl<'a> Iterator for FontFallbackIter<'a> { +impl Iterator for FontFallbackIter<'_> { type Item = Arc; fn next(&mut self) -> Option { if let Some(fallback_info) = self.font_system.monospace_fallbacks_buffer.pop_first() { diff --git a/src/font/mod.rs b/src/font/mod.rs index 252a2a0..4a14595 100644 --- a/src/font/mod.rs +++ b/src/font/mod.rs @@ -185,11 +185,11 @@ mod test { let now = std::time::Instant::now(); let mut db = fontdb::Database::new(); - let locale = get_locale().unwrap(); + let locale = get_locale().expect("Local available"); db.load_system_fonts(); FontSystem::new_with_locale_and_db(locale, db); #[cfg(not(target_arch = "wasm32"))] - println!("Fonts load time {}ms.", now.elapsed().as_millis()) + println!("Fonts load time {}ms.", now.elapsed().as_millis()); } } diff --git a/src/font/system.rs b/src/font/system.rs index c4d5662..275b0a8 100644 --- a/src/font/system.rs +++ b/src/font/system.rs @@ -106,7 +106,7 @@ pub struct FontSystem { /// Scratch buffer for shaping and laying out. pub(crate) shape_buffer: ShapeBuffer, - /// Buffer for use in FontFallbackIter. + /// Buffer for use in `FontFallbackIter`. pub(crate) monospace_fallbacks_buffer: BTreeSet, /// Cache for shaped runs @@ -372,7 +372,7 @@ pub struct BorrowedWithFontSystem<'a, T> { pub(crate) font_system: &'a mut FontSystem, } -impl<'a, T> Deref for BorrowedWithFontSystem<'a, T> { +impl Deref for BorrowedWithFontSystem<'_, T> { type Target = T; fn deref(&self) -> &Self::Target { @@ -380,7 +380,7 @@ impl<'a, T> Deref for BorrowedWithFontSystem<'a, T> { } } -impl<'a, T> DerefMut for BorrowedWithFontSystem<'a, T> { +impl DerefMut for BorrowedWithFontSystem<'_, T> { fn deref_mut(&mut self) -> &mut Self::Target { self.inner } diff --git a/src/layout.rs b/src/layout.rs index b1bcca4..5f7287d 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -28,7 +28,7 @@ pub struct LayoutGlyph { pub y: f32, /// Width of hitbox pub w: f32, - /// Unicode BiDi embedding level, character is left-to-right if `level` is divisible by 2 + /// Unicode `BiDi` embedding level, character is left-to-right if `level` is divisible by 2 pub level: unicode_bidi::Level, /// X offset in line /// @@ -58,7 +58,7 @@ pub struct LayoutGlyph { #[derive(Clone, Debug)] pub struct PhysicalGlyph { - /// Cache key, see [CacheKey] + /// Cache key, see [`CacheKey`] pub cache_key: CacheKey, /// Integer component of X offset in line pub x: i32, diff --git a/src/line_ending.rs b/src/line_ending.rs index 7c6de2e..13c1488 100644 --- a/src/line_ending.rs +++ b/src/line_ending.rs @@ -48,11 +48,11 @@ impl<'a> LineIter<'a> { } } -impl<'a> Iterator for LineIter<'a> { +impl Iterator for LineIter<'_> { type Item = (Range, LineEnding); fn next(&mut self) -> Option { let start = self.start; - match self.string[start..self.end].find(&['\r', '\n']) { + match self.string[start..self.end].find(['\r', '\n']) { Some(i) => { let end = start + i; self.start = end; diff --git a/src/shape.rs b/src/shape.rs index 7b24fdf..5a38d4f 100644 --- a/src/shape.rs +++ b/src/shape.rs @@ -384,12 +384,8 @@ fn shape_run_cached( // Skip if attrs matches default attrs continue; } - let start = max(attrs_range.start, start_run) - .checked_sub(start_run) - .unwrap_or(0); - let end = min(attrs_range.end, end_run) - .checked_sub(start_run) - .unwrap_or(0); + let start = max(attrs_range.start, start_run).saturating_sub(start_run); + let end = min(attrs_range.end, end_run).saturating_sub(start_run); if end > start { let range = start..end; key.attrs_spans.push((range, attrs.clone())); diff --git a/src/shape_run_cache.rs b/src/shape_run_cache.rs index 226deee..2e1ffc7 100644 --- a/src/shape_run_cache.rs +++ b/src/shape_run_cache.rs @@ -33,7 +33,7 @@ impl ShapeRunCache { self.cache.insert(key, (self.age, glyphs)); } - /// Remove anything in the cache with an age older than keep_ages + /// Remove anything in the cache with an age older than `keep_ages` pub fn trim(&mut self, keep_ages: u64) { self.cache .retain(|_key, (age, _glyphs)| *age + keep_ages >= self.age);