From 93c57a43e06ad799cba6b522232e9a5f10be2643 Mon Sep 17 00:00:00 2001 From: Artem Streltsov Date: Mon, 26 Feb 2024 18:34:31 +0000 Subject: [PATCH] slightly change code formatting --- src/document.rs | 52 +++++++++++++++++++++++++++---------------------- src/editor.rs | 12 ++++++++++++ src/filetype.rs | 8 ++++++++ src/main.rs | 4 +++- src/row.rs | 9 +++++++++ src/terminal.rs | 15 +++++++++++++- 6 files changed, 75 insertions(+), 25 deletions(-) diff --git a/src/document.rs b/src/document.rs index 9d8c568..c10ea46 100644 --- a/src/document.rs +++ b/src/document.rs @@ -28,15 +28,19 @@ impl Document { file_type }) } + pub fn row(&self, index: usize) -> Option<&Row> { self.rows.get(index) } + pub fn is_empty(&self) -> bool { self.rows.is_empty() } + pub fn len(&self) -> usize { self.rows.len() } + pub fn insert(&mut self, at: &Position, c: char) { if at.y > self.rows.len() { return; @@ -56,13 +60,20 @@ impl Document { self.unhighlight_rows(at.y); } - fn unhighlight_rows(&mut self, start: usize) { - let start = start.saturating_sub(1); - for row in self.rows.iter_mut().skip(start) { - row.is_highlighted = false; + fn insert_newline(&mut self, at: &Position) { + if at.y > self.rows.len() { + return; + } + if at.y == self.rows.len() { + self.rows.push(Row::default()); + return; } + #[allow(clippy::indexing_slicing)] + let current_row = &mut self.rows[at.y]; + let new_row = current_row.split(at.x); + self.rows.insert(at.y + 1, new_row); } - + #[allow(clippy::indexing_slicing)] pub fn delete(&mut self, at: &Position) { let len = self.rows.len(); @@ -70,7 +81,6 @@ impl Document { return; } self.dirty = true; - if at.x == self.rows[at.y].len() && at.y + 1 < len { let next_row = self.rows.remove(at.y + 1); let row = &mut self.rows[at.y]; @@ -81,19 +91,7 @@ impl Document { } self.unhighlight_rows(at.y) } - fn insert_newline(&mut self, at: &Position) { - if at.y > self.rows.len() { - return; - } - if at.y == self.rows.len() { - self.rows.push(Row::default()); - return; - } - #[allow(clippy::indexing_slicing)] - let current_row = &mut self.rows[at.y]; - let new_row = current_row.split(at.x); - self.rows.insert(at.y + 1, new_row); - } + pub fn save(&mut self) -> Result<(), Error> { if let Some(file_name) = &self.file_name { let mut file = fs::File::create(file_name)?; @@ -106,28 +104,27 @@ impl Document { } Ok(()) } + pub fn is_dirty(&self) -> bool { self.dirty } + #[allow(clippy::indexing_slicing)] pub fn find(&self, query: &str, at: &Position, direction: SearchDirection) -> Option { if at.y >= self.rows.len() { return None; } - let mut position = Position{x: at.x, y: at.y}; - + let mut position = Position { x: at.x, y: at.y }; let start = if direction == SearchDirection::Forward { at.y } else { 0 }; - let end = if direction == SearchDirection::Forward { self.rows.len() } else { at.y.saturating_add(1) }; - for _ in start..end { if let Some(row) = self.rows.get(position.y) { if let Some(x) = row.find(&query, position.x, direction) { @@ -147,6 +144,7 @@ impl Document { } None } + pub fn highlight(&mut self, word: &Option, until: Option) { let mut start_with_comment = false; let until = if let Some(until) = until { @@ -163,6 +161,14 @@ impl Document { start_with_comment = row.highlight(&self.file_type.highlighting_options(), word, start_with_comment); } } + + fn unhighlight_rows(&mut self, start: usize) { + let start = start.saturating_sub(1); + for row in self.rows.iter_mut().skip(start) { + row.is_highlighted = false; + } + } + pub fn file_type(&self) -> String { self.file_type.name() } diff --git a/src/editor.rs b/src/editor.rs index f851681..31ec585 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -66,6 +66,7 @@ impl Editor { } } } + pub fn default() -> Self { let args: Vec = env::args().collect(); let mut initial_status = String::from("HELP: Ctrl-F = find | Ctrl-S = save | Ctrl-Q = quit"); @@ -112,6 +113,7 @@ impl Editor { Terminal::cursor_show(); Terminal::flush() } + fn process_keypress(&mut self) -> Result<(), std::io::Error> { let pressed_key = Terminal::read_key()?; match pressed_key { @@ -156,6 +158,7 @@ impl Editor { } Ok(()) } + pub fn draw_row(&self, row: &Row) { let width = self.terminal.size().width as usize; let start = self.offset.x; @@ -163,6 +166,7 @@ impl Editor { let row = row.render(start, end); println!("{}\r", row) } + #[allow(clippy::integer_division)] fn draw_rows(&self) { let height = self.terminal.size().height; @@ -180,6 +184,7 @@ impl Editor { } } } + fn draw_welcome_message(&self) { let mut welcome_message = format!("Artem's editor -- version {}\r", VERSION); let width = self.terminal.size().width as usize; @@ -191,6 +196,7 @@ impl Editor { welcome_message.truncate(width); println!("{}\r", welcome_message); } + fn move_cursor(&mut self, key: Key) { let terminal_height = self.terminal.size().height as usize; let Position{ mut x, mut y } = self.cursor_position; @@ -258,6 +264,7 @@ impl Editor { self.cursor_position = Position{ x, y } } + fn scroll(&mut self) { let Position {x, y} = self.cursor_position; let width = self.terminal.size().width as usize; @@ -276,6 +283,7 @@ impl Editor { offset.x = x.saturating_sub(width).saturating_add(1); } } + fn draw_status_bar(&self) { let mut status; let width = self.terminal.size().width as usize; @@ -312,6 +320,7 @@ impl Editor { Terminal::reset_fg_color(); Terminal::reset_bg_color(); } + fn draw_message_bar(&self) { Terminal::clear_current_line(); let message = &self.status_message; @@ -321,6 +330,7 @@ impl Editor { print!("{}", text); } } + fn prompt(&mut self, prompt: &str, mut callback: C) -> Result, std::io::Error> where C: FnMut(&mut Self, Key, &String), @@ -352,6 +362,7 @@ impl Editor { } Ok(Some(result)) } + fn save(&mut self) { if self.document.file_name.is_none() { let new_name = self.prompt("Save as: ", |_, _, _| {}).unwrap_or(None); @@ -368,6 +379,7 @@ impl Editor { self.status_message = StatusMessage::from("Error writing file!".to_string()); } } + fn search(&mut self) { let old_position = self.cursor_position.clone(); let mut direction = SearchDirection::Forward; diff --git a/src/filetype.rs b/src/filetype.rs index a860f29..fefa41c 100644 --- a/src/filetype.rs +++ b/src/filetype.rs @@ -27,9 +27,11 @@ impl FileType { pub fn name(&self) -> String { self.name.clone() } + pub fn highlighting_options(&self) -> &HighlightingOptions { &self.hl_opts } + pub fn from(file_name: &str) -> Self { if file_name.ends_with(".rs") { return Self { @@ -120,21 +122,27 @@ impl HighlightingOptions { pub fn numbers(&self) -> bool { self.numbers } + pub fn strings(&self) -> bool { self.strings } + pub fn characters(&self) -> bool { self.characters } + pub fn comments(&self) -> bool { self.comments } + pub fn primary_keywords(&self) -> &Vec { &self.primary_keywords } + pub fn secondary_keywords(&self) -> &Vec { &self.secondary_keywords } + pub fn multiline_comments(&self) -> bool { self.multiline_comments } diff --git a/src/main.rs b/src/main.rs index bdf90ba..a6cd4e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,16 +7,18 @@ clippy::wildcard_enum_match_arm, clippy::else_if_without_else )] + mod document; mod editor; mod filetype; mod row; mod terminal; mod highlighting; + +use editor::Editor; pub use document::Document; pub use row::Row; pub use editor::SearchDirection; -use editor::Editor; pub use filetype::FileType; pub use filetype::HighlightingOptions; pub use terminal::Terminal; diff --git a/src/row.rs b/src/row.rs index b9abff6..85b6452 100644 --- a/src/row.rs +++ b/src/row.rs @@ -52,12 +52,15 @@ impl Row { result.push_str(&end_highlight[..]); result } + pub fn len(&self) -> usize { self.len } + pub fn is_empty(&self) -> bool { self.len == 0 } + pub fn insert(&mut self, at: usize, c: char) { if at >= self.len() { self.string.push(c); @@ -77,6 +80,7 @@ impl Row { self.len = length; self.string = result; } + pub fn delete(&mut self, at: usize) { if at >= self.len() { return; @@ -92,10 +96,12 @@ impl Row { self.len = length; self.string = result; } + pub fn append(&mut self, new: &Self) { self.string = format!("{}{}", self.string, new.string); self.len += new.len; } + pub fn split(&mut self, at: usize) -> Self { let mut row: String = String::new(); let mut length = 0; @@ -120,9 +126,11 @@ impl Row { is_highlighted: false } } + pub fn as_bytes(&self) -> &[u8] { self.string.as_bytes() } + pub fn find(&self, query: &str, at: usize, direction: SearchDirection) -> Option { if at > self.len || query.is_empty() { return None; @@ -230,6 +238,7 @@ impl Row { highlighting::Type::PrimaryKeywords, ) } + fn highlight_secondary_keywords(&mut self, index: &mut usize, opts: &HighlightingOptions, chars: &[char]) -> bool { self.highlight_keywords( index, diff --git a/src/terminal.rs b/src/terminal.rs index a80ee18..71faaf8 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -9,6 +9,7 @@ pub struct Size { pub width: u16, pub height: u16, } + pub struct Terminal { size: Size, _stdout: RawTerminal, @@ -25,24 +26,29 @@ impl Terminal { _stdout: stdout().into_raw_mode()?, }) } + pub fn size(&self) -> &Size { &self.size } + pub fn clear_screen() { print!("{}", termion::clear::All); } + #[allow(clippy::cast_possible_truncation)] pub fn cursor_position(position: &Position) { - let Position{ mut x, mut y } = position; + let Position { mut x, mut y } = position; x = x.saturating_add(1); y = y.saturating_add(1); let x = x as u16; let y = y as u16; print!("{}", termion::cursor::Goto(x, y)); } + pub fn flush() -> Result<(), std::io::Error> { io::stdout().flush() } + pub fn read_key() -> Result { loop { if let Some(key) = io::stdin().lock().keys().next() { @@ -50,24 +56,31 @@ impl Terminal { } } } + pub fn cursor_hide() { print!("{}", termion::cursor::Hide); } + pub fn cursor_show() { print!("{}", termion::cursor::Show); } + pub fn clear_current_line() { print!("{}", termion::clear::CurrentLine); } + pub fn set_bg_color(color: color::Rgb) { print!("{}", color::Bg(color)); } + pub fn reset_bg_color() { print!("{}", color::Bg(color::Reset)); } + pub fn set_fg_color(color: color::Rgb) { print!("{}", color::Fg(color)); } + pub fn reset_fg_color() { print!("{}", color::Fg(color::Reset)); }