Skip to content

Commit

Permalink
highlight keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-streltsov committed Feb 24, 2024
1 parent 4284533 commit beda86d
Show file tree
Hide file tree
Showing 4 changed files with 268 additions and 91 deletions.
18 changes: 9 additions & 9 deletions src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Document {
let mut rows = Vec::new();
for value in contents.lines() {
let mut row = Row::from(value);
row.highlight(file_type.highlighting_options(), None);
row.highlight(&file_type.highlighting_options(), None);
rows.push(row);
}
Ok(Self {
Expand Down Expand Up @@ -51,13 +51,13 @@ impl Document {
if at.y == self.rows.len() {
let mut row = Row::default();
row.insert(0, c);
row.highlight(self.file_type.highlighting_options(), 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(self.file_type.highlighting_options(), None);
row.highlight(&self.file_type.highlighting_options(), None);
}
}
#[allow(clippy::indexing_slicing)]
Expand All @@ -72,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(self.file_type.highlighting_options(), None);
row.highlight(&self.file_type.highlighting_options(), None);
} else {
let row = &mut self.rows[at.y];
row.delete(at.x);
row.highlight(self.file_type.highlighting_options(), None);
row.highlight(&self.file_type.highlighting_options(), None);
}
}
fn insert_newline(&mut self, at: &Position) {
Expand All @@ -90,8 +90,8 @@ 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(self.file_type.highlighting_options(), None);
new_row.highlight(self.file_type.highlighting_options(), 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> {
Expand All @@ -101,7 +101,7 @@ impl Document {
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)
row.highlight(&self.file_type.highlighting_options(), None)
}
self.dirty = false;
}
Expand Down Expand Up @@ -150,7 +150,7 @@ impl Document {
}
pub fn highlight(&mut self, word: Option<&str>) {
for row in &mut self.rows {
row.highlight(self.file_type.highlighting_options(), word);
row.highlight(&self.file_type.highlighting_options(), word);
}
}
pub fn file_type(&self) -> String {
Expand Down
92 changes: 87 additions & 5 deletions src/filetype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ pub struct FileType {
hl_opts: HighlightingOptions
}

#[derive(Default, Copy, Clone)]
#[derive(Default)]
pub struct HighlightingOptions {
numbers: bool,
strings: bool,
characters: bool
characters: bool,
comments: bool,
primary_keywords: Vec<String>,
secondary_keywords: Vec<String>,
}

impl Default for FileType {
Expand All @@ -23,8 +26,8 @@ impl FileType {
pub fn name(&self) -> String {
self.name.clone()
}
pub fn highlighting_options(&self) -> HighlightingOptions {
self.hl_opts
pub fn highlighting_options(&self) -> &HighlightingOptions {
&self.hl_opts
}
pub fn from(file_name: &str) -> Self {
if file_name.ends_with(".rs") {
Expand All @@ -33,7 +36,77 @@ impl FileType {
hl_opts: HighlightingOptions{
numbers: true,
strings: true,
characters: true
characters: true,
comments: true,
primary_keywords: vec![
"as".to_string(),
"break".to_string(),
"const".to_string(),
"continue".to_string(),
"crate".to_string(),
"else".to_string(),
"enum".to_string(),
"extern".to_string(),
"false".to_string(),
"fn".to_string(),
"for".to_string(),
"if".to_string(),
"impl".to_string(),
"in".to_string(),
"let".to_string(),
"loop".to_string(),
"match".to_string(),
"mod".to_string(),
"move".to_string(),
"mut".to_string(),
"pub".to_string(),
"ref".to_string(),
"return".to_string(),
"self".to_string(),
"Self".to_string(),
"static".to_string(),
"struct".to_string(),
"super".to_string(),
"trait".to_string(),
"true".to_string(),
"type".to_string(),
"unsafe".to_string(),
"use".to_string(),
"where".to_string(),
"while".to_string(),
"dyn".to_string(),
"abstract".to_string(),
"become".to_string(),
"box".to_string(),
"do".to_string(),
"final".to_string(),
"macro".to_string(),
"override".to_string(),
"priv".to_string(),
"typeof".to_string(),
"unsized".to_string(),
"virtual".to_string(),
"yield".to_string(),
"async".to_string(),
"await".to_string(),
"try".to_string(),
],
secondary_keywords: vec![
"bool".to_string(),
"char".to_string(),
"i8".to_string(),
"i16".to_string(),
"i32".to_string(),
"i64".to_string(),
"isize".to_string(),
"u8".to_string(),
"u16".to_string(),
"u32".to_string(),
"u64".to_string(),
"usize".to_string(),
"f32".to_string(),
"f64".to_string(),
],
}
}
}
Expand All @@ -51,4 +124,13 @@ impl HighlightingOptions {
pub fn characters(&self) -> bool {
self.characters
}
pub fn comments(&self) -> bool {
self.comments
}
pub fn primary_keywords(&self) -> &Vec<String> {
&self.primary_keywords
}
pub fn secondary_keywords(&self) -> &Vec<String> {
&self.secondary_keywords
}
}
14 changes: 10 additions & 4 deletions src/highlighting.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
use termion::color;

#[derive(PartialEq)]
#[derive(PartialEq, Clone, Copy)]
pub enum Type {
None,
Number,
Match,
String,
Character
Character,
Comment,
PrimaryKeywords,
SecondaryKeywords,
}

impl Type {
pub fn to_color(&self) -> impl color::Color {
pub fn to_color(self) -> impl color::Color {
match self {
Type::Number => color::Rgb(220, 163, 163),
Type::Match => color::Rgb(38, 139, 210),
Type::String => color::Rgb(255, 255, 255),
Type::Character => color::Rgb(108, 113, 196),
_ => color::Rgb(255, 255, 255)
Type::Comment => color::Rgb(133, 153, 9),
Type::PrimaryKeywords => color::Rgb(181, 137, 0),
Type::SecondaryKeywords => color::Rgb(42, 161, 152),
_ => color::Rgb(255, 255, 255),
}
}
}
Loading

0 comments on commit beda86d

Please sign in to comment.