From 5b20598fd319df4db5910ebc87430c39d082a169 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 16 Mar 2021 10:38:26 -0400 Subject: [PATCH] Implement highlighting options --- src/document.rs | 20 +++++++++++--------- src/filetype.rs | 14 ++++++++++++-- src/main.rs | 1 + src/row.rs | 17 +++++++++++------ 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/document.rs b/src/document.rs index 2ec4d1a..2a83bf5 100644 --- a/src/document.rs +++ b/src/document.rs @@ -16,12 +16,13 @@ pub struct Document { impl Document { pub fn open(filename: &str) -> Result { let contents = fs::read_to_string(filename)?; + let file_type = FileType::from(filename); let mut rows = Vec::new(); for value in contents.lines() { let mut row = Row::from(value); - row.highlight(None); + row.highlight(file_type.highlighting_options(), None); rows.push(row); } @@ -29,7 +30,7 @@ impl Document { rows, file_name: Some(filename.to_string()), dirty: false, - file_type: FileType::from(filename), + file_type, }) } @@ -65,13 +66,13 @@ impl Document { if at.y == self.rows.len() { let mut row = Row::default(); row.insert(0, c); - row.highlight(None); + row.highlight(self.file_type.highlighting_options(), None); self.rows.push(row); } else { #[allow(clippy::indexing_slicing)] let row = &mut self.rows[at.y]; row.insert(at.x, c); - row.highlight(None); + row.highlight(self.file_type.highlighting_options(), None); } } @@ -89,11 +90,11 @@ impl Document { let next_row = self.rows.remove(at.y + 1); let row = &mut self.rows[at.y]; row.append(&next_row); - row.highlight(None); + row.highlight(self.file_type.highlighting_options(), None); } else { let row = &mut self.rows[at.y]; row.delete(at.x); - row.highlight(None); + row.highlight(self.file_type.highlighting_options(), None); } } @@ -101,13 +102,14 @@ impl Document { if let Some(file_name) = &self.file_name { let mut file = fs::File::create(file_name)?; - for row in &self.rows { + self.file_type = FileType::from(file_name); + for row in &mut self.rows { file.write_all(row.as_bytes())?; file.write_all(b"\n")?; + row.highlight(self.file_type.highlighting_options(), None) } // File has been cleaned! (Saved) - self.file_type = FileType::from(file_name); self.dirty = false; } @@ -116,7 +118,7 @@ impl Document { pub fn highlight(&mut self, word: Option<&str>) { for row in &mut self.rows { - row.highlight(word); + row.highlight(self.file_type.highlighting_options(), word); } } diff --git a/src/filetype.rs b/src/filetype.rs index b191cbf..8710a6a 100644 --- a/src/filetype.rs +++ b/src/filetype.rs @@ -3,9 +3,9 @@ pub struct FileType { hl_opts: HighlightingOptions, } -#[derive(Default)] +#[derive(Default, Copy, Clone)] pub struct HighlightingOptions { - pub numbers: bool, + numbers: bool, } impl Default for FileType { @@ -22,6 +22,10 @@ impl FileType { 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 { @@ -32,4 +36,10 @@ impl FileType { Self::default() } +} + +impl HighlightingOptions { + pub fn numbers(self) -> bool { + self.numbers + } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 3135e82..217272a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ use editor::Editor; pub use editor::Position; pub use editor::SearchDirection; pub use filetype::FileType; +pub use filetype::HighlightingOptions; pub use row::Row; pub use terminal::Terminal; diff --git a/src/row.rs b/src/row.rs index a788de3..5d3f434 100644 --- a/src/row.rs +++ b/src/row.rs @@ -1,4 +1,5 @@ use crate::highlighting; +use crate::HighlightingOptions; use crate::SearchDirection; use std::cmp; use termion::color; @@ -189,7 +190,7 @@ impl Row { None } - pub fn highlight(&mut self, word: Option<&str>) { + pub fn highlight(&mut self, opts: HighlightingOptions, word: Option<&str>) { let mut highlighting = Vec::new(); let chars: Vec = self.string.chars().collect(); let mut matches = Vec::new(); @@ -230,11 +231,15 @@ impl Row { &highlighting::Type::None }; - if (c.is_ascii_digit() - && (prev_is_separator || previous_highlight == &highlighting::Type::Number)) - || (c == &'.' && previous_highlight == &highlighting::Type::Number) - { - highlighting.push(highlighting::Type::Number) + if opts.numbers() { + if (c.is_ascii_digit() + && (prev_is_separator || previous_highlight == &highlighting::Type::Number)) + || (c == &'.' && previous_highlight == &highlighting::Type::Number) + { + highlighting.push(highlighting::Type::Number) + } else { + highlighting.push(highlighting::Type::None) + } } else { highlighting.push(highlighting::Type::None) }