Skip to content

Commit

Permalink
add character highlights
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-streltsov committed Feb 23, 2024
1 parent 3124ed4 commit cf40272
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/filetype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ pub struct FileType {
#[derive(Default, Copy, Clone)]
pub struct HighlightingOptions {
numbers: bool,
strings: bool
strings: bool,
characters: bool
}

impl Default for FileType {
Expand All @@ -31,7 +32,8 @@ impl FileType {
name: String::from("Rust"),
hl_opts: HighlightingOptions{
numbers: true,
strings: true
strings: true,
characters: true
}
}
}
Expand All @@ -46,4 +48,7 @@ impl HighlightingOptions {
pub fn strings(&self) -> bool {
self.strings
}
pub fn characters(&self) -> bool {
self.characters
}
}
4 changes: 3 additions & 1 deletion src/highlighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ pub enum Type {
None,
Number,
Match,
String
String,
Character
}

impl Type {
Expand All @@ -14,6 +15,7 @@ impl Type {
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)
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,26 @@ impl Row {
&highlighting::Type::None
};

if opts.characters() && !in_string && *c == '\'' {
prev_is_separator = true;
if let Some(next_char) = chars.get(index.saturating_add(1)) {
let closing_index = if *next_char == '\\' {
index.saturating_add(3)
} else {
index.saturating_add(2)
};
if let Some(closing_char) = chars.get(closing_index) {
if *closing_char == '\'' {
for _ in 0..=closing_index.saturating_sub(index) {
highlighting.push(highlighting::Type::Character);
index += 1;
}
continue;
}
}
}
}

if opts.strings() {
if in_string {
highlighting.push(highlighting::Type::String);
Expand Down

0 comments on commit cf40272

Please sign in to comment.