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)]
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<EditorSyntax> {
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 {