From 11a058c8df9729c6d3787cfe796faf21d5d61c98 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 4 Sep 2019 13:15:22 -0400 Subject: [PATCH] Improve number highlighting logic --- src/editor.rs | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/src/editor.rs b/src/editor.rs index 4ff8d31..3f57959 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -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;