From 485497973fbb6e1828e6da9a7f486257efb67b31 Mon Sep 17 00:00:00 2001 From: dsgallups Date: Wed, 22 Jan 2025 16:54:18 -0500 Subject: [PATCH] chore: updating newfound lints --- examples/editor/src/main.rs | 489 ++++++++++++++++++------------------ src/shape.rs | 3 +- 2 files changed, 243 insertions(+), 249 deletions(-) diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs index 1ce0758c2e..9b1332212a 100644 --- a/examples/editor/src/main.rs +++ b/examples/editor/src/main.rs @@ -17,7 +17,7 @@ use winit::{ fn main() { env_logger::init(); - let path = env::args().nth(1).unwrap_or(String::new()); + let path = env::args().nth(1).unwrap_or_default(); let event_loop = EventLoop::new().unwrap(); let window = Rc::new(WindowBuilder::new().build(&event_loop).unwrap()); @@ -71,282 +71,275 @@ 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(font_sizes[font_size_i].scale(display_scale)) - }); + display_scale = scale_factor as f32; + editor.with_buffer_mut(|buffer| { + buffer.set_metrics(font_sizes[font_size_i].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(tiny_skia::Color::from_rgba8(0, 0, 0, 0xFF)); + 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(tiny_skia::Color::from_rgba8(0, 0, 0, 0xFF)); - editor.with_buffer_mut(|buffer| { - buffer.set_size( - Some(width as f32 - scrollbar_width * display_scale), - Some(height as f32), - ) - }); + editor.with_buffer_mut(|buffer| { + buffer.set_size( + Some(width as f32 - scrollbar_width * display_scale), + Some(height as f32), + ) + }); - let mut paint = Paint::default(); - paint.anti_alias = false; - editor.shape_as_needed(true); - editor.draw(&mut swash_cache, |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, - ); - }); - if let Some((x, y)) = editor.cursor_position() { - window.set_ime_cursor_area( - PhysicalPosition::new(x, y), - PhysicalSize::new(20, 20), - ); - } - - // Draw scrollbar - { - let mut start_line_opt = None; - let mut end_line = 0; - editor.with_buffer(|buffer| { - for run in buffer.layout_runs() { - end_line = run.line_i; - if start_line_opt.is_none() { - start_line_opt = Some(end_line); - } - } - }); + let mut paint = Paint { + anti_alias: false, + ..Default::default() + }; + editor.shape_as_needed(true); + editor.draw(&mut swash_cache, |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, + ); + }); + if let Some((x, y)) = editor.cursor_position() { + window.set_ime_cursor_area( + PhysicalPosition::new(x, y), + PhysicalSize::new(20, 20), + ); + } - let start_line = start_line_opt.unwrap_or(end_line); - let lines = editor.with_buffer(|buffer| buffer.lines.len()); - let start_y = (start_line * height as usize) / lines; - let end_y = (end_line * height as usize) / lines; - paint.set_color_rgba8(0xFF, 0xFF, 0xFF, 0x40); - if end_y > start_y { - pixmap.fill_rect( - Rect::from_xywh( - width as f32 - scrollbar_width * display_scale, - start_y as f32, - scrollbar_width * display_scale, - (end_y - start_y) as f32, - ) - .unwrap(), - &paint, - Transform::identity(), - None, - ); + // Draw scrollbar + { + let mut start_line_opt = None; + let mut end_line = 0; + editor.with_buffer(|buffer| { + for run in buffer.layout_runs() { + end_line = run.line_i; + if start_line_opt.is_none() { + start_line_opt = Some(end_line); } } + }); - surface_buffer.present().unwrap(); - } - WindowEvent::ModifiersChanged(modifiers) => { - ctrl_pressed = modifiers.state().control_key() + let start_line = start_line_opt.unwrap_or(end_line); + let lines = editor.with_buffer(|buffer| buffer.lines.len()); + let start_y = (start_line * height as usize) / lines; + let end_y = (end_line * height as usize) / lines; + paint.set_color_rgba8(0xFF, 0xFF, 0xFF, 0x40); + if end_y > start_y { + pixmap.fill_rect( + Rect::from_xywh( + width as f32 - scrollbar_width * display_scale, + start_y as f32, + scrollbar_width * display_scale, + (end_y - start_y) as f32, + ) + .unwrap(), + &paint, + Transform::identity(), + None, + ); } - 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) + 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::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 { + match &*text { + "0" => { + font_size_i = font_size_default; + editor.with_buffer_mut(|buffer| { + buffer.set_metrics( + font_sizes[font_size_i].scale(display_scale), + ) + }); } - } - Key::Character(text) => { - if ctrl_pressed { - match &*text { - "0" => { - font_size_i = font_size_default; - editor.with_buffer_mut(|buffer| { - buffer.set_metrics( - font_sizes[font_size_i] - .scale(display_scale), - ) - }); - } - "-" => { - if font_size_i > 0 { - font_size_i -= 1; - editor.with_buffer_mut(|buffer| { - buffer.set_metrics( - font_sizes[font_size_i] - .scale(display_scale), - ) - }); - } - } - "=" => { - if font_size_i + 1 < font_sizes.len() { - font_size_i += 1; - editor.with_buffer_mut(|buffer| { - buffer.set_metrics( - font_sizes[font_size_i] - .scale(display_scale), - ) - }); - } - } - "s" => { - let mut text = String::new(); - editor.with_buffer(|buffer| { - for line in buffer.lines.iter() { - text.push_str(line.text()); - text.push_str(line.ending().as_str()); - } - }); - fs::write(&path, &text).unwrap(); - log::info!("saved {:?}", path); - } - _ => {} + "-" => { + if font_size_i > 0 { + font_size_i -= 1; + editor.with_buffer_mut(|buffer| { + buffer.set_metrics( + font_sizes[font_size_i] + .scale(display_scale), + ) + }); } - } else { - for c in text.chars() { - editor.action(Action::Insert(c)); + } + "=" => { + if font_size_i + 1 < font_sizes.len() { + font_size_i += 1; + editor.with_buffer_mut(|buffer| { + buffer.set_metrics( + font_sizes[font_size_i] + .scale(display_scale), + ) + }); } } + "s" => { + let mut text = String::new(); + editor.with_buffer(|buffer| { + for line in buffer.lines.iter() { + text.push_str(line.text()); + text.push_str(line.ending().as_str()); + } + }); + fs::write(&path, &text).unwrap(); + log::info!("saved {:?}", path); + } + _ => {} + } + } else { + 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, - }); + 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; - // 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 }); - } + // 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, + }); - window.request_redraw(); - } + // 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 }); } - 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 as i32, - y: mouse_y as i32, - }); - window.request_redraw(); - } - 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::MouseInput { + device_id: _, + state, + button, + } => { + if button == MouseButton::Left { + if state == ElementState::Pressed && mouse_left == ElementState::Released { + editor.action(Action::Click { + x: mouse_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/shape.rs b/src/shape.rs index 5a38d4fd80..ab0cdba557 100644 --- a/src/shape.rs +++ b/src/shape.rs @@ -1249,7 +1249,8 @@ impl ShapeLine { let trailing_blank = span .words .get(i + 1) - .map_or(false, |previous_word| previous_word.blank); + .is_some_and(|previous_word| previous_word.blank); + if trailing_blank { number_of_blanks = number_of_blanks.saturating_sub(1); add_to_visual_line(