#![doc = include_str!("readme.md")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HighlightKind {
Keyword,
String,
Number,
Comment,
Identifier,
}
pub trait Highlighter {
fn highlight(&self, text: &str) -> Vec<(usize, usize, HighlightKind)>;
}
pub struct JHighlighter {
pub use_parser: bool,
}
impl Default for JHighlighter {
fn default() -> Self {
Self { use_parser: false }
}
}
impl JHighlighter {
pub fn new() -> Self {
Self::default()
}
fn highlight_keywords(&self, text: &str) -> Vec<(usize, usize, HighlightKind)> {
let mut highlights = Vec::new();
for op in ["=:", "=."] {
let mut start = 0;
while let Some(pos) = text[start..].find(op) {
highlights.push((start + pos, start + pos + op.len(), HighlightKind::Keyword));
start += pos + op.len();
}
}
highlights
}
}
impl Highlighter for JHighlighter {
fn highlight(&self, text: &str) -> Vec<(usize, usize, HighlightKind)> {
let mut highlights = self.highlight_keywords(text);
highlights.sort_by_key(|h| h.0);
highlights
}
}
#[cfg(feature = "oak-highlight")]
impl oak_highlight::Highlighter for JHighlighter {
fn highlight<'a>(&self, _source: &'a str, _language: &str, _theme: oak_highlight::Theme) -> oak_core::errors::ParseResult<oak_highlight::HighlightResult<'a>> {
Ok(oak_highlight::HighlightResult { segments: Vec::new(), source: std::borrow::Cow::Borrowed(_source) })
}
}