diff --git a/src/editor.rs b/src/editor.rs index 6de6809..2224302 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -51,6 +51,7 @@ impl EditorSyntax { #[derive(Copy, Clone, Debug, PartialEq)] pub enum Highlight { Normal, + String, Number, SearchMatch, } @@ -361,6 +362,8 @@ impl Editor { let current_syntax = self.syntax.as_ref().unwrap(); let mut prev_separator = false; + let mut in_string = false; + let mut str_start = '\0'; let mut i = 0; let bytes = row.render.clone().into_bytes(); @@ -372,6 +375,32 @@ impl Editor { Highlight::Normal }; + // Strings + if current_syntax + .flags + .contains(EditorSyntaxFlags::HIGHLIGHT_STRINGS) + { + if in_string { + row.highlight[i as usize] = Highlight::String; + if c == str_start { + in_string = false; + str_start = '\0'; + i += 1; + prev_separator = true; + continue; + } + } else { + if c == '"' || c == '\'' { + in_string = true; + str_start = c; + row.highlight[i as usize] = Highlight::String; + i += 1; + continue; + } + } + } + + // Numbers if current_syntax .flags .contains(EditorSyntaxFlags::HIGHLIGHT_NUMBERS) @@ -396,8 +425,9 @@ impl Editor { match syntax_type { Normal => 37, - Number => 31, // Red - SearchMatch => 34, // Blue + Number => 31, // Red + SearchMatch => 34, // Blue + Highlight::String => 35, // Magenta } } @@ -421,6 +451,12 @@ impl Editor { for ext in file_match { if ext == file_ext { self.syntax = Some(language.clone()); + + // Re-highlight file when the type is determined + for x in 0..self.rows.len() { + self.update_syntax(x); + } + return; } } @@ -1163,11 +1199,18 @@ impl Editor { // ------------------------------------------------------------------------ fn get_syntax_db() -> Vec { - vec![EditorSyntax::new( - "c", - vec![".c", ".h", ".cpp"], - EditorSyntaxFlags::HIGHLIGHT_NUMBERS, - )] + vec![ + EditorSyntax::new( + "c", + vec![".c", ".h", ".cpp"], + EditorSyntaxFlags::HIGHLIGHT_NUMBERS | EditorSyntaxFlags::HIGHLIGHT_STRINGS, + ), + EditorSyntax::new( + "rust", + vec![".rs"], + EditorSyntaxFlags::HIGHLIGHT_NUMBERS | EditorSyntaxFlags::HIGHLIGHT_STRINGS, + ), + ] } fn is_separator(c: char) -> bool {