From cec958932c48ff413fb027995cbba55dec88287f Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 10 Sep 2019 16:47:15 -0400 Subject: [PATCH] Misc tweaks, add Javascript highlighting --- src/editor.rs | 89 +++++++++++++++++++++++++++++++---------- src/main.rs | 4 +- src/terminal_helpers.rs | 4 ++ 3 files changed, 73 insertions(+), 24 deletions(-) diff --git a/src/editor.rs b/src/editor.rs index f109b2d..53c4a5d 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -133,7 +133,7 @@ pub struct Editor { /// Keycode mapping enum #[derive(Copy, Clone, Debug, PartialEq)] -pub enum KeyCode { +pub enum KeyCode { Enter, Escape, Backspace, @@ -436,7 +436,6 @@ impl Editor { }; let row = &mut rows[index]; let render_len = row.render.len(); - // Reset the highlighting of the row row.highlight = vec![Highlight::Normal; render_len]; @@ -477,11 +476,7 @@ impl Editor { let range = get_slice_range(i, scs.len(), render_len); if &row.render[range] == scs { // Pretty simple, highlight from the match to the end of the line - highlight_range( - &mut row.highlight, - i..render_len, - Highlight::LineComment, - ); + highlight_range(&mut row.highlight, i..render_len, Highlight::LineComment); break; } } @@ -634,14 +629,14 @@ impl Editor { use Highlight::*; match syntax_type { - Keyword1 => 33, // Yellow - Keyword2 => 32, // Green - LineComment => 36, // Cyan - MultiLineComment => 34, // Blue - Normal => 37, // White - Number => 31, // Red - SearchMatch => 7, // Reverse! - String => 35, // Magenta + Keyword1 => 33, // Yellow + Keyword2 => 32, // Green + LineComment => 36, // Cyan + MultiLineComment => 90, // Bright Black + Normal => 37, // White + Number => 31, // Red + SearchMatch => 7, // Reverse! + String => 35, // Magenta } } @@ -1069,7 +1064,7 @@ impl Editor { // Move cursor to state position let y = self.cursor_y - self.row_offset + 1; let x = self.render_x - self.col_offset + 1; - let cursor_code = format!("\x1b[{y};{x}H", y = y, x = x); + let cursor_code = format!("\x1b[{y};{x}f", y = y, x = x); self.append_out(&cursor_code); // Show cursor @@ -1438,7 +1433,7 @@ impl Editor { fn get_syntax_db() -> Vec { vec![ Syntax::new( - "c", + "C/C++", vec![".c", ".h", ".cpp"], vec![ "continue", "typedef", "switch", "return", "static", "while", "break", "struct", @@ -1454,7 +1449,7 @@ fn get_syntax_db() -> Vec { SyntaxFlags::HIGHLIGHT_NUMBERS | SyntaxFlags::HIGHLIGHT_STRINGS, ), Syntax::new( - "rust", + "Rust", vec![".rs"], vec![ "continue", "return", "static", "struct", "unsafe", "break", "const", "crate", @@ -1526,6 +1521,52 @@ fn get_syntax_db() -> Vec { "*/", SyntaxFlags::HIGHLIGHT_NUMBERS | SyntaxFlags::HIGHLIGHT_STRINGS, ), + Syntax::new( + "JavaScript", + vec![".js", ".mjs", ".jsx", ".ts", ".tsx"], + vec![ + "instanceof", + "continue", + "debugger", + "function", + "default", + "extends", + "finally", + "delete", + "export", + "import", + "return", + "switch", + "typeof", + "break", + "catch", + "class", + "const", + "super", + "throw", + "while", + "yield", + "case", + "else", + "this", + "void", + "with", + "from", + "for", + "new", + "try", + "var", + "do", + "if", + "in", + "as", + ], + vec!["=>", "Number", "String", "Object", "Math", "JSON", "Boolean"], + "//", + "/*", + "*/", + SyntaxFlags::HIGHLIGHT_NUMBERS | SyntaxFlags::HIGHLIGHT_STRINGS, + ), ] } @@ -1592,8 +1633,7 @@ where match res { Ok(value) => value, Err(e) => { - print!("\x1b[2J"); - print!("\x1b[H"); + print!("\x1bc"); disable_raw_mode(); panic!("{:?}", e); } @@ -1623,8 +1663,13 @@ mod tests { } // Check each whitespace character - for ch in " \t\n\x0c".chars() { - assert_eq!(is_separator(ch), true, "Character {:#} should be a separator", ch as u8); + for ch in " \t\n\r\x0c".chars() { + assert_eq!( + is_separator(ch), + true, + "Character {:#} should be a separator", + ch as u8 + ); } // Letters are not separators! diff --git a/src/main.rs b/src/main.rs index 1a0746e..acce25a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,8 +4,8 @@ extern crate bitflags; #[macro_use] extern crate lazy_static; -mod editor; -mod terminal_helpers; +pub mod editor; +pub mod terminal_helpers; use crate::editor::Editor; use crate::terminal_helpers::*; diff --git a/src/terminal_helpers.rs b/src/terminal_helpers.rs index da3f3c8..0c0be98 100644 --- a/src/terminal_helpers.rs +++ b/src/terminal_helpers.rs @@ -66,6 +66,10 @@ pub fn disable_raw_mode() { let mutex = Arc::clone(&super::ORIGINAL_TERMIOS); let termios = mutex.lock().unwrap(); + // First attempt to reset terminal settings via a terminal code + print!("\x1bc"); + + // Restore previous terminal settings termios::tcsetattr(STDIN_FILENO, termios::SetArg::TCSAFLUSH, &termios).unwrap(); }