#![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 DHighlighter;
impl DHighlighter {
pub fn new() -> Self {
Self
}
}
impl Highlighter for DHighlighter {
fn highlight(&self, text: &str) -> Vec<(usize, usize, HighlightKind)> {
let mut highlights = Vec::new();
let keywords = [
"module",
"import",
"public",
"private",
"protected",
"package",
"export",
"static",
"final",
"abstract",
"override",
"synchronized",
"const",
"immutable",
"inout",
"shared",
"class",
"struct",
"interface",
"union",
"enum",
"function",
"delegate",
"if",
"else",
"while",
"for",
"foreach",
"do",
"switch",
"case",
"default",
"break",
"continue",
"return",
"goto",
"try",
"catch",
"finally",
"throw",
"assert",
"new",
"delete",
"this",
"super",
"void",
"bool",
"byte",
"ubyte",
"short",
"ushort",
"int",
"uint",
"long",
"ulong",
"float",
"double",
"real",
"char",
"wchar",
"dchar",
];
for keyword in keywords {
let mut start = 0;
while let Some(pos) = text[start..].find(keyword) {
let abs_pos = start + pos;
let end_pos = abs_pos + keyword.len();
let before = abs_pos == 0 || !text.chars().nth(abs_pos - 1).unwrap_or(' ').is_alphanumeric();
let after = end_pos >= text.len() || !text.chars().nth(end_pos).unwrap_or(' ').is_alphanumeric();
if before && after {
highlights.push((abs_pos, end_pos, HighlightKind::Keyword))
}
start = abs_pos + 1
}
}
highlights
}
}