Skip to content

Commit

Permalink
feat(repl): add color to functions for syntax highlighting (denoland#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sigmaSd committed Aug 10, 2022
1 parent afc29c2 commit 5b2ae25
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions cli/tools/repl/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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()
}
}
}
},
Expand Down

0 comments on commit 5b2ae25

Please sign in to comment.