refactor: address clippy warnings and improve code quality (#409)

- Fix string formatting with modern interpolation syntax
- Improve Debug implementation with finish_non_exhaustive()
- Fix function placement in shape.rs to avoid items_after_statements warning
- Use more idiomatic Rust patterns (map_or_else, next_back)
- Clean up conditional imports in vi.rs
- Convert multiple methods to `const` functions for optimization and consistency
- Introduce `core_maths` for enhanced no-std compatibility
- Update `Cargo.toml` for the new optional dependency and feature adjustments
This commit is contained in:
romanstingler 2025-08-11 21:58:59 +02:00 committed by GitHub
parent e80dbc3607
commit a2f1f4b2a0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 282 additions and 299 deletions

View file

@ -2,11 +2,16 @@
#[cfg(not(feature = "std"))]
use alloc::{string::String, vec::Vec};
use core::{cmp, fmt};
#[cfg(not(feature = "std"))]
use core_maths::CoreFloat;
use unicode_segmentation::UnicodeSegmentation;
#[cfg(feature = "swash")]
use crate::Color;
use crate::{
Affinity, Align, Attrs, AttrsList, BidiParagraphs, BorrowedWithFontSystem, BufferLine, Cursor,
FontSystem, LayoutCursor, LayoutGlyph, LayoutLine, LineEnding, LineIter, Motion, Scroll,
@ -45,36 +50,34 @@ impl LayoutRun<'_> {
let mut x_end = None;
let rtl_factor = if self.rtl { 1. } else { 0. };
let ltr_factor = 1. - rtl_factor;
for glyph in self.glyphs.iter() {
for glyph in self.glyphs {
let cursor = self.cursor_from_glyph_left(glyph);
if cursor >= cursor_start && cursor <= cursor_end {
if x_start.is_none() {
x_start = Some(glyph.x + glyph.w * rtl_factor);
x_start = Some(glyph.x + glyph.w.mul_add(rtl_factor, 0.0));
}
x_end = Some(glyph.x + glyph.w * rtl_factor);
x_end = Some(glyph.x + glyph.w.mul_add(rtl_factor, 0.0));
}
let cursor = self.cursor_from_glyph_right(glyph);
if cursor >= cursor_start && cursor <= cursor_end {
if x_start.is_none() {
x_start = Some(glyph.x + glyph.w * ltr_factor);
x_start = Some(glyph.x + glyph.w.mul_add(ltr_factor, 0.0));
}
x_end = Some(glyph.x + glyph.w * ltr_factor);
x_end = Some(glyph.x + glyph.w.mul_add(ltr_factor, 0.0));
}
}
if let Some(x_start) = x_start {
x_start.map(|x_start| {
let x_end = x_end.expect("end of cursor not found");
let (x_start, x_end) = if x_start < x_end {
(x_start, x_end)
} else {
(x_end, x_start)
};
Some((x_start, x_end - x_start))
} else {
None
}
(x_start, x_end - x_start)
})
}
fn cursor_from_glyph_left(&self, glyph: &LayoutGlyph) -> Cursor {
const fn cursor_from_glyph_left(&self, glyph: &LayoutGlyph) -> Cursor {
if self.rtl {
Cursor::new_with_affinity(self.line_i, glyph.end, Affinity::Before)
} else {
@ -82,7 +85,7 @@ impl LayoutRun<'_> {
}
}
fn cursor_from_glyph_right(&self, glyph: &LayoutGlyph) -> Cursor {
const fn cursor_from_glyph_right(&self, glyph: &LayoutGlyph) -> Cursor {
if self.rtl {
Cursor::new_with_affinity(self.line_i, glyph.start, Affinity::After)
} else {
@ -102,7 +105,7 @@ pub struct LayoutRunIter<'b> {
}
impl<'b> LayoutRunIter<'b> {
pub fn new(buffer: &'b Buffer) -> Self {
pub const fn new(buffer: &'b Buffer) -> Self {
Self {
buffer,
line_i: buffer.scroll.line,
@ -276,7 +279,7 @@ impl Buffer {
pub fn borrow_with<'a>(
&'a mut self,
font_system: &'a mut FontSystem,
) -> BorrowedWithFontSystem<'a, Buffer> {
) -> BorrowedWithFontSystem<'a, Self> {
BorrowedWithFontSystem {
inner: self,
font_system,
@ -358,7 +361,7 @@ impl Buffer {
let layout = self
.line_layout(font_system, line_i)
.expect("shape_until_cursor failed to scroll forwards");
for layout_line in layout.iter() {
for layout_line in layout {
total_height += layout_line.line_height_opt.unwrap_or(metrics.line_height);
}
if total_height > height + self.scroll.vertical {
@ -379,18 +382,16 @@ impl Buffer {
if let Some(layout_cursor) = self.layout_cursor(font_system, cursor) {
if let Some(layout_lines) = self.line_layout(font_system, layout_cursor.line) {
if let Some(layout_line) = layout_lines.get(layout_cursor.layout) {
let (x_min, x_max) = if let Some(glyph) = layout_line
let (x_min, x_max) = layout_line
.glyphs
.get(layout_cursor.glyph)
.or_else(|| layout_line.glyphs.last())
{
//TODO: use code from cursor_glyph_opt?
let x_a = glyph.x;
let x_b = glyph.x + glyph.w;
(x_a.min(x_b), x_a.max(x_b))
} else {
(0.0, 0.0)
};
.map_or((0.0, 0.0), |glyph| {
//TODO: use code from cursor_glyph_opt?
let x_a = glyph.x;
let x_b = glyph.x + glyph.w;
(x_a.min(x_b), x_a.max(x_b))
});
if x_min < self.scroll.horizontal {
self.scroll.horizontal = x_min;
self.redraw = true;
@ -419,7 +420,7 @@ impl Buffer {
let line_i = self.scroll.line - 1;
if let Some(layout) = self.line_layout(font_system, line_i) {
let mut layout_height = 0.0;
for layout_line in layout.iter() {
for layout_line in layout {
layout_height +=
layout_line.line_height_opt.unwrap_or(metrics.line_height);
}
@ -451,16 +452,15 @@ impl Buffer {
if prune {
self.lines[line_i].reset_shaping();
continue;
} else {
break;
}
break;
}
let mut layout_height = 0.0;
let layout = self
.line_layout(font_system, line_i)
.expect("shape_until_scroll invalid line");
for layout_line in layout.iter() {
for layout_line in layout {
let line_height = layout_line.line_height_opt.unwrap_or(metrics.line_height);
layout_height += line_height;
total_height += line_height;
@ -547,7 +547,7 @@ impl Buffer {
}
/// Get the current [`Metrics`]
pub fn metrics(&self) -> Metrics {
pub const fn metrics(&self) -> Metrics {
self.metrics
}
@ -561,7 +561,7 @@ impl Buffer {
}
/// Get the current [`Wrap`]
pub fn wrap(&self) -> Wrap {
pub const fn wrap(&self) -> Wrap {
self.wrap
}
@ -575,7 +575,7 @@ impl Buffer {
}
/// Get the current `monospace_width`
pub fn monospace_width(&self) -> Option<f32> {
pub const fn monospace_width(&self) -> Option<f32> {
self.monospace_width
}
@ -593,7 +593,7 @@ impl Buffer {
}
/// Get the current `tab_width`
pub fn tab_width(&self) -> u16 {
pub const fn tab_width(&self) -> u16 {
self.tab_width
}
@ -606,7 +606,7 @@ impl Buffer {
if tab_width != self.tab_width {
self.tab_width = tab_width;
// Shaping must be reset when tab width is changed
for line in self.lines.iter_mut() {
for line in &mut self.lines {
if line.shape_opt().is_some() && line.text().contains('\t') {
line.reset_shaping();
}
@ -617,7 +617,7 @@ impl Buffer {
}
/// Get the current buffer dimensions (width, height)
pub fn size(&self) -> (Option<f32>, Option<f32>) {
pub const fn size(&self) -> (Option<f32>, Option<f32>) {
(self.width_opt, self.height_opt)
}
@ -660,7 +660,7 @@ impl Buffer {
}
/// Get the current scroll location
pub fn scroll(&self) -> Scroll {
pub const fn scroll(&self) -> Scroll {
self.scroll
}
@ -758,8 +758,7 @@ impl Buffer {
let mut attrs_list = self
.lines
.get_mut(line_count)
.map(BufferLine::reclaim_attrs)
.unwrap_or_else(|| AttrsList::new(&Attrs::new()))
.map_or_else(|| AttrsList::new(&Attrs::new()), BufferLine::reclaim_attrs)
.reset(default_attrs);
let mut line_string = self
.lines
@ -811,8 +810,7 @@ impl Buffer {
let next_attrs_list = self
.lines
.get_mut(line_count + 1)
.map(BufferLine::reclaim_attrs)
.unwrap_or_else(|| AttrsList::new(&Attrs::new()))
.map_or_else(|| AttrsList::new(&Attrs::new()), BufferLine::reclaim_attrs)
.reset(default_attrs);
let next_line_string = self
.lines
@ -856,7 +854,7 @@ impl Buffer {
}
/// True if a redraw is needed
pub fn redraw(&self) -> bool {
pub const fn redraw(&self) -> bool {
self.redraw
}
@ -866,7 +864,7 @@ impl Buffer {
}
/// Get the visible layout runs for rendering and other tasks
pub fn layout_runs(&self) -> LayoutRunIter {
pub const fn layout_runs(&self) -> LayoutRunIter {
LayoutRunIter::new(self)
}
@ -991,14 +989,16 @@ impl Buffer {
},
};
let (new_index, new_affinity) = match layout_line.glyphs.get(layout_cursor.glyph) {
Some(glyph) => (glyph.start, Affinity::After),
None => match layout_line.glyphs.last() {
Some(glyph) => (glyph.end, Affinity::Before),
//TODO: is this correct?
None => (0, Affinity::After),
},
};
let (new_index, new_affinity) =
layout_line.glyphs.get(layout_cursor.glyph).map_or_else(
|| {
layout_line
.glyphs
.last()
.map_or((0, Affinity::After), |glyph| (glyph.end, Affinity::Before))
},
|glyph| (glyph.start, Affinity::After),
);
if cursor.line != layout_cursor.line
|| cursor.index != new_index
@ -1159,8 +1159,7 @@ impl Buffer {
cursor.index = line
.text()
.char_indices()
.filter_map(|(i, c)| if c.is_whitespace() { None } else { Some(i) })
.next()
.find_map(|(i, c)| if c.is_whitespace() { None } else { Some(i) })
.unwrap_or(0);
cursor_x_opt = None;
}
@ -1253,7 +1252,7 @@ impl Buffer {
.unicode_word_indices()
.map(|(i, word)| i + word.len())
.find(|&i| i > cursor.index)
.unwrap_or(line.text().len());
.unwrap_or_else(|| line.text().len());
} else if cursor.line + 1 < self.lines.len() {
cursor.line += 1;
cursor.index = 0;
@ -1340,13 +1339,9 @@ impl Buffer {
F: FnMut(i32, i32, u32, u32, Color),
{
for run in self.layout_runs() {
for glyph in run.glyphs.iter() {
for glyph in run.glyphs {
let physical_glyph = glyph.physical((0., 0.), 1.0);
let glyph_color = match glyph.color_opt {
Some(some) => some,
None => color,
};
let glyph_color = glyph.color_opt.map_or(color, |some| some);
cache.with_pixels(
font_system,