Highlight single-line comments

This commit is contained in:
Timothy Warren 2021-03-16 11:02:27 -04:00
parent d84da2b0de
commit aa849373ad
3 changed files with 20 additions and 0 deletions

View File

@ -8,6 +8,7 @@ pub struct HighlightingOptions {
numbers: bool, numbers: bool,
strings: bool, strings: bool,
characters: bool, characters: bool,
comments: bool,
} }
impl Default for FileType { impl Default for FileType {
@ -36,6 +37,7 @@ impl FileType {
numbers: true, numbers: true,
strings: true, strings: true,
characters: true, characters: true,
comments: true,
}, },
}; };
} }
@ -56,4 +58,8 @@ impl HighlightingOptions {
pub fn characters(self) -> bool { pub fn characters(self) -> bool {
self.characters self.characters
} }
pub fn comments(self) -> bool {
self.comments
}
} }

View File

@ -7,6 +7,7 @@ pub enum Type {
Match, Match,
String, String,
Character, Character,
Comment,
} }
impl Type { impl Type {
@ -16,6 +17,7 @@ impl Type {
Type::Match => color::Rgb(38, 139, 210), Type::Match => color::Rgb(38, 139, 210),
Type::String => color::Rgb(211, 54, 130), Type::String => color::Rgb(211, 54, 130),
Type::Character => color::Rgb(108, 113, 196), Type::Character => color::Rgb(108, 113, 196),
Type::Comment => color::Rgb(133, 153, 0),
_ => color::Rgb(255, 255, 255), _ => color::Rgb(255, 255, 255),
} }
} }

View File

@ -289,6 +289,18 @@ impl Row {
} }
} }
if opts.comments() && *c == '/' {
if let Some(next_char) = chars.get(index.saturating_add(1)) {
if *next_char == '/' {
for _ in index..chars.len() {
highlighting.push(highlighting::Type::Comment);
}
break;
}
};
}
if opts.numbers() { if opts.numbers() {
if (c.is_ascii_digit() if (c.is_ascii_digit()
&& (prev_is_separator || *previous_highlight == highlighting::Type::Number)) && (prev_is_separator || *previous_highlight == highlighting::Type::Number))