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
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum KeyCode<T> {
pub enum KeyCode<T = char> {
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<Syntax> {
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<Syntax> {
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<Syntax> {
"*/",
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!

View File

@ -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::*;

View File

@ -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();
}