fix clippy issues

This commit is contained in:
2024-12-11 09:11:30 +07:00
parent 516eb0e8bc
commit 10f042acab
49 changed files with 661 additions and 319 deletions

View File

@@ -9,6 +9,7 @@ use crate::{
pub(crate) struct ClearButton {}
impl ClearButton {
#[allow(clippy::new_ret_no_self)]
pub fn new(cx: &mut WindowContext) -> Button {
Button::new("clean")
.icon(Icon::new(IconName::CircleX).text_color(cx.theme().muted_foreground))

View File

@@ -134,7 +134,7 @@ impl TextElement {
}
}
bounds.origin = bounds.origin + scroll_offset;
bounds.origin += scroll_offset;
if input.show_cursor(cx) {
// cursor blink
@@ -268,7 +268,7 @@ impl TextElement {
// print_points_as_svg_path(&line_corners, &points);
let first_p = *points.get(0).unwrap();
let first_p = *points.first().unwrap();
let mut path = gpui::Path::new(bounds.origin + first_p);
for p in points.iter().skip(1) {
path.line_to(bounds.origin + *p);
@@ -295,10 +295,7 @@ impl IntoElement for TextElement {
/// A debug function to print points as SVG path.
#[allow(unused)]
fn print_points_as_svg_path(
line_corners: &Vec<Corners<Point<Pixels>>>,
points: &Vec<Point<Pixels>>,
) {
fn print_points_as_svg_path(line_corners: &Vec<Corners<Point<Pixels>>>, points: &[Point<Pixels>]) {
for corners in line_corners {
println!(
"tl: ({}, {}), tr: ({}, {}), bl: ({}, {}), br: ({}, {})",
@@ -313,7 +310,7 @@ fn print_points_as_svg_path(
);
}
if points.len() > 0 {
if !points.is_empty() {
println!("M{},{}", points[0].x.0 as i32, points[0].y.0 as i32);
for p in points.iter().skip(1) {
println!("L{},{}", p.x.0 as i32, p.y.0 as i32);

View File

@@ -145,14 +145,17 @@ pub fn init(cx: &mut AppContext) {
]);
}
type Affixes<T> = Option<Box<dyn Fn(&mut ViewContext<T>) -> AnyElement + 'static>>;
type Validate = Option<Box<dyn Fn(&str) -> bool + 'static>>;
pub struct TextInput {
pub(super) focus_handle: FocusHandle,
pub(super) text: SharedString,
multi_line: bool,
pub(super) history: History<Change>,
pub(super) blink_cursor: Model<BlinkCursor>,
pub(super) prefix: Option<Box<dyn Fn(&mut ViewContext<Self>) -> AnyElement + 'static>>,
pub(super) suffix: Option<Box<dyn Fn(&mut ViewContext<Self>) -> AnyElement + 'static>>,
pub(super) prefix: Affixes<Self>,
pub(super) suffix: Affixes<Self>,
pub(super) loading: bool,
pub(super) placeholder: SharedString,
pub(super) selected_range: Range<usize>,
@@ -177,7 +180,7 @@ pub struct TextInput {
pub(super) size: Size,
pub(super) rows: usize,
pattern: Option<regex::Regex>,
validate: Option<Box<dyn Fn(&str) -> bool + 'static>>,
validate: Validate,
pub(crate) scroll_handle: ScrollHandle,
scrollbar_state: Rc<Cell<ScrollbarState>>,
/// The size of the scrollable content.
@@ -506,13 +509,12 @@ impl TextInput {
}
let offset = self.previous_boundary(self.cursor_offset());
let line = self
.text_for_range(self.range_to_utf16(&(0..offset + 1)), &mut None, cx)
self.text_for_range(self.range_to_utf16(&(0..offset + 1)), &mut None, cx)
.unwrap_or_default()
.rfind('\n')
.map(|i| i + 1)
.unwrap_or(0);
line
.unwrap_or(0)
}
/// Get end of line
@@ -531,17 +533,15 @@ impl TextInput {
return offset;
}
let line = self
.text_for_range(
self.range_to_utf16(&(offset..self.text.len())),
&mut None,
cx,
)
.unwrap_or_default()
.find('\n')
.map(|i| i + offset)
.unwrap_or(self.text.len());
line
self.text_for_range(
self.range_to_utf16(&(offset..self.text.len())),
&mut None,
cx,
)
.unwrap_or_default()
.find('\n')
.map(|i| i + offset)
.unwrap_or(self.text.len())
}
fn backspace(&mut self, _: &Backspace, cx: &mut ViewContext<Self>) {
@@ -675,7 +675,7 @@ impl TextInput {
}
let old_text = self
.text_for_range(self.range_to_utf16(&range), &mut None, cx)
.text_for_range(self.range_to_utf16(range), &mut None, cx)
.unwrap_or("".to_string());
let new_range = range.start..range.start + new_text.len();
@@ -753,7 +753,7 @@ impl TextInput {
let mut y_offset = px(0.);
for line in lines.iter() {
let line_origin = self.line_origin_with_y_offset(&mut y_offset, &line, line_height);
let line_origin = self.line_origin_with_y_offset(&mut y_offset, line, line_height);
let mut pos = inner_position - line_origin;
// Ignore the y position in single line mode, only check x position.
if self.is_single_line() {
@@ -765,7 +765,10 @@ impl TextInput {
// Add 1 for place cursor after the character.
index += v + 1;
break;
} else if let Ok(_) = line.index_for_position(point(px(0.), pos.y), line_height) {
} else if line
.index_for_position(point(px(0.), pos.y), line_height)
.is_ok()
{
// Click in the this line but not in the text, move cursor to the end of the line.
// The fallback index is saved in Err from `index_for_position` method.
index += index_result.unwrap_err();
@@ -809,7 +812,7 @@ impl TextInput {
if self.is_multi_line() {
let p = point(px(0.), *y_offset);
let height = line_height + line.wrap_boundaries.len() as f32 * line_height;
*y_offset = *y_offset + height;
*y_offset += height;
p
} else {
point(px(0.), px(0.))
@@ -859,7 +862,7 @@ impl TextInput {
let prev_chars = prev_text.chars().rev().peekable();
let next_chars = next_text.chars().peekable();
for (_, c) in prev_chars.enumerate() {
for c in prev_chars {
if !is_word(c) {
break;
}
@@ -867,7 +870,7 @@ impl TextInput {
start -= c.len_utf16();
}
for (_, c) in next_chars.enumerate() {
for c in next_chars {
if !is_word(c) {
break;
}
@@ -1177,12 +1180,8 @@ impl Render for TextInput {
.on_action(cx.listener(Self::delete_to_end_of_line))
.on_action(cx.listener(Self::enter))
})
.on_action(cx.listener(Self::up))
.on_action(cx.listener(Self::down))
.on_action(cx.listener(Self::left))
.on_action(cx.listener(Self::right))
.on_action(cx.listener(Self::select_up))
.on_action(cx.listener(Self::select_down))
.on_action(cx.listener(Self::select_left))
.on_action(cx.listener(Self::select_right))
.on_action(cx.listener(Self::select_all))
@@ -1206,7 +1205,13 @@ impl Render for TextInput {
.input_py(self.size)
.input_h(self.size)
.cursor_text()
.when(self.multi_line, |this| this.h_auto())
.when(self.multi_line, |this| {
this.on_action(cx.listener(Self::up))
.on_action(cx.listener(Self::down))
.on_action(cx.listener(Self::select_up))
.on_action(cx.listener(Self::select_down))
.h_auto()
})
.when(self.appearance, |this| {
this.bg(if self.disabled {
cx.theme().muted
@@ -1249,7 +1254,7 @@ impl Render for TextInput {
.absolute()
.top_0()
.left_0()
.right_0()
.right(px(1.))
.bottom_0()
.child(
Scrollbar::vertical(

View File

@@ -2,6 +2,7 @@ mod blink_cursor;
mod change;
mod clear_button;
mod element;
#[allow(clippy::module_inception)]
mod input;
mod otp_input;

View File

@@ -127,7 +127,7 @@ impl OtpInput {
}
_ => {
let c = key.chars().next().unwrap();
if !matches!(c, '0'..='9') {
if !c.is_ascii_digit() {
return;
}
if ix >= self.length {