Highlight strings

This commit is contained in:
Timothy Warren 2021-03-16 10:45:19 -04:00
parent 5b20598fd3
commit 6ee4c092f9
4 changed files with 36 additions and 3 deletions

View File

@ -175,8 +175,8 @@ impl Document {
let current_row = &mut self.rows[at.y]; let current_row = &mut self.rows[at.y];
let mut new_row = current_row.split(at.x); let mut new_row = current_row.split(at.x);
current_row.highlight(None); current_row.highlight(self.file_type.highlighting_options(), None);
new_row.highlight(None); new_row.highlight(self.file_type.highlighting_options(), None);
#[allow(clippy::integer_arithmetic)] #[allow(clippy::integer_arithmetic)]
self.rows.insert(at.y + 1, new_row); self.rows.insert(at.y + 1, new_row);

View File

@ -6,6 +6,7 @@ pub struct FileType {
#[derive(Default, Copy, Clone)] #[derive(Default, Copy, Clone)]
pub struct HighlightingOptions { pub struct HighlightingOptions {
numbers: bool, numbers: bool,
strings: bool,
} }
impl Default for FileType { impl Default for FileType {
@ -30,7 +31,10 @@ impl FileType {
if file_name.ends_with(".rs") { if file_name.ends_with(".rs") {
return Self { return Self {
name: String::from("Rust"), name: String::from("Rust"),
hl_opts: HighlightingOptions { numbers: true }, hl_opts: HighlightingOptions {
numbers: true,
strings: true,
},
}; };
} }
@ -42,4 +46,8 @@ impl HighlightingOptions {
pub fn numbers(self) -> bool { pub fn numbers(self) -> bool {
self.numbers self.numbers
} }
pub fn strings(self) -> bool {
self.strings
}
} }

View File

@ -5,6 +5,7 @@ pub enum Type {
None, None,
Number, Number,
Match, Match,
String,
} }
impl Type { impl Type {
@ -12,6 +13,7 @@ impl Type {
match self { match self {
Type::Number => color::Rgb(220, 163, 163), Type::Number => color::Rgb(220, 163, 163),
Type::Match => color::Rgb(38, 139, 210), Type::Match => color::Rgb(38, 139, 210),
Type::String => color::Rgb(211, 54, 130),
_ => color::Rgb(255, 255, 255), _ => color::Rgb(255, 255, 255),
} }
} }

View File

@ -210,6 +210,7 @@ impl Row {
} }
let mut prev_is_separator = true; let mut prev_is_separator = true;
let mut in_string = false;
let mut index = 0; let mut index = 0;
while let Some(c) = chars.get(index) { while let Some(c) = chars.get(index) {
if let Some(word) = word { if let Some(word) = word {
@ -231,6 +232,28 @@ impl Row {
&highlighting::Type::None &highlighting::Type::None
}; };
if opts.strings() {
if in_string {
highlighting.push(highlighting::Type::String);
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 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))