Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added syntax highlighting for numbers, identifiers and template literals #1047

Merged
merged 3 commits into from
Jan 8, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions boa_cli/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -124,10 +134,12 @@ impl Highlighter for LineHighlighter {

let reg = Regex::new(
r#"(?x)
(?P<identifier>[$A-z_]+[$A-z_0-9]*) |
(?P<identifier>\b[$_\p{ID_Start}][$_\p{ID_Continue}\u{200C}\u{200D}]*\b) |
(?P<string_double_quote>"([^"\\]|\\.)*") |
(?P<string_single_quote>'([^'\\]|\\.)*') |
(?P<op>[+\-/*%~^!&|=<>;:])"#,
(?P<template_literal>`([^`\\]|\\.)*`) |
(?P<op>[+\-/*%~^!&|=<>;:]) |
(?P<number>\b0[bB][01]+|0[xX][0-9a-fA-F]+|[0-9]+\.[0-9]*|[0-9]*\.[0-9]+|[0-9]+\b)"#,
Razican marked this conversation as resolved.
Show resolved Hide resolved
)
.unwrap();

Expand All @@ -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()
}
Expand Down