From 1e1eb95926f556e666bc20355327abd24d264858 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Thu, 1 Aug 2024 22:07:04 +0200 Subject: minor cleanup --- src/widgets/logview.rs | 3 ++- src/widgets/luaeditor/buffer.rs | 10 ++++---- src/widgets/luaeditor/mod.rs | 25 ++++++++++++++------ src/widgets/luaeditor/treesitter.rs | 4 ++-- src/widgets/sheetview/mod.rs | 47 ++++++++++++++++--------------------- src/widgets/statusbar.rs | 12 +++++----- 6 files changed, 53 insertions(+), 48 deletions(-) (limited to 'src/widgets') diff --git a/src/widgets/logview.rs b/src/widgets/logview.rs index c5b6432..35c7bed 100644 --- a/src/widgets/logview.rs +++ b/src/widgets/logview.rs @@ -4,6 +4,7 @@ use crate::lua::iobuffer::iobuffer; use super::statusbar::StatusBar; +#[derive(Default)] pub struct LogView { bar: StatusBar, } @@ -29,7 +30,7 @@ impl Widget for &mut LogView { buffer.get() }; - let mut line_area = inner_area.clone(); + let mut line_area = inner_area; line_area.height = 1; line_area.y = inner_area.y + inner_area.height - 1; diff --git a/src/widgets/luaeditor/buffer.rs b/src/widgets/luaeditor/buffer.rs index 3f59b6c..1af0c08 100644 --- a/src/widgets/luaeditor/buffer.rs +++ b/src/widgets/luaeditor/buffer.rs @@ -18,7 +18,7 @@ impl Buffer { fn refresh_line_max(&mut self) { self.cursor - .set_x_max(self.lines[self.cursor.y() as usize].len()) + .set_x_max(self.lines[self.cursor.y()].len()) } fn refresh_buffer_max(&mut self) { @@ -39,7 +39,7 @@ impl Buffer { s.starts_with(&(str.clone() + "if ")) || s.starts_with(&(str.clone() + "for ")) || s.starts_with(&(str.clone() + "while ")) - || (s.starts_with(&str) && s.contains("function(") && s.ends_with(")")) + || (s.starts_with(&str) && s.contains("function(") && s.ends_with(')')) }) .count(); @@ -66,7 +66,7 @@ impl Buffer { let l = a.trim(); let insert_end = (l.starts_with("if ") && l.ends_with(" then")) || ((l.starts_with("for ") || l.starts_with("while ")) && l.ends_with(" do")) - || (l.contains("function(") && l.ends_with(")")); + || (l.contains("function(") && l.ends_with(')')); let extra_indent = if insert_end { 1 } else { 0 }; @@ -126,11 +126,11 @@ impl Buffer { } pub fn current_line(&self) -> &String { - self.line(self.cursor.y() as usize).unwrap() + self.line(self.cursor.y()).unwrap() } fn current_line_mut(&mut self) -> &mut String { - self.line_mut(self.cursor.y() as usize).unwrap() + self.line_mut(self.cursor.y()).unwrap() } pub fn cursor(&self) -> &Cursor { diff --git a/src/widgets/luaeditor/mod.rs b/src/widgets/luaeditor/mod.rs index 98af9e5..939a65f 100644 --- a/src/widgets/luaeditor/mod.rs +++ b/src/widgets/luaeditor/mod.rs @@ -97,10 +97,10 @@ impl Widget for &mut LuaEditor { let highlights = treesitter::highlighter_split(text.as_bytes(), &self.highlight_config); let nr_width = (self.buffer.lines().len().to_string().len() + 1).max(4) as u16; - let mut text_area = inner_area.clone(); + let mut text_area = inner_area; text_area.x += nr_width; - let mut span_area = text_area.clone(); + let mut span_area = text_area; let mut current_line = 0; for (hl, group) in highlights.iter() { @@ -112,7 +112,7 @@ impl Widget for &mut LuaEditor { current_line += 1; } else if current_line >= self.scroll { - let group = group.replace("\t", " "); + let group = group.replace('\t', " "); let span = group.to_span(); if inner_area.contains(span_area.into()) { @@ -124,7 +124,7 @@ impl Widget for &mut LuaEditor { } for i in self.scroll..self.buffer.lines().len() { - let mut nr_area = span_area.clone(); + let mut nr_area = span_area; nr_area.x = inner_area.x; nr_area.width = nr_width - 1; nr_area.y = inner_area.y + (i - self.scroll) as u16; @@ -137,7 +137,7 @@ impl Widget for &mut LuaEditor { (i + 1).to_line().right_aligned().render(nr_area, buf); } - let mut cursor_area = text_area.clone(); + let mut cursor_area = text_area; cursor_area.width = 1; cursor_area.height = 1; cursor_area.y += self.buffer.cursor().y() as u16; @@ -146,7 +146,7 @@ impl Widget for &mut LuaEditor { let (first, _) = self .buffer .current_line() - .split_at(self.buffer.cursor().x() as usize); + .split_at(self.buffer.cursor().x()); for c in first.chars() { if c == '\t' { @@ -166,7 +166,7 @@ impl Widget for &mut LuaEditor { self.buffer .current_line() .chars() - .nth(self.buffer.cursor().x() as usize) + .nth(self.buffer.cursor().x()) .map(|c| if c == '\t' { ' ' } else { c }) .unwrap_or(' ') .to_span() @@ -175,3 +175,14 @@ impl Widget for &mut LuaEditor { } } } + +impl Default for LuaEditor { + fn default() -> Self { + Self { + highlight_config: treesitter::new_highlight_configuration(), + bar: StatusBar::default(), + scroll: 0, + buffer: Buffer::default(), + } + } +} diff --git a/src/widgets/luaeditor/treesitter.rs b/src/widgets/luaeditor/treesitter.rs index 1a4dc04..1474434 100644 --- a/src/widgets/luaeditor/treesitter.rs +++ b/src/widgets/luaeditor/treesitter.rs @@ -1,6 +1,6 @@ use tree_sitter_highlight::{Highlight, HighlightConfiguration, HighlightEvent, Highlighter}; -static HIGHLIGHT_NAMES: [&'static str; 29] = [ +static HIGHLIGHT_NAMES: [&str; 29] = [ "attribute", "boolean", "comment", @@ -71,7 +71,7 @@ pub fn highlighter_split<'a>( splits.push((current, "\n")); } - if !group.ends_with("\n") { + if !group.ends_with('\n') { splits.pop(); } } diff --git a/src/widgets/sheetview/mod.rs b/src/widgets/sheetview/mod.rs index 6ef8125..f81edab 100644 --- a/src/widgets/sheetview/mod.rs +++ b/src/widgets/sheetview/mod.rs @@ -9,7 +9,7 @@ use ratatui::{ }; use crate::{ - config::{theme::style, GlobalConfig}, + config::GlobalConfig, lua, sheet::{ cell::Cell, @@ -23,7 +23,9 @@ use super::{luaeditor::LuaEditor, statusbar::StatusBar}; const DEFAULT_COLUMN_WIDTH: u16 = 10; +#[derive(Default)] enum SheetViewMode { + #[default] Normal, Insert, Visual, @@ -34,6 +36,7 @@ enum SheetViewMode { EditorError(String), } +#[derive(Default)] pub struct SheetView { bar: StatusBar, sheet: SheetId, @@ -153,7 +156,7 @@ impl SheetView { KeyCode::Char('h') => self.move_cursor_by((0, -1)), KeyCode::Char('l') => self.move_cursor_by((0, 1)), KeyCode::Char('v') => { - self.selection_anchor = Some(self.cursor.clone()); + self.selection_anchor = Some(self.cursor); self.mode = SheetViewMode::Visual } KeyCode::Char('s') => { @@ -175,7 +178,7 @@ impl SheetView { let lock = Register::get(self.sheet).unwrap(); let mut sheet = lock.write().unwrap(); - if let Some(_) = self.selection_anchor { + if self.selection_anchor.is_some() { self.selection().iter().map(|(r, c)| (*r, *c)).collect() } else { vec![self.cursor] @@ -235,12 +238,10 @@ impl SheetView { KeyCode::Enter => { if let Err(error) = lua::get().load(self.bar.input().unwrap_or("")).exec() { self.mode = SheetViewMode::CommandError(error.to_string()) + } else if self.selection_anchor.is_some() { + self.mode = SheetViewMode::Visual; } else { - if self.selection_anchor.is_some() { - self.mode = SheetViewMode::Visual; - } else { - self.mode = SheetViewMode::Normal; - } + self.mode = SheetViewMode::Normal; } self.bar.set_input_mode(false); @@ -271,7 +272,7 @@ impl SheetView { let mut cells = Vec::new(); - if let Some(_) = self.selection_anchor { + if self.selection_anchor.is_some() { cells = self .selection() .iter() @@ -311,26 +312,18 @@ impl SheetView { } fn is_editor_visible(&self) -> bool { - match self.mode { - SheetViewMode::Script - | SheetViewMode::Processing(_) - | SheetViewMode::EditorError(_) => true, - _ => false, - } + matches!( + self.mode, + SheetViewMode::Script | SheetViewMode::Processing(_) | SheetViewMode::EditorError(_), + ) } fn is_error_window_visible(&self) -> bool { - match self.mode { - SheetViewMode::EditorError(_) => true, - _ => false, - } + matches!(self.mode, SheetViewMode::EditorError(_)) } fn is_progress_visible(&self) -> bool { - match self.mode { - SheetViewMode::Processing(_) => true, - _ => false, - } + matches!(self.mode, SheetViewMode::Processing(_)) } fn areas(&self, area: Rect) -> (Rect, Rect, Rect, Rect, Rect) { @@ -428,19 +421,19 @@ impl Widget for &mut SheetView { theme .cursor .get(cell_ref, &lua::get()) - .unwrap_or(style::Style::new()) + .unwrap_or_default() .apply(cell.to_line()) } else if self.is_in_selection(cell_pos_y, cell_pos_x) { theme .selection .get(cell_ref, &lua::get()) - .unwrap_or(style::Style::new()) + .unwrap_or_default() .apply(cell.to_line()) } else { theme .cell .get(cell_ref, &lua::get()) - .unwrap_or(style::Style::new()) + .unwrap_or_default() .apply(cell.to_line()) }; @@ -537,7 +530,7 @@ impl Widget for &mut SheetView { ) .percent(progress as u16); - Clear::default().render(progress_area, buf); + Clear.render(progress_area, buf); gauge.render(progress_area, buf); } } diff --git a/src/widgets/statusbar.rs b/src/widgets/statusbar.rs index 60b58a6..c8d52e5 100644 --- a/src/widgets/statusbar.rs +++ b/src/widgets/statusbar.rs @@ -6,7 +6,7 @@ use ratatui::{ widgets::Widget, }; -#[derive(Clone)] +#[derive(Clone, Default)] pub struct StatusBar { left: String, left_style: Style, @@ -125,11 +125,11 @@ impl StatusBar { } pub fn input(&self) -> Option<&str> { - self.input.as_ref().map(|s| s.as_str()) + self.input.as_deref() } pub fn area(&self, rect: Rect) -> Rect { - let mut inner = rect.clone(); + let mut inner = rect; inner.height -= 1; inner } @@ -162,9 +162,9 @@ impl StatusBar { area.y += area.height - 1; area.height = 1; - let mut left = area.clone(); - let mut right = area.clone(); - let mut middle = area.clone(); + let mut left = area; + let mut right = area; + let mut middle = area; left.width = self.left.len() as u16; right.width = self.right.len() as u16; -- cgit v1.2.3-70-g09d2