Unfinished keyword highlighting

This commit is contained in:
Timothy Warren 2019-09-04 16:39:56 -04:00
parent 78c612e90f
commit d2ac4a422c

View File

@ -35,14 +35,27 @@ bitflags! {
pub struct EditorSyntax { pub struct EditorSyntax {
file_type: String, file_type: String,
file_match: Vec<&'static str>, file_match: Vec<&'static str>,
keywords1: Vec<&'static str>,
keywords2: Vec<&'static str>,
singleline_comment_start: String,
flags: EditorSyntaxFlags, flags: EditorSyntaxFlags,
} }
impl EditorSyntax { 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 { EditorSyntax {
file_type: String::from(file_type), file_type: String::from(file_type),
file_match, file_match,
keywords1,
keywords2,
singleline_comment_start: String::from(single_line_comment_start),
flags, flags,
} }
} }
@ -51,6 +64,9 @@ impl EditorSyntax {
#[derive(Copy, Clone, Debug, PartialEq)] #[derive(Copy, Clone, Debug, PartialEq)]
pub enum Highlight { pub enum Highlight {
Normal, Normal,
LineComment,
Keyword1,
Keyword2,
String, String,
Number, Number,
SearchMatch, SearchMatch,
@ -361,6 +377,11 @@ impl Editor {
// which can then be unwrapped. // which can then be unwrapped.
let current_syntax = self.syntax.as_ref().unwrap(); let current_syntax = self.syntax.as_ref().unwrap();
let keywords1 = &current_syntax.keywords1;
let keywords2 = &current_syntax.keywords2;
let scs = current_syntax.singleline_comment_start.clone();
let mut prev_separator = false; let mut prev_separator = false;
let mut in_string = false; let mut in_string = false;
let mut str_start = '\0'; let mut str_start = '\0';
@ -375,6 +396,18 @@ impl Editor {
Highlight::Normal 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 // Strings
if current_syntax if current_syntax
.flags .flags
@ -382,13 +415,21 @@ impl Editor {
{ {
if in_string { if in_string {
row.highlight[i as usize] = Highlight::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 { if c == str_start {
in_string = false; in_string = false;
str_start = '\0'; str_start = '\0';
}
i += 1; i += 1;
prev_separator = true; prev_separator = true;
continue; continue;
}
} else { } else {
if c == '"' || c == '\'' { if c == '"' || c == '\'' {
in_string = true; 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); prev_separator = is_separator(c);
i += 1; i += 1;
} }
@ -424,10 +488,13 @@ impl Editor {
use Highlight::*; use Highlight::*;
match syntax_type { match syntax_type {
Keyword1 => 33, // Yellow
Keyword2 => 32, // Green
LineComment => 36, // Cyan
Normal => 37, Normal => 37,
Number => 31, // Red Number => 31, // Red
SearchMatch => 34, // Blue SearchMatch => 34, // Blue
Highlight::String => 35, // Magenta String => 35, // Magenta
} }
} }
@ -1203,11 +1270,63 @@ fn get_syntax_db() -> Vec<EditorSyntax> {
EditorSyntax::new( EditorSyntax::new(
"c", "c",
vec![".c", ".h", ".cpp"], 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, EditorSyntaxFlags::HIGHLIGHT_NUMBERS | EditorSyntaxFlags::HIGHLIGHT_STRINGS,
), ),
EditorSyntax::new( EditorSyntax::new(
"rust", "rust",
vec![".rs"], 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, EditorSyntaxFlags::HIGHLIGHT_NUMBERS | EditorSyntaxFlags::HIGHLIGHT_STRINGS,
), ),
] ]