From ba84f526de30978663a33c5accbf0b341853065e Mon Sep 17 00:00:00 2001 From: Iban Eguia Date: Fri, 8 Jan 2021 19:54:09 +0100 Subject: [PATCH] Added syntax highlighting for numbers, identifiers and template literals (#1047) * Added syntax highlighting for numbers, identifiers and template literals * Update boa_cli/src/helper.rs Co-authored-by: Jevan Chan * Update boa_cli/src/helper.rs Co-authored-by: Jevan Chan Co-authored-by: Jevan Chan --- boa_cli/src/helper.rs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/boa_cli/src/helper.rs b/boa_cli/src/helper.rs index 8806e33f90..0049f4a95e 100644 --- a/boa_cli/src/helper.rs +++ b/boa_cli/src/helper.rs @@ -23,6 +23,16 @@ const UNDEFINED_COLOR: Color = Color::TrueColor { g: 100, b: 100, }; +const NUMBER_COLOR: Color = Color::TrueColor { + r: 26, + g: 214, + b: 175, +}; +const IDENTIFIER_COLOR: Color = Color::TrueColor { + r: 26, + g: 160, + b: 214, +}; #[derive(Completer, Helper, Hinter)] pub(crate) struct RLHelper { @@ -124,10 +134,12 @@ impl Highlighter for LineHighlighter { let reg = Regex::new( r#"(?x) - (?P[$A-z_]+[$A-z_0-9]*) | + (?P\b[$_\p{ID_Start}][$_\p{ID_Continue}\u{200C}\u{200D}]*\b) | (?P"([^"\\]|\\.)*") | (?P'([^'\\]|\\.)*') | - (?P[+\-/*%~^!&|=<>;:])"#, + (?P`([^`\\]|\\.)*`) | + (?P[+\-/*%~^!&|=<>;:]) | + (?P0[bB][01](_?[01])*n?|0[oO][0-7](_?[0-7])*n?|0[xX][0-9a-fA-F](_?[0-9a-fA-F])*n?|(([0-9](_?[0-9])*\.([0-9](_?[0-9])*)?)|(([0-9](_?[0-9])*)?\.[0-9](_?[0-9])*)|([0-9](_?[0-9])*))([eE][+-]?[0-9](_?[0-9])*)?n?)"#, ) .unwrap(); @@ -142,14 +154,18 @@ impl Highlighter for LineHighlighter { identifier if KEYWORDS.contains(identifier) => { cap.as_str().color(KEYWORD_COLOR).bold().to_string() } - _ => cap.as_str().to_string(), + _ => cap.as_str().color(IDENTIFIER_COLOR).to_string(), } } else if let Some(cap) = caps.name("string_double_quote") { cap.as_str().color(STRING_COLOR).to_string() } else if let Some(cap) = caps.name("string_single_quote") { cap.as_str().color(STRING_COLOR).to_string() + } else if let Some(cap) = caps.name("template_literal") { + cap.as_str().color(STRING_COLOR).to_string() } else if let Some(cap) = caps.name("op") { cap.as_str().color(OPERATOR_COLOR).to_string() + } else if let Some(cap) = caps.name("number") { + cap.as_str().color(NUMBER_COLOR).to_string() } else { caps[0].to_string() }