1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-22 06:09:25 -05:00

feat(repl): add color to functions for syntax highlighting (#15434)

This commit is contained in:
sigmaSd 2022-08-10 17:39:16 +01:00 committed by GitHub
parent afc29c28ae
commit 5b2ae25f13
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -304,7 +304,10 @@ impl Highlighter for EditorHelper {
fn highlight<'l>(&self, line: &'l str, _: usize) -> Cow<'l, str> {
let mut out_line = String::from(line);
for item in deno_ast::lex(line, deno_ast::MediaType::TypeScript) {
let mut lexed_items = deno_ast::lex(line, deno_ast::MediaType::TypeScript)
.into_iter()
.peekable();
while let Some(item) = lexed_items.next() {
// Adding color adds more bytes to the string,
// so an offset is needed to stop spans falling out of sync.
let offset = out_line.len() - line.len();
@ -334,7 +337,17 @@ impl Highlighter for EditorHelper {
} else if ident == *"async" || ident == *"of" {
colors::cyan(&line[range]).to_string()
} else {
line[range].to_string()
let next = lexed_items.peek().map(|item| &item.inner);
if matches!(
next,
Some(deno_ast::TokenOrComment::Token(Token::LParen))
) {
// We're looking for something that looks like a function
// We use a simple heuristic: 'ident' followed by 'LParen'
colors::intense_blue(&line[range]).to_string()
} else {
line[range].to_string()
}
}
}
},