Implement highlighting options
This commit is contained in:
parent
b765f894fc
commit
5b20598fd3
@ -16,12 +16,13 @@ pub struct Document {
|
|||||||
impl Document {
|
impl Document {
|
||||||
pub fn open(filename: &str) -> Result<Self, std::io::Error> {
|
pub fn open(filename: &str) -> Result<Self, std::io::Error> {
|
||||||
let contents = fs::read_to_string(filename)?;
|
let contents = fs::read_to_string(filename)?;
|
||||||
|
let file_type = FileType::from(filename);
|
||||||
|
|
||||||
let mut rows = Vec::new();
|
let mut rows = Vec::new();
|
||||||
|
|
||||||
for value in contents.lines() {
|
for value in contents.lines() {
|
||||||
let mut row = Row::from(value);
|
let mut row = Row::from(value);
|
||||||
row.highlight(None);
|
row.highlight(file_type.highlighting_options(), None);
|
||||||
rows.push(row);
|
rows.push(row);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,7 +30,7 @@ impl Document {
|
|||||||
rows,
|
rows,
|
||||||
file_name: Some(filename.to_string()),
|
file_name: Some(filename.to_string()),
|
||||||
dirty: false,
|
dirty: false,
|
||||||
file_type: FileType::from(filename),
|
file_type,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,13 +66,13 @@ impl Document {
|
|||||||
if at.y == self.rows.len() {
|
if at.y == self.rows.len() {
|
||||||
let mut row = Row::default();
|
let mut row = Row::default();
|
||||||
row.insert(0, c);
|
row.insert(0, c);
|
||||||
row.highlight(None);
|
row.highlight(self.file_type.highlighting_options(), None);
|
||||||
self.rows.push(row);
|
self.rows.push(row);
|
||||||
} else {
|
} else {
|
||||||
#[allow(clippy::indexing_slicing)]
|
#[allow(clippy::indexing_slicing)]
|
||||||
let row = &mut self.rows[at.y];
|
let row = &mut self.rows[at.y];
|
||||||
row.insert(at.x, c);
|
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 next_row = self.rows.remove(at.y + 1);
|
||||||
let row = &mut self.rows[at.y];
|
let row = &mut self.rows[at.y];
|
||||||
row.append(&next_row);
|
row.append(&next_row);
|
||||||
row.highlight(None);
|
row.highlight(self.file_type.highlighting_options(), None);
|
||||||
} else {
|
} else {
|
||||||
let row = &mut self.rows[at.y];
|
let row = &mut self.rows[at.y];
|
||||||
row.delete(at.x);
|
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 {
|
if let Some(file_name) = &self.file_name {
|
||||||
let mut file = fs::File::create(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(row.as_bytes())?;
|
||||||
file.write_all(b"\n")?;
|
file.write_all(b"\n")?;
|
||||||
|
row.highlight(self.file_type.highlighting_options(), None)
|
||||||
}
|
}
|
||||||
|
|
||||||
// File has been cleaned! (Saved)
|
// File has been cleaned! (Saved)
|
||||||
self.file_type = FileType::from(file_name);
|
|
||||||
self.dirty = false;
|
self.dirty = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,7 +118,7 @@ impl Document {
|
|||||||
|
|
||||||
pub fn highlight(&mut self, word: Option<&str>) {
|
pub fn highlight(&mut self, word: Option<&str>) {
|
||||||
for row in &mut self.rows {
|
for row in &mut self.rows {
|
||||||
row.highlight(word);
|
row.highlight(self.file_type.highlighting_options(), word);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,9 +3,9 @@ pub struct FileType {
|
|||||||
hl_opts: HighlightingOptions,
|
hl_opts: HighlightingOptions,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default, Copy, Clone)]
|
||||||
pub struct HighlightingOptions {
|
pub struct HighlightingOptions {
|
||||||
pub numbers: bool,
|
numbers: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for FileType {
|
impl Default for FileType {
|
||||||
@ -22,6 +22,10 @@ impl FileType {
|
|||||||
self.name.clone()
|
self.name.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn highlighting_options(&self) -> HighlightingOptions {
|
||||||
|
self.hl_opts
|
||||||
|
}
|
||||||
|
|
||||||
pub fn from(file_name: &str) -> Self {
|
pub fn from(file_name: &str) -> Self {
|
||||||
if file_name.ends_with(".rs") {
|
if file_name.ends_with(".rs") {
|
||||||
return Self {
|
return Self {
|
||||||
@ -32,4 +36,10 @@ impl FileType {
|
|||||||
|
|
||||||
Self::default()
|
Self::default()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HighlightingOptions {
|
||||||
|
pub fn numbers(self) -> bool {
|
||||||
|
self.numbers
|
||||||
|
}
|
||||||
}
|
}
|
@ -12,6 +12,7 @@ use editor::Editor;
|
|||||||
pub use editor::Position;
|
pub use editor::Position;
|
||||||
pub use editor::SearchDirection;
|
pub use editor::SearchDirection;
|
||||||
pub use filetype::FileType;
|
pub use filetype::FileType;
|
||||||
|
pub use filetype::HighlightingOptions;
|
||||||
pub use row::Row;
|
pub use row::Row;
|
||||||
pub use terminal::Terminal;
|
pub use terminal::Terminal;
|
||||||
|
|
||||||
|
17
src/row.rs
17
src/row.rs
@ -1,4 +1,5 @@
|
|||||||
use crate::highlighting;
|
use crate::highlighting;
|
||||||
|
use crate::HighlightingOptions;
|
||||||
use crate::SearchDirection;
|
use crate::SearchDirection;
|
||||||
use std::cmp;
|
use std::cmp;
|
||||||
use termion::color;
|
use termion::color;
|
||||||
@ -189,7 +190,7 @@ impl Row {
|
|||||||
None
|
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 mut highlighting = Vec::new();
|
||||||
let chars: Vec<char> = self.string.chars().collect();
|
let chars: Vec<char> = self.string.chars().collect();
|
||||||
let mut matches = Vec::new();
|
let mut matches = Vec::new();
|
||||||
@ -230,11 +231,15 @@ impl Row {
|
|||||||
&highlighting::Type::None
|
&highlighting::Type::None
|
||||||
};
|
};
|
||||||
|
|
||||||
if (c.is_ascii_digit()
|
if opts.numbers() {
|
||||||
&& (prev_is_separator || previous_highlight == &highlighting::Type::Number))
|
if (c.is_ascii_digit()
|
||||||
|| (c == &'.' && previous_highlight == &highlighting::Type::Number)
|
&& (prev_is_separator || previous_highlight == &highlighting::Type::Number))
|
||||||
{
|
|| (c == &'.' && previous_highlight == &highlighting::Type::Number)
|
||||||
highlighting.push(highlighting::Type::Number)
|
{
|
||||||
|
highlighting.push(highlighting::Type::Number)
|
||||||
|
} else {
|
||||||
|
highlighting.push(highlighting::Type::None)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
highlighting.push(highlighting::Type::None)
|
highlighting.push(highlighting::Type::None)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user