Build up highlighting array

This commit is contained in:
Timothy Warren 2021-03-15 13:53:30 -04:00
parent 49807ec9fe
commit 4618b2be0e
4 changed files with 45 additions and 1 deletions

View File

@ -18,7 +18,9 @@ impl Document {
let mut rows = Vec::new();
for value in contents.lines() {
rows.push(Row::from(value));
let mut row = Row::From(value);
row.highlight();
rows.push(row);
}
Ok(Self {
@ -56,11 +58,13 @@ impl Document {
if at.y == self.rows.len() {
let mut row = Row::default();
row.insert(0, c);
row.highlight();
self.rows.push(row);
} else {
#[allow(clippy::indexing_slicing)]
let row = &mut self.rows[at.y];
row.insert(at.x, c);
row.highlight();
}
}
@ -78,9 +82,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();
} else {
let row = &mut self.rows[at.y];
row.delete(at.x);
row.highlight();
}
}
@ -150,8 +156,12 @@ impl Document {
}
#[allow(clippy::indexing_slicing)]
let current_row = &mut self.rows[at.y];
let new_row = self.rows[at.y].split(at.x);
current_row.highlight();
new_row.highlight();
#[allow(clippy::integer_arithmetic)]
self.rows.insert(at.y + 1, new_row);
}

15
src/highlighting.rs Normal file
View File

@ -0,0 +1,15 @@
use termion::color;
pub enum Type {
None,
Number,
}
impl Type {
pub fn to_color(&self) -> impl color::Color {
match self {
Type::Number => color::Rgb(220, 163, 163),
_ => color::Rgb(255, 255, 255),
}
}
}

View File

@ -2,6 +2,7 @@
#![allow(clippy::missing_errors_doc, clippy::must_use_candidate, clippy::panic)]
mod document;
mod editor;
mod highlighting;
mod row;
mod terminal;

View File

@ -1,3 +1,4 @@
use crate::highlighting;
use crate::SearchDirection;
use std::cmp;
use termion::color;
@ -6,6 +7,7 @@ use unicode_segmentation::UnicodeSegmentation;
#[derive(Default)]
pub struct Row {
string: String,
highlighting: Vec<highlighting::Type>,
len: usize,
}
@ -13,6 +15,7 @@ impl From<&str> for Row {
fn from(slice: &str) -> Self {
Self {
string: String::from(slice),
highlighting: Vec::new(),
len: slice.graphemes(true).count(),
}
}
@ -133,6 +136,7 @@ impl Row {
Self {
string: splitted_row,
len: splittend_length,
highlighting: Vec::new(),
}
}
@ -174,4 +178,18 @@ impl Row {
None
}
pub fn highlight(&mut self) {
let mut highlighting = Vec::new();
for c in self.string.chars() {
if c.is_ascii_digit() {
highlighting.push(highlighting::Type::Number)
} else {
highlighting.push(highlighting::Type::None)
}
}
self.highlighting = highlighting;
}
}