Highlight strings

This commit is contained in:
Timothy Warren 2019-09-04 15:53:36 -04:00
parent f25252a39e
commit 78c612e90f
1 changed files with 50 additions and 7 deletions

View File

@ -51,6 +51,7 @@ impl EditorSyntax {
#[derive(Copy, Clone, Debug, PartialEq)] #[derive(Copy, Clone, Debug, PartialEq)]
pub enum Highlight { pub enum Highlight {
Normal, Normal,
String,
Number, Number,
SearchMatch, SearchMatch,
} }
@ -361,6 +362,8 @@ impl Editor {
let current_syntax = self.syntax.as_ref().unwrap(); let current_syntax = self.syntax.as_ref().unwrap();
let mut prev_separator = false; let mut prev_separator = false;
let mut in_string = false;
let mut str_start = '\0';
let mut i = 0; let mut i = 0;
let bytes = row.render.clone().into_bytes(); let bytes = row.render.clone().into_bytes();
@ -372,6 +375,32 @@ impl Editor {
Highlight::Normal 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 if current_syntax
.flags .flags
.contains(EditorSyntaxFlags::HIGHLIGHT_NUMBERS) .contains(EditorSyntaxFlags::HIGHLIGHT_NUMBERS)
@ -396,8 +425,9 @@ impl Editor {
match syntax_type { match syntax_type {
Normal => 37, Normal => 37,
Number => 31, // Red Number => 31, // Red
SearchMatch => 34, // Blue SearchMatch => 34, // Blue
Highlight::String => 35, // Magenta
} }
} }
@ -421,6 +451,12 @@ impl Editor {
for ext in file_match { for ext in file_match {
if ext == file_ext { if ext == file_ext {
self.syntax = Some(language.clone()); 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; return;
} }
} }
@ -1163,11 +1199,18 @@ impl Editor {
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
fn get_syntax_db() -> Vec<EditorSyntax> { fn get_syntax_db() -> Vec<EditorSyntax> {
vec![EditorSyntax::new( vec![
"c", EditorSyntax::new(
vec![".c", ".h", ".cpp"], "c",
EditorSyntaxFlags::HIGHLIGHT_NUMBERS, 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 { fn is_separator(c: char) -> bool {