use ratatui::style::{Modifier, Style as RStyle};
use crate::theme::Theme;
enum Role {
Keyword,
String,
Comment,
Function,
Type,
Constant,
Tag,
Namespace,
Invalid,
MarkupBold,
MarkupItalic,
MarkupStrike,
}
const CAPTURES: &[(&str, Role)] = &[
("keyword", Role::Keyword),
("conditional", Role::Keyword), ("repeat", Role::Keyword), ("include", Role::Keyword), ("exception", Role::Keyword), ("label", Role::Keyword), ("variable.builtin", Role::Keyword), ("string", Role::String),
("string.escape", Role::String),
("string.regexp", Role::String),
("string.special", Role::String),
("comment", Role::Comment),
("constant", Role::Constant), ("number", Role::Constant),
("boolean", Role::Constant),
("constructor", Role::Function), ("function", Role::Function),
("attribute", Role::Function), ("type", Role::Type), ("tag", Role::Tag),
("module", Role::Namespace),
("namespace", Role::Namespace), ("error", Role::Invalid), ("invalid", Role::Invalid), ("text.title", Role::Tag),
("text.literal", Role::String),
("text.emphasis", Role::MarkupItalic),
("text.strong", Role::MarkupBold),
("text.uri", Role::String),
("punctuation.special", Role::Keyword), ("markup.heading", Role::Tag),
("markup.raw", Role::String), ("markup.bold", Role::MarkupBold),
("markup.italic", Role::MarkupItalic),
("markup.strikethrough", Role::MarkupStrike),
("markup.list", Role::Keyword),
("markup.quote", Role::Comment),
("markup.link.url", Role::String),
];
pub(super) fn capture_names() -> Vec<&'static str> {
CAPTURES.iter().map(|(name, _)| *name).collect()
}
pub(super) struct StyleTable {
styles: Vec<RStyle>,
default: RStyle,
}
impl StyleTable {
pub(super) fn new(theme: &Theme) -> Self {
let syntax = &theme.syntax;
let fg = |color| RStyle::new().fg(color);
let plain = fg(theme.semantic.text);
StyleTable {
default: plain,
styles: CAPTURES
.iter()
.map(|(_, role)| match role {
Role::Keyword => fg(syntax.keyword),
Role::String => fg(syntax.string),
Role::Comment => fg(syntax.comment),
Role::Function => fg(syntax.function),
Role::Type => fg(syntax.type_),
Role::Constant => fg(syntax.constant),
Role::Tag => fg(syntax.tag),
Role::Namespace => fg(syntax.namespace),
Role::Invalid => fg(syntax.invalid),
Role::MarkupBold => fg(syntax.constant).add_modifier(Modifier::BOLD),
Role::MarkupItalic => fg(syntax.keyword).add_modifier(Modifier::ITALIC),
Role::MarkupStrike => plain.add_modifier(Modifier::CROSSED_OUT),
})
.collect(),
}
}
pub(super) fn style(&self, slot: Option<usize>) -> RStyle {
match slot {
Some(i) => self.styles.get(i).copied().unwrap_or(self.default),
None => self.default,
}
}
pub(super) fn default_style(&self) -> RStyle {
self.default
}
}