Skip to content

Commit

Permalink
add string highlights
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-streltsov committed Feb 23, 2024
1 parent 86a2c9b commit 3124ed4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
11 changes: 9 additions & 2 deletions src/filetype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ pub struct FileType {

#[derive(Default, Copy, Clone)]
pub struct HighlightingOptions {
numbers: bool
numbers: bool,
strings: bool
}

impl Default for FileType {
Expand All @@ -28,7 +29,10 @@ impl FileType {
if file_name.ends_with(".rs") {
return Self {
name: String::from("Rust"),
hl_opts: HighlightingOptions{ numbers: true }
hl_opts: HighlightingOptions{
numbers: true,
strings: true
}
}
}
Self::default()
Expand All @@ -39,4 +43,7 @@ impl HighlightingOptions {
pub fn numbers(&self) -> bool {
self.numbers
}
pub fn strings(&self) -> bool {
self.strings
}
}
4 changes: 3 additions & 1 deletion src/highlighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ use termion::color;
pub enum Type {
None,
Number,
Match
Match,
String
}

impl Type {
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),
_ => color::Rgb(255, 255, 255)
}
}
Expand Down
31 changes: 29 additions & 2 deletions src/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ impl Row {
}

let mut prev_is_separator = true;
let mut in_string = false;
let mut index = 0;
while let Some(c) = chars.get(index) {
if let Some(word) = word {
Expand All @@ -187,11 +188,37 @@ impl Row {
} else {
&highlighting::Type::None
};

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

if *c == '\\' && index < self.len().saturating_sub(1) {
highlighting.push(highlighting::Type::String);
index += 2;
continue;
}
if *c == '"' {
in_string = false;
prev_is_separator = true;
} else {
prev_is_separator = false;
}
index += 1;
continue;
} else if prev_is_separator && *c == '"' {
highlighting.push(highlighting::Type::String);
in_string = true;
prev_is_separator = true;
index += 1;
continue;
}
}

if opts.numbers() {
if (c.is_ascii_digit()
&& (prev_is_separator || previous_highlight == &highlighting::Type::Number))
|| (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);
} else {
Expand Down

0 comments on commit 3124ed4

Please sign in to comment.