Implement highlighting options

This commit is contained in:
Timothy Warren 2021-03-16 10:38:26 -04:00
parent b765f894fc
commit 5b20598fd3
4 changed files with 35 additions and 17 deletions

View File

@ -16,12 +16,13 @@ pub struct Document {
impl Document {
pub fn open(filename: &str) -> Result<Self, std::io::Error> {
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);
}
}

View File

@ -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 {
@ -33,3 +37,9 @@ impl FileType {
Self::default()
}
}
impl HighlightingOptions {
pub fn numbers(self) -> bool {
self.numbers
}
}

View File

@ -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;

View File

@ -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<char> = self.string.chars().collect();
let mut matches = Vec::new();
@ -230,6 +231,7 @@ impl Row {
&highlighting::Type::None
};
if opts.numbers() {
if (c.is_ascii_digit()
&& (prev_is_separator || previous_highlight == &highlighting::Type::Number))
|| (c == &'.' && previous_highlight == &highlighting::Type::Number)
@ -238,6 +240,9 @@ impl Row {
} else {
highlighting.push(highlighting::Type::None)
}
} else {
highlighting.push(highlighting::Type::None)
}
prev_is_separator = c.is_ascii_punctuation() || c.is_ascii_whitespace();
index += 1;