Add more clippy warnings, and solve a bunch of them
This commit is contained in:
parent
cf0c576284
commit
297ff6b80b
@ -40,7 +40,7 @@ impl Document {
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, at: &Position, c: char) {
|
||||
if at.y > self.len() {
|
||||
if at.y > self.rows.len() {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -52,18 +52,20 @@ impl Document {
|
||||
return;
|
||||
}
|
||||
|
||||
if at.y == self.len() {
|
||||
if at.y == self.rows.len() {
|
||||
let mut row = Row::default();
|
||||
row.insert(0, c);
|
||||
self.rows.push(row);
|
||||
} else {
|
||||
let row = self.rows.get_mut(at.y).unwrap();
|
||||
#[allow(clippy::indexing_slicing)]
|
||||
let row = &mut self.rows[at.y];
|
||||
row.insert(at.x, c);
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::integer_arithmetic, clippy::indexing_slicing)]
|
||||
pub fn delete(&mut self, at: &Position) {
|
||||
let len = self.len();
|
||||
let len = self.rows.len();
|
||||
if at.y >= len {
|
||||
return
|
||||
}
|
||||
@ -71,12 +73,12 @@ impl Document {
|
||||
// File has been modified
|
||||
self.dirty = true;
|
||||
|
||||
if at.x == self.rows.get_mut(at.y).unwrap().len() && at.y < len - 1 {
|
||||
if at.x == self.rows[at.y].len() && at.y + 1 < len {
|
||||
let next_row = self.rows.remove(at.y + 1);
|
||||
let row = self.rows.get_mut(at.y).unwrap();
|
||||
let row = &mut self.rows[at.y];
|
||||
row.append(&next_row);
|
||||
} else {
|
||||
let row = self.rows.get_mut(at.y).unwrap();
|
||||
let row = &mut self.rows[at.y];
|
||||
row.delete(at.x);
|
||||
}
|
||||
}
|
||||
@ -102,12 +104,19 @@ impl Document {
|
||||
}
|
||||
|
||||
fn insert_newline(&mut self, at: &Position) {
|
||||
if at.y == self.len() {
|
||||
if at.y > self.rows.len() {
|
||||
return;
|
||||
}
|
||||
|
||||
if at.y == self.rows.len() {
|
||||
self.rows.push(Row::default());
|
||||
return;
|
||||
}
|
||||
|
||||
let new_row = self.rows.get_mut(at.y).unwrap().split(at.x);
|
||||
#[allow(clippy::indexing_slicing)]
|
||||
let new_row = self.rows[at.y].split(at.x);
|
||||
|
||||
#[allow(clippy::integer_arithmetic)]
|
||||
self.rows.insert(at.y + 1, new_row);
|
||||
}
|
||||
}
|
@ -64,11 +64,11 @@ impl Editor {
|
||||
pub fn default() -> Self {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
let mut initial_status = String::from("HELP: Ctrl-S = save | Ctrl-Q = quit");
|
||||
let document = if args.len() > 1 {
|
||||
let document = if let Some(file_name) = args.get(1) {
|
||||
let file_name = &args[1];
|
||||
let doc = Document::open(&file_name);
|
||||
if doc.is_ok() {
|
||||
doc.unwrap()
|
||||
if let Ok(doc) = doc {
|
||||
doc
|
||||
} else {
|
||||
initial_status = format!("ERR: Could not open file: {}", file_name);
|
||||
Document::default()
|
||||
@ -134,9 +134,11 @@ impl Editor {
|
||||
self.cursor_position.y.saturating_add(1),
|
||||
self.document.len()
|
||||
);
|
||||
|
||||
#[allow(clippy::integer_arithmetic)]
|
||||
let len = status.len() + line_indicator.len();
|
||||
if width > len {
|
||||
status.push_str(&" ".repeat(width - len));
|
||||
status.push_str(&" ".repeat(width.saturating_sub(len)));
|
||||
}
|
||||
status = format!("{}{}", status, line_indicator);
|
||||
status.truncate(width);
|
||||
@ -235,11 +237,7 @@ impl Editor {
|
||||
self.refresh_screen()?;
|
||||
|
||||
match Terminal::read_key()? {
|
||||
Key::Backspace => {
|
||||
if !result.is_empty() {
|
||||
result.truncate(result.len() - 1);
|
||||
}
|
||||
}
|
||||
Key::Backspace => result.truncate(result.len().saturating_sub(1)),
|
||||
Key::Char('\n') => break,
|
||||
Key::Char(c) => {
|
||||
if !c.is_control() {
|
||||
@ -322,14 +320,14 @@ impl Editor {
|
||||
},
|
||||
Key::PageUp => {
|
||||
y = if y > terminal_height {
|
||||
y - terminal_height
|
||||
y.saturating_sub(terminal_height)
|
||||
} else {
|
||||
0
|
||||
}
|
||||
},
|
||||
Key::PageDown => {
|
||||
y = if y.saturating_add(terminal_height) < height {
|
||||
y + terminal_height as usize
|
||||
y.saturating_add(terminal_height)
|
||||
} else {
|
||||
height
|
||||
}
|
||||
@ -356,28 +354,36 @@ impl Editor {
|
||||
let mut welcome_message = format!("Hecto editor -- version {}", VERSION);
|
||||
let width = self.terminal.size().width as usize;
|
||||
let len = welcome_message.len();
|
||||
|
||||
#[allow(clippy::integer_arithmetic, clippy::integer_division)]
|
||||
let padding = width.saturating_sub(len) / 2;
|
||||
let spaces = " ".repeat(padding.saturating_sub(1));
|
||||
|
||||
welcome_message = format!("~{}{}", spaces, welcome_message);
|
||||
welcome_message.truncate(width);
|
||||
|
||||
println!("{}\r", welcome_message);
|
||||
}
|
||||
|
||||
pub fn draw_row(&self, row: &Row) {
|
||||
let width = self.terminal.size().width as usize;
|
||||
let start = self.offset.x;
|
||||
let end = self.offset.x + width;
|
||||
let end = self.offset.x.saturating_add(width);
|
||||
let row = row.render(start, end);
|
||||
println!("{}\r", row);
|
||||
}
|
||||
|
||||
#[allow(clippy::integer_arithmetic, clippy::integer_division)]
|
||||
fn draw_rows(&self) {
|
||||
let height = self.terminal.size().height;
|
||||
|
||||
for terminal_row in 0..height {
|
||||
Terminal::clear_current_line();
|
||||
|
||||
if let Some(row) = self.document.row(terminal_row as usize + self.offset.y) {
|
||||
if let Some(row) = self
|
||||
.document
|
||||
.row(self.offset.y.saturating_add(terminal_row as usize))
|
||||
{
|
||||
self.draw_row(row);
|
||||
} else if self.document.is_empty() && terminal_row == height / 3 {
|
||||
self.draw_welcome_message();
|
||||
|
10
src/main.rs
10
src/main.rs
@ -1,4 +1,12 @@
|
||||
#![warn(clippy::all, clippy::pedantic)]
|
||||
#![warn(clippy::all, clippy::pedantic, clippy::restriction)]
|
||||
#![allow(
|
||||
clippy::missing_docs_in_private_items,
|
||||
clippy::implicit_return,
|
||||
clippy::shadow_reuse,
|
||||
clippy::print_stdout,
|
||||
clippy::wildcard_enum_match_arm,
|
||||
clippy::else_if_without_else
|
||||
)]
|
||||
mod document;
|
||||
mod editor;
|
||||
mod row;
|
||||
|
@ -26,6 +26,7 @@ impl Row {
|
||||
|
||||
let mut result = String::new();
|
||||
|
||||
#[allow(clippy::integer_arithmetic)]
|
||||
for grapheme in self.string[..]
|
||||
.graphemes(true)
|
||||
.skip(start)
|
||||
@ -69,6 +70,7 @@ impl Row {
|
||||
self.update_len();
|
||||
}
|
||||
|
||||
#[allow(clippy::integer_arithmetic)]
|
||||
pub fn delete(&mut self, at: usize) {
|
||||
if at >= self.len() {
|
||||
return
|
||||
|
Loading…
Reference in New Issue
Block a user