use std::rc::Rc;
use blots_core::{environment::Environment, functions::BUILTIN_FUNCTION_NAMES};
use rustyline::{
Context, Helper, completion::Completer, highlight::Highlighter, hint::Hinter,
validate::Validator,
};
use yansi::{Color, Paint};
pub struct BlotsHighlighter {
bindings: Rc<Environment>,
}
impl BlotsHighlighter {
pub fn new(bindings: Rc<Environment>) -> Self {
Self { bindings }
}
fn highlight_line(&self, line: &str) -> String {
let mut highlighted = String::new();
let mut last_end = 0;
for (start, end, highlight_type) in tokens(line) {
if start > last_end {
highlighted.push_str(&line[last_end..start]);
}
let text = &line[start..end];
let colored_text = match highlight_type {
HighlightType::Comment => text.fg(Color::Rgb(128, 128, 128)).to_string(), HighlightType::Keyword => text.fg(Color::Magenta).bold().to_string(), HighlightType::Operator => text.fg(Color::Cyan).to_string(), HighlightType::String => text.fg(Color::Green).to_string(), HighlightType::Number => text.fg(Color::Yellow).to_string(), HighlightType::InputReference => text.fg(Color::Blue).to_string(), };
highlighted.push_str(&colored_text);
last_end = end;
}
if last_end < line.len() {
highlighted.push_str(&line[last_end..]);
}
highlighted
}
pub fn highlight_result(&self, result: &str) -> String {
format!("= {}", self.highlight_line(result))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum HighlightType {
Comment,
Keyword,
Operator,
String,
Number,
InputReference,
}
type Token = (usize, usize, HighlightType);
type TokenAt = fn(&str, usize) -> Option<usize>;
fn tokens(line: &str) -> Vec<Token> {
let mut found: Vec<Token> = Vec::new();
let kinds: [(HighlightType, TokenAt); 6] = [
(HighlightType::Comment, comment_at),
(HighlightType::String, string_at),
(HighlightType::Number, number_at),
(HighlightType::InputReference, input_reference_at),
(HighlightType::Keyword, keyword_at),
(HighlightType::Operator, operator_at),
];
for (kind, token_at) in kinds {
let known = found.len();
let mut at = 0;
while at < line.len() {
let Some(end) = token_at(line, at) else {
at += 1;
continue;
};
let inside = found[..known]
.iter()
.any(|&(start, known_end, _)| at >= start && end <= known_end);
if !inside {
found.push((at, end, kind));
}
at = end;
}
}
found.sort_by_key(|&(start, _, _)| start);
found
}
fn is_word(c: char) -> bool {
c.is_alphanumeric() || c == '_'
}
fn word_before(line: &str, at: usize) -> bool {
line[..at].chars().next_back().is_some_and(is_word)
}
fn word_after(line: &str, at: usize) -> bool {
line[at..].chars().next().is_some_and(is_word)
}
fn digits_end(bytes: &[u8], from: usize) -> usize {
let digits = bytes.get(from..).unwrap_or_default();
from + digits
.iter()
.take_while(|byte| byte.is_ascii_digit())
.count()
}
fn comment_at(line: &str, at: usize) -> Option<usize> {
let rest = line.as_bytes().get(at..)?;
(rest.starts_with(b"//") && !rest.contains(&b'\n')).then_some(line.len())
}
fn string_at(line: &str, at: usize) -> Option<usize> {
let bytes = line.as_bytes();
let quote = bytes[at];
if quote != b'"' && quote != b'\'' {
return None;
}
let length = bytes[at + 1..].iter().position(|byte| *byte == quote)?;
Some(at + 1 + length + 1)
}
fn number_at(line: &str, at: usize) -> Option<usize> {
let bytes = line.as_bytes();
if !bytes[at].is_ascii_digit() || word_before(line, at) {
return None;
}
let exponent_end = |from: usize| {
if !matches!(bytes.get(from), Some(b'e' | b'E')) {
return None;
}
let digits = from + 1 + usize::from(matches!(bytes.get(from + 1), Some(b'+' | b'-')));
let end = digits_end(bytes, digits);
(end > digits).then_some(end)
};
let whole = digits_end(bytes, at);
let fraction = (bytes.get(whole) == Some(&b'.'))
.then(|| digits_end(bytes, whole + 1))
.filter(|end| *end > whole + 1);
[
fraction.and_then(exponent_end),
fraction,
exponent_end(whole),
Some(whole),
]
.into_iter()
.flatten()
.find(|end| !word_after(line, *end))
}
fn input_reference_at(line: &str, at: usize) -> Option<usize> {
let bytes = line.as_bytes();
let starts_name = |byte: &u8| byte.is_ascii_alphabetic() || *byte == b'_';
if bytes[at] != b'#' || !bytes.get(at + 1).is_some_and(starts_name) {
return None;
}
let name = &bytes[at + 2..];
let length = name
.iter()
.take_while(|byte| byte.is_ascii_alphanumeric() || **byte == b'_')
.count();
Some(at + 2 + length)
}
fn word_at(line: &str, at: usize, words: &[&str]) -> Option<usize> {
if !line.as_bytes()[at].is_ascii_lowercase() || word_before(line, at) {
return None;
}
words
.iter()
.filter(|word| line[at..].starts_with(**word))
.map(|word| at + word.len())
.find(|end| !word_after(line, *end))
}
fn keyword_at(line: &str, at: usize) -> Option<usize> {
word_at(line, at, &["if", "then", "else", "output"])
}
fn operator_at(line: &str, at: usize) -> Option<usize> {
let bytes = line.as_bytes();
let next = bytes.get(at + 1).copied();
let length = match bytes[at] {
b'=' if matches!(next, Some(b'>' | b'=')) => 2,
b'!' | b'<' | b'>' if next == Some(b'=') => 2,
b'&' | b'|' | b'?' if next == Some(bytes[at]) => 2,
b'+' | b'-' | b'*' | b'/' | b'%' | b'^' | b'=' | b'!' | b'<' | b'>' | b'.' | b'{'
| b'}' | b'[' | b']' | b'(' | b')' | b',' | b'?' => 1,
_ => return word_at(line, at, &["and", "or", "via", "into", "where"]),
};
Some(at + length)
}
impl Highlighter for BlotsHighlighter {
fn highlight<'l>(&self, line: &'l str, _pos: usize) -> std::borrow::Cow<'l, str> {
self.highlight_line(line).into()
}
fn highlight_char(
&self,
_line: &str,
_pos: usize,
_forced: rustyline::highlight::CmdKind,
) -> bool {
true }
}
impl Hinter for BlotsHighlighter {
type Hint = String;
fn hint(&self, _line: &str, _pos: usize, _ctx: &Context<'_>) -> Option<String> {
None
}
}
impl Completer for BlotsHighlighter {
type Candidate = String;
fn complete(
&self,
line: &str,
pos: usize,
_ctx: &Context<'_>,
) -> rustyline::Result<(usize, Vec<String>)> {
let maybe_last_word = line.split_whitespace().last();
if let Some(last_word) = maybe_last_word
&& let Some(last_word_pos) = line.match_indices(last_word).last()
&& pos == line.len()
&& last_word_pos.0 + last_word.len() == pos
{
let rel_pos = pos - last_word_pos.0;
let mut completions: Vec<String> = self
.bindings
.keys()
.map(|key| key.as_str())
.filter(|key| key.starts_with(last_word))
.map(|key| key[rel_pos..].to_string())
.collect();
let builtin_completions: Vec<String> = BUILTIN_FUNCTION_NAMES
.iter()
.filter(|&name| name.starts_with(last_word))
.map(|&name| name[rel_pos..].to_string())
.collect();
completions.extend(builtin_completions);
return Ok((pos, completions));
}
Ok((pos, vec![]))
}
}
impl Validator for BlotsHighlighter {
fn validate(
&self,
_ctx: &mut rustyline::validate::ValidationContext,
) -> rustyline::Result<rustyline::validate::ValidationResult> {
Ok(rustyline::validate::ValidationResult::Valid(None))
}
}
impl Helper for BlotsHighlighter {}
#[cfg(test)]
mod tests {
use super::HighlightType::{Comment, InputReference, Keyword, Number, Operator, String};
use super::*;
use rustyline::history::MemHistory;
fn highlights(line: &str) -> Vec<(&str, HighlightType)> {
tokens(line)
.into_iter()
.map(|(start, end, kind)| (&line[start..end], kind))
.collect()
}
#[test]
fn finds_each_kind_of_token() {
assert_eq!(
highlights("output total = #price * 1.5e3 // tax"),
[
("output", Keyword),
("=", Operator),
("#price", InputReference),
("*", Operator),
("1.5e3", Number),
("// tax", Comment),
]
);
assert_eq!(
highlights("xs via (x, i) => x ?? 'none' into sum where true"),
[
("via", Operator),
("(", Operator),
(",", Operator),
(")", Operator),
("=>", Operator),
("??", Operator),
("'none'", String),
("into", Operator),
("where", Operator),
]
);
assert_eq!(
highlights("a && b || !c == d => e <= f < g % h ^ i"),
[
("&&", Operator),
("||", Operator),
("!", Operator),
("==", Operator),
("=>", Operator),
("<=", Operator),
("<", Operator),
("%", Operator),
("^", Operator),
]
);
}
#[test]
fn leaves_the_inside_of_strings_and_comments_alone() {
assert_eq!(
highlights("if x >= 10 and y != \"a or b\" then x else y"),
[
("if", Keyword),
(">=", Operator),
("10", Number),
("and", Operator),
("!=", Operator),
("\"a or b\"", String),
("then", Keyword),
("else", Keyword),
]
);
assert_eq!(
highlights("1 // \"a\" + 2"),
[("1", Number), ("// \"a\" + 2", Comment)]
);
assert_eq!(
highlights("'it''s' \"unterminated"),
[("'it'", String), ("'s'", String)]
);
}
#[test]
fn only_takes_words_and_numbers_that_stand_alone() {
assert_eq!(highlights("iffy android x1 1x output_ _if"), []);
assert_eq!(
highlights("é1 + 日if - 2"),
[("+", Operator), ("-", Operator), ("2", Number)]
);
assert_eq!(
highlights("#a #_b1 #1 # x"),
[
("#a", InputReference),
("#_b1", InputReference),
("1", Number)
]
);
}
#[test]
fn falls_back_to_a_shorter_number_before_a_word() {
assert_eq!(
highlights("1.5x 3. 2e+x 4e-2 5E6.7"),
[
("1", Number),
(".", Operator),
("3", Number),
(".", Operator),
("+", Operator),
("4e-2", Number),
("5E6", Number),
(".", Operator),
("7", Number),
]
);
}
#[test]
fn treats_each_dot_of_a_spread_as_an_operator() {
assert_eq!(
highlights("[...xs, {a: 1}]"),
[
("[", Operator),
(".", Operator),
(".", Operator),
(".", Operator),
(",", Operator),
("{", Operator),
("1", Number),
("}", Operator),
("]", Operator),
]
);
}
#[test]
fn only_finds_a_comment_on_the_last_line() {
assert_eq!(
highlights("1 // a\n2 // b"),
[
("1", Number),
("/", Operator),
("/", Operator),
("2", Number),
("// b", Comment),
]
);
}
#[test]
fn completes_prefix_at_word_end_and_ignores_trailing_whitespace() {
let bindings = Rc::new(Environment::new());
let highlighter = BlotsHighlighter::new(bindings);
let history = MemHistory::new();
let ctx = Context::new(&history);
let (pos, completions) = highlighter.complete("sum ", 6, &ctx).unwrap();
assert_eq!(pos, 6);
assert!(completions.is_empty());
let (pos, completions) = highlighter.complete("su", 2, &ctx).unwrap();
assert_eq!(pos, 2);
assert!(completions.iter().any(|c| c == "m"));
}
}