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

feat(cli/repl): add regex based syntax highlighter #7811

87 changes: 85 additions & 2 deletions cli/repl.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

use crate::colors;
use crate::global_state::GlobalState;
use crate::inspector::InspectorSession;
use deno_core::error::AnyError;
use deno_core::serde_json::json;
use regex::Captures;
use regex::Regex;
use rustyline::error::ReadlineError;
use rustyline::highlight::Highlighter;
use rustyline::validate::MatchingBracketValidator;
use rustyline::validate::ValidationContext;
use rustyline::validate::ValidationResult;
use rustyline::validate::Validator;
use rustyline::Editor;
use rustyline_derive::{Completer, Helper, Highlighter, Hinter};
use rustyline_derive::{Completer, Helper, Hinter};
use std::borrow::Cow;
use std::sync::Arc;
use std::sync::Mutex;

// Provides syntax specific helpers to the editor like validation for multi-line edits.
#[derive(Completer, Helper, Highlighter, Hinter)]
#[derive(Completer, Helper, Hinter)]
struct Helper {
highlighter: SyntaxHighlighter,
validator: MatchingBracketValidator,
}

Expand All @@ -29,6 +35,82 @@ impl Validator for Helper {
}
}

impl Highlighter for Helper {
fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> {
hint.into()
}

fn highlight<'l>(&self, line: &'l str, pos: usize) -> Cow<'l, str> {
self.highlighter.highlight(line, pos)
}

fn highlight_candidate<'c>(
&self,
candidate: &'c str,
_completion: rustyline::CompletionType,
) -> Cow<'c, str> {
self.highlighter.highlight(candidate, 0)
}

fn highlight_char(&self, line: &str, _: usize) -> bool {
!line.is_empty()
}
}

struct SyntaxHighlighter {
lexer: Regex,
}

impl SyntaxHighlighter {
fn new() -> Self {
let lexer = Regex::new(
r#"(?x)
(?P<comment>(?:/\*[\s\S]*?\*/|//[^\n]*)) |
(?P<string>(?:"([^"\\]|\\.)*"|'([^'\\]|\\.)*'|`([^`\\]|\\.)*`)) |
(?P<regexp>/(?:(?:\\/|[^\n/]))*?/) |
caspervonb marked this conversation as resolved.
Show resolved Hide resolved
(?P<number>\d+(?:\.\d+)*(?:e[+-]?\d+)*n*) |
(?P<boolean>\b(?:true|false)\b) |
(?P<null>\b(?:null)\b) |
(?P<undefined>\b(?:undefined)\b) |
(?P<keyword>\b(?:await|async|var|let|for|if|else|in|of|class|const|function|yield|return|with|case|break|switch|import|export|new|while|do|throw|catch)\b) |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, treating it as identifier (no color) for now.

"#,
)
.unwrap();

Self { lexer }
}
}

impl Highlighter for SyntaxHighlighter {
fn highlight<'l>(&self, line: &'l str, _: usize) -> Cow<'l, str> {
self
.lexer
.replace_all(&line.to_string(), |caps: &Captures<'_>| {
if let Some(cap) = caps.name("comment") {
format!("{}", colors::gray(cap.as_str()))
} else if let Some(cap) = caps.name("string") {
format!("{}", colors::green(cap.as_str()))
} else if let Some(cap) = caps.name("regexp") {
format!("{}", colors::red(cap.as_str()))
} else if let Some(cap) = caps.name("number") {
format!("{}", colors::yellow(cap.as_str()))
} else if let Some(cap) = caps.name("boolean") {
format!("{}", colors::yellow(cap.as_str()))
} else if let Some(cap) = caps.name("null") {
format!("{}", colors::yellow(cap.as_str()))
} else if let Some(cap) = caps.name("undefined") {
format!("{}", colors::gray(cap.as_str()))
} else if let Some(cap) = caps.name("keyword") {
format!("{}", colors::cyan(cap.as_str()))
} else {
caps[0].to_string()
}
})
.to_string()
.into()
}
}

pub async fn run(
global_state: &GlobalState,
mut session: Box<InspectorSession>,
Expand All @@ -43,6 +125,7 @@ pub async fn run(
.await?;

let helper = Helper {
highlighter: SyntaxHighlighter::new(),
validator: MatchingBracketValidator::new(),
};

Expand Down