Misc tweaks, add Javascript highlighting

This commit is contained in:
Timothy Warren 2019-09-10 16:47:15 -04:00
parent 934791d1c4
commit cec958932c
3 changed files with 73 additions and 24 deletions

View File

@ -133,7 +133,7 @@ pub struct Editor {
/// Keycode mapping enum /// Keycode mapping enum
#[derive(Copy, Clone, Debug, PartialEq)] #[derive(Copy, Clone, Debug, PartialEq)]
pub enum KeyCode<T> { pub enum KeyCode<T = char> {
Enter, Enter,
Escape, Escape,
Backspace, Backspace,
@ -436,7 +436,6 @@ impl Editor {
}; };
let row = &mut rows[index]; let row = &mut rows[index];
let render_len = row.render.len(); let render_len = row.render.len();
// Reset the highlighting of the row // Reset the highlighting of the row
row.highlight = vec![Highlight::Normal; render_len]; row.highlight = vec![Highlight::Normal; render_len];
@ -477,11 +476,7 @@ impl Editor {
let range = get_slice_range(i, scs.len(), render_len); let range = get_slice_range(i, scs.len(), render_len);
if &row.render[range] == scs { if &row.render[range] == scs {
// Pretty simple, highlight from the match to the end of the line // Pretty simple, highlight from the match to the end of the line
highlight_range( highlight_range(&mut row.highlight, i..render_len, Highlight::LineComment);
&mut row.highlight,
i..render_len,
Highlight::LineComment,
);
break; break;
} }
} }
@ -634,14 +629,14 @@ impl Editor {
use Highlight::*; use Highlight::*;
match syntax_type { match syntax_type {
Keyword1 => 33, // Yellow Keyword1 => 33, // Yellow
Keyword2 => 32, // Green Keyword2 => 32, // Green
LineComment => 36, // Cyan LineComment => 36, // Cyan
MultiLineComment => 34, // Blue MultiLineComment => 90, // Bright Black
Normal => 37, // White Normal => 37, // White
Number => 31, // Red Number => 31, // Red
SearchMatch => 7, // Reverse! SearchMatch => 7, // Reverse!
String => 35, // Magenta String => 35, // Magenta
} }
} }
@ -1069,7 +1064,7 @@ impl Editor {
// Move cursor to state position // Move cursor to state position
let y = self.cursor_y - self.row_offset + 1; let y = self.cursor_y - self.row_offset + 1;
let x = self.render_x - self.col_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); self.append_out(&cursor_code);
// Show cursor // Show cursor
@ -1438,7 +1433,7 @@ impl Editor {
fn get_syntax_db() -> Vec<Syntax> { fn get_syntax_db() -> Vec<Syntax> {
vec![ vec![
Syntax::new( Syntax::new(
"c", "C/C++",
vec![".c", ".h", ".cpp"], vec![".c", ".h", ".cpp"],
vec![ vec![
"continue", "typedef", "switch", "return", "static", "while", "break", "struct", "continue", "typedef", "switch", "return", "static", "while", "break", "struct",
@ -1454,7 +1449,7 @@ fn get_syntax_db() -> Vec<Syntax> {
SyntaxFlags::HIGHLIGHT_NUMBERS | SyntaxFlags::HIGHLIGHT_STRINGS, SyntaxFlags::HIGHLIGHT_NUMBERS | SyntaxFlags::HIGHLIGHT_STRINGS,
), ),
Syntax::new( Syntax::new(
"rust", "Rust",
vec![".rs"], vec![".rs"],
vec![ vec![
"continue", "return", "static", "struct", "unsafe", "break", "const", "crate", "continue", "return", "static", "struct", "unsafe", "break", "const", "crate",
@ -1526,6 +1521,52 @@ fn get_syntax_db() -> Vec<Syntax> {
"*/", "*/",
SyntaxFlags::HIGHLIGHT_NUMBERS | SyntaxFlags::HIGHLIGHT_STRINGS, 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 { match res {
Ok(value) => value, Ok(value) => value,
Err(e) => { Err(e) => {
print!("\x1b[2J"); print!("\x1bc");
print!("\x1b[H");
disable_raw_mode(); disable_raw_mode();
panic!("{:?}", e); panic!("{:?}", e);
} }
@ -1623,8 +1663,13 @@ mod tests {
} }
// Check each whitespace character // Check each whitespace character
for ch in " \t\n\x0c".chars() { for ch in " \t\n\r\x0c".chars() {
assert_eq!(is_separator(ch), true, "Character {:#} should be a separator", ch as u8); assert_eq!(
is_separator(ch),
true,
"Character {:#} should be a separator",
ch as u8
);
} }
// Letters are not separators! // Letters are not separators!

View File

@ -4,8 +4,8 @@ extern crate bitflags;
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
mod editor; pub mod editor;
mod terminal_helpers; pub mod terminal_helpers;
use crate::editor::Editor; use crate::editor::Editor;
use crate::terminal_helpers::*; use crate::terminal_helpers::*;

View File

@ -66,6 +66,10 @@ pub fn disable_raw_mode() {
let mutex = Arc::clone(&super::ORIGINAL_TERMIOS); let mutex = Arc::clone(&super::ORIGINAL_TERMIOS);
let termios = mutex.lock().unwrap(); 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(); termios::tcsetattr(STDIN_FILENO, termios::SetArg::TCSAFLUSH, &termios).unwrap();
} }