From 13987165088d991d3c024b5bbb8cd7eb89b4c2ad Mon Sep 17 00:00:00 2001 From: Hojjat Date: Tue, 22 Sep 2026 17:33:07 -0600 Subject: [PATCH] chore: clean up formatting and lint --- src/config.rs | 11 +-- src/git.rs | 2 +- src/main.rs | 247 +++++++++++++++++++++++------------------------- src/menu.rs | 10 +- src/project.rs | 16 ++-- src/search.rs | 8 +- src/tab.rs | 7 +- src/text_box.rs | 47 ++++----- 8 files changed, 164 insertions(+), 184 deletions(-) diff --git a/src/config.rs b/src/config.rs index c5cea7f..3eaaac0 100644 --- a/src/config.rs +++ b/src/config.rs @@ -112,17 +112,8 @@ impl Config { } } -#[derive(Clone, CosmicConfigEntry, Debug, Deserialize, Eq, PartialEq, Serialize)] +#[derive(Clone, CosmicConfigEntry, Debug, Deserialize, Eq, PartialEq, Serialize, Default)] pub struct ConfigState { pub recent_files: VecDeque, pub recent_projects: VecDeque, } - -impl Default for ConfigState { - fn default() -> Self { - Self { - recent_files: VecDeque::new(), - recent_projects: VecDeque::new(), - } - } -} diff --git a/src/git.rs b/src/git.rs index 02bf15a..c4855d6 100644 --- a/src/git.rs +++ b/src/git.rs @@ -124,7 +124,7 @@ impl GitRepository { msg.push_str("\nstderr> "); msg.push_str(line); } - Err(io::Error::new(io::ErrorKind::Other, msg)) + Err(io::Error::other(msg)) } } diff --git a/src/main.rs b/src/main.rs index 3a08af8..4b9cbba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -328,6 +328,7 @@ impl PartialEq for WatcherWrapper { } } +#[allow(clippy::large_enum_variant)] enum NewTab { Tab(EditorTab), Exists(Entity), @@ -529,7 +530,7 @@ impl App { continue; } let entry_path = entry.path(); - let node = match ProjectNode::new(&entry_path) { + let node = match ProjectNode::new(entry_path) { Ok(ok) => ok, Err(err) => { log::error!( @@ -682,11 +683,11 @@ impl App { //TODO: allow files to be open multiple times let mut activate_opt = None; for entity in self.tab_model.iter() { - if let Some(Tab::Editor(tab)) = self.tab_model.data::(entity) { - if tab.path_opt.as_ref() == Some(&canonical) { - activate_opt = Some(entity); - break; - } + if let Some(Tab::Editor(tab)) = self.tab_model.data::(entity) + && tab.path_opt.as_ref() == Some(&canonical) + { + activate_opt = Some(entity); + break; } } if let Some(entity) = activate_opt { @@ -734,10 +735,10 @@ impl App { } let entities: Vec<_> = self.tab_model.iter().collect(); for entity in entities { - if self.tab_model.is_active(entity) { - if let Some(Tab::Editor(tab)) = self.tab_model.data_mut::(entity) { - tab.set_config(&self.config); - } + if self.tab_model.is_active(entity) + && let Some(Tab::Editor(tab)) = self.tab_model.data_mut::(entity) + { + tab.set_config(&self.config); } } } @@ -754,10 +755,10 @@ impl App { } fn save_config_state(&mut self) { - if let Some(ref config_state_handler) = self.config_state_handler { - if let Err(err) = self.config_state.write_entry(config_state_handler) { - log::error!("failed to save config_state: {}", err); - } + if let Some(ref config_state_handler) = self.config_state_handler + && let Err(err) = self.config_state.write_entry(config_state_handler) + { + log::error!("failed to save config_state: {}", err); } } @@ -777,10 +778,10 @@ impl App { Some(DialogPage::PromptSaveQuit(ref _entities)) => { let mut unsaved = Vec::new(); for entity in self.tab_model.iter() { - if let Some(Tab::Editor(tab)) = self.tab_model.data::(entity) { - if tab.changed() { - unsaved.push(entity); - } + if let Some(Tab::Editor(tab)) = self.tab_model.data::(entity) + && tab.changed() + { + unsaved.push(entity); } } if unsaved.is_empty() { @@ -919,16 +920,16 @@ impl App { } 'tabs: for entity in self.tab_model.iter() { - if let Some(Tab::Editor(tab)) = self.tab_model.data::(entity) { - if let Some(path) = &tab.path_opt { - for (_, project_path) in self.projects.iter() { - if path.starts_with(&project_path) { - // Do not watch tabs inside of already watched projects - continue 'tabs; - } + if let Some(Tab::Editor(tab)) = self.tab_model.data::(entity) + && let Some(path) = &tab.path_opt + { + for (_, project_path) in self.projects.iter() { + if path.starts_with(project_path) { + // Do not watch tabs inside of already watched projects + continue 'tabs; } - new_paths.insert((path.to_path_buf(), RecursiveMode::NonRecursive)); } + new_paths.insert((path.to_path_buf(), RecursiveMode::NonRecursive)); } } @@ -1443,7 +1444,7 @@ impl Application for App { let mut theme_names = Vec::with_capacity(SYNTAX_SYSTEM.get().unwrap().theme_set.themes.len()); - for (theme_name, _theme) in SYNTAX_SYSTEM.get().unwrap().theme_set.themes.iter() { + for theme_name in SYNTAX_SYSTEM.get().unwrap().theme_set.themes.keys() { theme_names.push(theme_name.to_string()); } @@ -1652,9 +1653,7 @@ impl Application for App { } fn dialog(&self) -> Option> { - let Some(ref dialog) = self.dialog_page_opt else { - return None; - }; + let dialog = self.dialog_page_opt.as_ref()?; let cosmic_theme::Spacing { space_xxs, .. } = self.core().system_theme().cosmic().spacing; @@ -1937,21 +1936,21 @@ impl Application for App { return self.update_config(); } Message::FindNext => { - if !self.find_search_value.is_empty() { - if let Some(Tab::Editor(tab)) = self.active_tab() { - //TODO: do not compile find regex on every search? - match self.config.find_regex(&self.find_search_value) { - Ok(regex) => { - tab.search(®ex, true, self.config.find_wrap_around); - } - Err(err) => { - //TODO: put regex error in find box - log::warn!( - "failed to compile regex {:?}: {}", - self.find_search_value, - err - ); - } + if !self.find_search_value.is_empty() + && let Some(Tab::Editor(tab)) = self.active_tab() + { + //TODO: do not compile find regex on every search? + match self.config.find_regex(&self.find_search_value) { + Ok(regex) => { + tab.search(®ex, true, self.config.find_wrap_around); + } + Err(err) => { + //TODO: put regex error in find box + log::warn!( + "failed to compile regex {:?}: {}", + self.find_search_value, + err + ); } } } @@ -1960,21 +1959,21 @@ impl Application for App { return self.update_focus(); } Message::FindPrevious => { - if !self.find_search_value.is_empty() { - if let Some(Tab::Editor(tab)) = self.active_tab() { - //TODO: do not compile find regex on every search? - match self.config.find_regex(&self.find_search_value) { - Ok(regex) => { - tab.search(®ex, false, self.config.find_wrap_around); - } - Err(err) => { - //TODO: put regex error in find box - log::warn!( - "failed to compile regex {:?}: {}", - self.find_search_value, - err - ); - } + if !self.find_search_value.is_empty() + && let Some(Tab::Editor(tab)) = self.active_tab() + { + //TODO: do not compile find regex on every search? + match self.config.find_regex(&self.find_search_value) { + Ok(regex) => { + tab.search(®ex, false, self.config.find_wrap_around); + } + Err(err) => { + //TODO: put regex error in find box + log::warn!( + "failed to compile regex {:?}: {}", + self.find_search_value, + err + ); } } } @@ -1983,27 +1982,27 @@ impl Application for App { return self.update_focus(); } Message::FindReplace => { - if !self.find_search_value.is_empty() { - if let Some(Tab::Editor(tab)) = self.active_tab() { - //TODO: do not compile find regex on every search? - match self.config.find_regex(&self.find_search_value) { - Ok(regex) => { - //TODO: support captures - tab.replace( - ®ex, - &self.find_replace_value, - self.config.find_wrap_around, - ); - return self.update(Message::TabChanged(self.tab_model.active())); - } - Err(err) => { - //TODO: put regex error in find box - log::warn!( - "failed to compile regex {:?}: {}", - self.find_search_value, - err - ); - } + if !self.find_search_value.is_empty() + && let Some(Tab::Editor(tab)) = self.active_tab() + { + //TODO: do not compile find regex on every search? + match self.config.find_regex(&self.find_search_value) { + Ok(regex) => { + //TODO: support captures + tab.replace( + ®ex, + &self.find_replace_value, + self.config.find_wrap_around, + ); + return self.update(Message::TabChanged(self.tab_model.active())); + } + Err(err) => { + //TODO: put regex error in find box + log::warn!( + "failed to compile regex {:?}: {}", + self.find_search_value, + err + ); } } } @@ -2012,27 +2011,27 @@ impl Application for App { return self.update_focus(); } Message::FindReplaceAll => { - if !self.find_search_value.is_empty() { - if let Some(Tab::Editor(tab)) = self.active_tab() { - //TODO: do not compile find regex on every search? - match self.config.find_regex(&self.find_search_value) { - Ok(regex) => { - //TODO: support captures - { - let mut editor = tab.editor.lock().unwrap(); - editor.set_cursor(cosmic_text::Cursor::new(0, 0)); - } - while tab.replace(®ex, &self.find_replace_value, false) {} - return self.update(Message::TabChanged(self.tab_model.active())); - } - Err(err) => { - //TODO: put regex error in find box - log::warn!( - "failed to compile regex {:?}: {}", - self.find_search_value, - err - ); + if !self.find_search_value.is_empty() + && let Some(Tab::Editor(tab)) = self.active_tab() + { + //TODO: do not compile find regex on every search? + match self.config.find_regex(&self.find_search_value) { + Ok(regex) => { + //TODO: support captures + { + let mut editor = tab.editor.lock().unwrap(); + editor.set_cursor(cosmic_text::Cursor::new(0, 0)); } + while tab.replace(®ex, &self.find_replace_value, false) {} + return self.update(Message::TabChanged(self.tab_model.active())); + } + Err(err) => { + //TODO: put regex error in find box + log::warn!( + "failed to compile regex {:?}: {}", + self.find_search_value, + err + ); } } } @@ -2172,18 +2171,14 @@ impl Application for App { // Reload tabs that changed let mut tab_reload = Vec::new(); for entity in self.tab_model.iter() { - if let Some(Tab::Editor(tab)) = self.tab_model.data::(entity) { - if let Some(path) = &tab.path_opt { - if event.paths.contains(path) { - if tab.changed() { - log::warn!( - "file changed externally before being saved: {:?}", - path - ); - } else { - tab_reload.push(entity); - } - } + if let Some(Tab::Editor(tab)) = self.tab_model.data::(entity) + && let Some(path) = &tab.path_opt + && event.paths.contains(path) + { + if tab.changed() { + log::warn!("file changed externally before being saved: {:?}", path); + } else { + tab_reload.push(entity); } } } @@ -2281,7 +2276,7 @@ impl Application for App { { for (_, project_path) in self.projects.iter() { for path in event.paths.iter() { - if let Ok(prefix) = path.strip_prefix(&project_path) { + if let Ok(prefix) = path.strip_prefix(project_path) { // Manually ignore project .git folders //TODO: use logic from ignore crate somehow? if prefix.starts_with(".git") { @@ -2351,10 +2346,10 @@ impl Application for App { { let mut close = Vec::new(); for entity in self.tab_model.iter() { - if let Some(Tab::GitDiff(other_tab)) = self.tab_model.data::(entity) { - if other_tab.diff.path == diff.path { - close.push(entity); - } + if let Some(Tab::GitDiff(other_tab)) = self.tab_model.data::(entity) + && other_tab.diff.path == diff.path + { + close.push(entity); } } for entity in close { @@ -2672,10 +2667,10 @@ impl Application for App { tab.path_opt = Some(paths.remove(0)); title_opt = Some(tab.title()); tab.save(); - if let Some(path) = tab.path_opt.clone() { - if let Ok(canonical) = fs::canonicalize(&path) { - self.add_to_recents(&canonical); - } + if let Some(path) = tab.path_opt.clone() + && let Ok(canonical) = fs::canonicalize(&path) + { + self.add_to_recents(&canonical); } } if let Some(title) = title_opt { @@ -2851,13 +2846,7 @@ impl Application for App { .tab_model .position(self.tab_model.active()) .and_then(|i| (i as usize).checked_sub(1)) - .unwrap_or_else(|| { - self.tab_model - .iter() - .count() - .checked_sub(1) - .unwrap_or_default() - }); + .unwrap_or_else(|| self.tab_model.iter().count().saturating_sub(1)); let entity = self.tab_model.iter().nth(pos); if let Some(entity) = entity { diff --git a/src/menu.rs b/src/menu.rs index 18e5bdb..79388bc 100644 --- a/src/menu.rs +++ b/src/menu.rs @@ -12,7 +12,11 @@ use cosmic::{ responsive_menu_bar, segmented_button, }, }; -use std::{collections::HashMap, path::PathBuf, sync::LazyLock}; +use std::{ + collections::HashMap, + path::{Path, PathBuf}, + sync::LazyLock, +}; use crate::{Action, Config, ConfigState, Message, fl}; @@ -60,7 +64,7 @@ fn truncate_middle(value: &str, max_chars: usize) -> String { ) } -fn format_recent_menu_path(path: &PathBuf, home_dir_opt: Option<&PathBuf>) -> String { +fn format_recent_menu_path(path: &Path, home_dir_opt: Option<&PathBuf>) -> String { const ELLIPSIS: &str = "..."; let display = if let Some(home_dir) = home_dir_opt { @@ -163,7 +167,7 @@ pub fn menu_bar<'a>( config: &Config, config_state: &ConfigState, key_binds: &HashMap, - projects: &Vec<(String, PathBuf)>, + projects: &[(String, PathBuf)], has_selection: bool, ) -> Element<'a, Message> { //TODO: port to libcosmic diff --git a/src/project.rs b/src/project.rs index 11c8978..0a13262 100644 --- a/src/project.rs +++ b/src/project.rs @@ -29,15 +29,15 @@ impl ProjectNode { let path = fs::canonicalize(path)?; let name = path .file_name() - .ok_or(io::Error::new( - io::ErrorKind::Other, - format!("path {:?} has no file name", path), - ))? + .ok_or(io::Error::other(format!( + "path {:?} has no file name", + path + )))? .to_str() - .ok_or(io::Error::new( - io::ErrorKind::Other, - format!("path {:?} is not valid UTF-8", path), - ))? + .ok_or(io::Error::other(format!( + "path {:?} is not valid UTF-8", + path + )))? .to_string(); Ok(if path.is_dir() { Self::Folder { diff --git a/src/search.rs b/src/search.rs index 7eaed1a..cbefcd2 100644 --- a/src/search.rs +++ b/src/search.rs @@ -54,10 +54,10 @@ impl ProjectSearchResult { } }; - if let Some(file_type) = entry.file_type() { - if file_type.is_dir() { - continue; - } + if let Some(file_type) = entry.file_type() + && file_type.is_dir() + { + continue; } let entry_path = entry.path(); diff --git a/src/tab.rs b/src/tab.rs index d9a8e7a..120fff6 100644 --- a/src/tab.rs +++ b/src/tab.rs @@ -25,6 +25,7 @@ fn editor_text(editor: &ViEditor<'static, 'static>) -> String { }) } +#[allow(clippy::large_enum_variant)] pub enum Tab { Editor(EditorTab), GitDiff(GitDiffTab), @@ -313,7 +314,7 @@ impl EditorTab { .filter_map(|m| { if cursor.line != start_line || m.start() >= cursor.index - || m.start() < cursor.index && wrapped == true + || m.start() < cursor.index && wrapped { Some((m.start(), m.len())) } else { @@ -390,7 +391,7 @@ impl EditorTab { if cursor.line != start_line || m.start() > cursor.index || m.start() == cursor.index && current_selection == Selection::None - || m.start() < cursor.index && wrapped == true + || m.start() < cursor.index && wrapped { Some((m.start(), m.end())) } else { @@ -433,7 +434,7 @@ impl EditorTab { if cursor.line != start_line || m.start() < cursor.index || m.start() == cursor.index && current_selection == Selection::None - || m.start() > cursor.index && wrapped == true + || m.start() > cursor.index && wrapped { Some((m.start(), m.end())) } else { diff --git a/src/text_box.rs b/src/text_box.rs index 3b5dacd..de7050a 100644 --- a/src/text_box.rs +++ b/src/text_box.rs @@ -317,9 +317,8 @@ where editor.with_buffer(|buffer| { let mut layout_lines = 0; for line in buffer.lines.iter() { - match line.layout_opt() { - Some(layout) => layout_lines += layout.len(), - None => (), + if let Some(layout) = line.layout_opt() { + layout_lines += layout.len() } } @@ -755,7 +754,7 @@ where match editor.buffer_ref() { cosmic_text::BufferRef::Arc(buffer) => { renderer.fill_raw(Raw { - buffer: Arc::downgrade(&buffer), + buffer: Arc::downgrade(buffer), position: pos, color: Color::from_rgba(1.0, 1.0, 1.0, 1.0), clip_bounds, @@ -1095,15 +1094,13 @@ where editor.set_redraw(true); shell.capture_event(); } - Named::Tab => { - if !modifiers.control() && !modifiers.alt() { - if modifiers.shift() { - editor.action(Action::Unindent); - } else { - editor.action(Action::Indent); - } - shell.capture_event(); + Named::Tab if !modifiers.control() && !modifiers.alt() => { + if modifiers.shift() { + editor.action(Action::Unindent); + } else { + editor.action(Action::Indent); } + shell.capture_event(); } _ => (), }, @@ -1152,7 +1149,7 @@ where InputMethodEvent::Commit(text) => { if state.is_focused { editor.start_change(); - editor.insert_string(&text, None); + editor.insert_string(text, None); editor.finish_change(); shell.capture_event(); } @@ -1174,13 +1171,13 @@ where let y = y_logical * scale_factor; // Do this first as the horizontal scrollbar is on top of the buffer - if let Some(scrollbar_h_rect) = state.scrollbar_h_rect.get() { - if scrollbar_h_rect.contains(Point::new(x_logical, y_logical)) { - state.dragging = Some(Dragging::ScrollbarH { - start_x: x, - start_scroll: editor.with_buffer(|buffer| buffer.scroll()), - }); - } + if let Some(scrollbar_h_rect) = state.scrollbar_h_rect.get() + && scrollbar_h_rect.contains(Point::new(x_logical, y_logical)) + { + state.dragging = Some(Dragging::ScrollbarH { + start_x: x, + start_scroll: editor.with_buffer(|buffer| buffer.scroll()), + }); } if matches!(state.dragging, Some(Dragging::ScrollbarH { .. })) { @@ -1207,7 +1204,7 @@ where ClickKind::Single }; let maybe_anchor = if state.modifiers.shift() { - state.shift_anchor.lock().unwrap().clone() + *state.shift_anchor.lock().unwrap() } else { None }; @@ -1388,11 +1385,9 @@ where shell.capture_event(); } } - Event::Window(WindowEvent::RedrawRequested(_now)) => { - if state.is_focused { - state.caret_position = editor.cursor_position().unwrap_or(state.caret_position); - shell.request_input_method(&self.input_method(state, scale_factor, layout)); - } + Event::Window(WindowEvent::RedrawRequested(_now)) if state.is_focused => { + state.caret_position = editor.cursor_position().unwrap_or(state.caret_position); + shell.request_input_method(&self.input_method(state, scale_factor, layout)); } _ => (), }