Highlighting single characters

This commit is contained in:
Timothy Warren 2021-03-16 10:57:15 -04:00
parent 6267dc4888
commit d84da2b0de
3 changed files with 35 additions and 0 deletions

View File

@ -7,6 +7,7 @@ pub struct FileType {
pub struct HighlightingOptions {
numbers: bool,
strings: bool,
characters: bool,
}
impl Default for FileType {
@ -34,6 +35,7 @@ impl FileType {
hl_opts: HighlightingOptions {
numbers: true,
strings: true,
characters: true,
},
};
}
@ -50,4 +52,8 @@ impl HighlightingOptions {
pub fn strings(self) -> bool {
self.strings
}
pub fn characters(self) -> bool {
self.characters
}
}

View File

@ -6,6 +6,7 @@ pub enum Type {
Number,
Match,
String,
Character,
}
impl Type {
@ -14,6 +15,7 @@ impl Type {
Type::Number => color::Rgb(220, 163, 163),
Type::Match => color::Rgb(38, 139, 210),
Type::String => color::Rgb(211, 54, 130),
Type::Character => color::Rgb(108, 113, 196),
_ => color::Rgb(255, 255, 255),
}
}

View File

@ -232,6 +232,33 @@ 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;
}
}
}
highlighting.push(highlighting::Type::None);
index += 1;
continue;
}
if opts.strings() {
if in_string {
highlighting.push(highlighting::Type::String);