Improve number highlighting logic

This commit is contained in:
Timothy Warren 2019-09-04 13:15:22 -04:00
parent 4092b0226e
commit 11a058c8df

View File

@ -309,14 +309,45 @@ impl Editor {
// Syntax Highlighting
// ------------------------------------------------------------------------
fn is_separator(c: char) -> bool {
let separator_chars = ",.()+-/*=~%<>[];";
for ch in separator_chars.chars() {
if c == ch {
return true;
}
}
c.is_ascii_whitespace() || c == '\0'
}
fn update_syntax(&mut self, index: usize) {
let row = &mut self.rows[index];
row.highlight = vec![Highlight::Normal; row.render.len()];
for (x, ch) in row.render.char_indices() {
if ch.is_ascii_digit() {
row.highlight[x] = Highlight::Number;
let mut prev_separator = false;
let mut i = 0;
let bytes = row.render.clone().into_bytes();
while i < row.render.len() {
let c = bytes[i] as char;
let prev_highlight = if i > 0 {
row.highlight[i - 1]
} else {
Highlight::Normal
};
if (c.is_ascii_digit() && (prev_separator || prev_highlight == Highlight::Number))
|| (c == '.' && prev_highlight == Highlight::Number)
{
row.highlight[i as usize] = Highlight::Number;
i += 1;
prev_separator = false;
continue;
}
prev_separator = Self::is_separator(c);
i += 1;
}
}
@ -325,7 +356,7 @@ impl Editor {
match syntax_type {
Normal => 37,
Number => 31, // Red
Number => 31, // Red
SearchMatch => 34, // Blue
}
}
@ -1039,7 +1070,10 @@ impl Editor {
let saved_coloff = self.col_offset;
let saved_rowoff = self.row_offset;
let query = self.prompt("Search (Use ESC/Arrows/Enter):", Some(&mut Self::find_callback));
let query = self.prompt(
"Search (Use ESC/Arrows/Enter):",
Some(&mut Self::find_callback),
);
if query.is_empty() {
self.cursor_x = saved_cx;