From 86a2c9be3c6df050a3ba8e4253c1532b33dcd313 Mon Sep 17 00:00:00 2001 From: Artem Streltsov Date: Fri, 23 Feb 2024 10:47:49 +0000 Subject: [PATCH] detect file type --- src/document.rs | 31 ++++++++++++++++++++----------- src/editor.rs | 3 ++- src/filetype.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 3 +++ src/row.rs | 19 ++++++++++++------- 5 files changed, 79 insertions(+), 19 deletions(-) create mode 100644 src/filetype.rs diff --git a/src/document.rs b/src/document.rs index 949f643..5fd41cc 100644 --- a/src/document.rs +++ b/src/document.rs @@ -1,3 +1,4 @@ +use crate::FileType; use crate::Position; use crate::Row; use crate::SearchDirection; @@ -8,22 +9,25 @@ use std::io::{Error, Write}; pub struct Document { rows: Vec, pub file_name: Option, - dirty: bool + dirty: bool, + file_type: FileType } 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); } Ok(Self { rows, file_name: Some(filename.to_string()), - dirty: false + dirty: false, + file_type }) } pub fn row(&self, index: usize) -> Option<&Row> { @@ -47,13 +51,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); } } #[allow(clippy::indexing_slicing)] @@ -68,11 +72,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); } } fn insert_newline(&mut self, at: &Position) { @@ -86,16 +90,18 @@ impl Document { #[allow(clippy::indexing_slicing)] let current_row = &mut self.rows[at.y]; let mut new_row = current_row.split(at.x); - current_row.highlight(None); - new_row.highlight(None); + current_row.highlight(self.file_type.highlighting_options(), None); + new_row.highlight(self.file_type.highlighting_options(), None); 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)?; - 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) } self.dirty = false; } @@ -144,7 +150,10 @@ 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); } } + pub fn file_type(&self) -> String { + self.file_type.name() + } } diff --git a/src/editor.rs b/src/editor.rs index 975eedb..f2ddcc4 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -295,7 +295,8 @@ impl Editor { ); status.truncate(width); let line_indicator = format!( - "{}/{}", + "{} | {}/{}", + self.document.file_type(), self.cursor_position.y.saturating_add(1), self.document.len() ); diff --git a/src/filetype.rs b/src/filetype.rs new file mode 100644 index 0000000..403b984 --- /dev/null +++ b/src/filetype.rs @@ -0,0 +1,42 @@ +pub struct FileType { + name: String, + hl_opts: HighlightingOptions +} + +#[derive(Default, Copy, Clone)] +pub struct HighlightingOptions { + numbers: bool +} + +impl Default for FileType { + fn default() -> Self { + Self { + name: String::from("No filetype"), + hl_opts: HighlightingOptions::default() + } + } +} + +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 { + name: String::from("Rust"), + hl_opts: HighlightingOptions{ numbers: true } + } + } + Self::default() + } +} + +impl HighlightingOptions { + pub fn numbers(&self) -> bool { + self.numbers + } +} diff --git a/src/main.rs b/src/main.rs index d85e3f3..bdf90ba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,7 @@ )] mod document; mod editor; +mod filetype; mod row; mod terminal; mod highlighting; @@ -16,6 +17,8 @@ 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; pub use editor::Position; diff --git a/src/row.rs b/src/row.rs index aac135b..da7d731 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, usize}; use termion::color; @@ -151,7 +152,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(); @@ -186,12 +187,16 @@ impl Row { } else { &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); }