From d2ac4a422ce47cd4856c2a16d1532fcafa4dfe00 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 4 Sep 2019 16:39:56 -0400 Subject: [PATCH] Unfinished keyword highlighting --- src/editor.rs | 133 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 126 insertions(+), 7 deletions(-) diff --git a/src/editor.rs b/src/editor.rs index 2224302..92d3cba 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -35,14 +35,27 @@ bitflags! { pub struct EditorSyntax { file_type: String, file_match: Vec<&'static str>, + keywords1: Vec<&'static str>, + keywords2: Vec<&'static str>, + singleline_comment_start: String, flags: EditorSyntaxFlags, } impl EditorSyntax { - pub fn new(file_type: &str, file_match: Vec<&'static str>, flags: EditorSyntaxFlags) -> Self { + pub fn new( + file_type: &str, + file_match: Vec<&'static str>, + keywords1: Vec<&'static str>, + keywords2: Vec<&'static str>, + single_line_comment_start: &str, + flags: EditorSyntaxFlags, + ) -> Self { EditorSyntax { file_type: String::from(file_type), file_match, + keywords1, + keywords2, + singleline_comment_start: String::from(single_line_comment_start), flags, } } @@ -51,6 +64,9 @@ impl EditorSyntax { #[derive(Copy, Clone, Debug, PartialEq)] pub enum Highlight { Normal, + LineComment, + Keyword1, + Keyword2, String, Number, SearchMatch, @@ -361,6 +377,11 @@ impl Editor { // which can then be unwrapped. let current_syntax = self.syntax.as_ref().unwrap(); + let keywords1 = ¤t_syntax.keywords1; + let keywords2 = ¤t_syntax.keywords2; + + let scs = current_syntax.singleline_comment_start.clone(); + let mut prev_separator = false; let mut in_string = false; let mut str_start = '\0'; @@ -375,6 +396,18 @@ impl Editor { Highlight::Normal }; + // Single line comments + if scs.len() > 0 && !in_string { + let comment = row.render.find(&scs); + if comment.is_some() { + // Pretty simple, highlight from the match to the end of the line + let comment_start = comment.unwrap(); + for x in comment_start..row.render.len() { + row.highlight[x] = Highlight::LineComment; + } + } + } + // Strings if current_syntax .flags @@ -382,13 +415,21 @@ impl Editor { { if in_string { row.highlight[i as usize] = Highlight::String; + // Don't end highlighting for a string on an escaped quote + if c == '\\' && i + 1 < row.render.len() { + row.highlight[i as usize + 1] = Highlight::String; + i += 2; + continue; + } + + // End delimiter for the string if c == str_start { in_string = false; str_start = '\0'; - i += 1; - prev_separator = true; - continue; } + i += 1; + prev_separator = true; + continue; } else { if c == '"' || c == '\'' { in_string = true; @@ -415,6 +456,29 @@ impl Editor { } } + // Keywords + if prev_separator { + for &keyword in keywords1 { + let matches = row.render.match_indices(keyword); + for (start, _) in matches { + let end = start + keyword.len(); + for x in start..end { + row.highlight[x] = Highlight::Keyword1; + } + } + } + + for &keyword in keywords2 { + let matches = row.render.match_indices(keyword); + for (start, _) in matches { + let end = start + keyword.len(); + for x in start..end { + row.highlight[x] = Highlight::Keyword2; + } + } + } + } + prev_separator = is_separator(c); i += 1; } @@ -424,10 +488,13 @@ impl Editor { use Highlight::*; match syntax_type { + Keyword1 => 33, // Yellow + Keyword2 => 32, // Green + LineComment => 36, // Cyan Normal => 37, - Number => 31, // Red - SearchMatch => 34, // Blue - Highlight::String => 35, // Magenta + Number => 31, // Red + SearchMatch => 34, // Blue + String => 35, // Magenta } } @@ -1203,11 +1270,63 @@ fn get_syntax_db() -> Vec { EditorSyntax::new( "c", vec![".c", ".h", ".cpp"], + vec![ + "switch", "if", "while", "for", "break", "continue", "return", "else", "struct", + "union", "typedef", "static", "enum", "class", "case", + ], + vec!["int", "long", "double", "float", "char", "unsigned", "signed", "void"], + "//", EditorSyntaxFlags::HIGHLIGHT_NUMBERS | EditorSyntaxFlags::HIGHLIGHT_STRINGS, ), EditorSyntax::new( "rust", vec![".rs"], + vec![ + "as", "break", "const", "continue", "crate", "else", "enum", "extern", "false", + "fn", "for", "if", "impl", "in", "let", "loop", "match", "mod", "move", "mut", + "pub", "ref", "return", "self", "static", "struct", "super", "trait", "true", + "type", "unsafe", "use", "where", "while", + ], + vec![ + "dyn", + "Self", + "Copy", + "Send", + "Sync", + "Sized", + "Sync", + "Unpin", + "Drop", + "Fn", + "FnMut", + "FnOnce", + "Box", + "ToOwned", + "Clone", + "PartialEq", + "PartialOrd", + "Eq", + "Ord", + "AsRef", + "AsMut", + "Into", + "From", + "Default", + "Iterator", + "Extend", + "IntoIterator", + "DoubleEndedIterator", + "ExactSizeIterator", + "Option", + "Some", + "None", + "Ok", + "Err", + "String", + "ToString", + "Vec", + ], + "//", EditorSyntaxFlags::HIGHLIGHT_NUMBERS | EditorSyntaxFlags::HIGHLIGHT_STRINGS, ), ]