Skip to content

Commit

Permalink
Add keyword and regex highlighting to themes
Browse files Browse the repository at this point in the history
- Update highlighter to support keywords and regexes
- Enhance theme mapping with keyword and regex parsing
- Modify theme structs to include keywords and regex configurations
  • Loading branch information
bensadeh committed Oct 12, 2024
1 parent 5ccfd5d commit 381856a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/highlighter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ pub fn get_highlighter(highlighter_groups: HighlighterGroups, theme: Theme) -> R
add_builtin_keywords(&mut builder);
}

builder.with_keyword_highlighter(theme.keywords);

if !theme.regexes.is_empty() {
for regex in theme.regexes {
builder.with_regex_highlighter(regex);
}
}

if highlighter_groups.numbers {
builder.with_number_highlighter(theme.numbers);
}
Expand Down
24 changes: 24 additions & 0 deletions src/theme/mappers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ use crate::theme::*;
impl From<TomlTheme> for Theme {
fn from(toml: TomlTheme) -> Self {
Theme {
keywords: toml.keywords.map_or_else(Vec::new, |keywords| {
keywords.into_iter().map(KeywordConfig::from).collect()
}),
regexes: toml
.regexes
.map_or_else(Vec::new, |regexes| regexes.into_iter().map(RegexConfig::from).collect()),
numbers: toml.numbers.map_or_else(NumberConfig::default, NumberConfig::from),
uuids: toml.uuids.map_or_else(UuidConfig::default, UuidConfig::from),
quotes: toml.quotes.map_or_else(QuotesConfig::default, QuotesConfig::from),
Expand All @@ -22,6 +28,24 @@ impl From<TomlTheme> for Theme {
}
}

impl From<KeywordToml> for KeywordConfig {
fn from(keyword_toml: KeywordToml) -> Self {
KeywordConfig {
words: keyword_toml.keyword,
style: keyword_toml.style,
}
}
}

impl From<RegexToml> for RegexConfig {
fn from(regex_toml: RegexToml) -> Self {
RegexConfig {
regex: regex_toml.regex,
style: regex_toml.style,
}
}
}

impl From<NumberToml> for NumberConfig {
fn from(number_toml: NumberToml) -> Self {
let default_config = NumberConfig::default();
Expand Down
16 changes: 16 additions & 0 deletions src/theme/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ mod mappers;
pub mod reader;

pub struct Theme {
pub keywords: Vec<KeywordConfig>,
pub regexes: Vec<RegexConfig>,
pub numbers: NumberConfig,
pub uuids: UuidConfig,
pub quotes: QuotesConfig,
Expand All @@ -20,6 +22,8 @@ pub struct Theme {

#[derive(Deserialize, Debug, Default)]
pub struct TomlTheme {
pub keywords: Option<Vec<KeywordToml>>,
pub regexes: Option<Vec<RegexToml>>,
pub numbers: Option<NumberToml>,
pub uuids: Option<UuidToml>,
pub quotes: Option<QuotesToml>,
Expand All @@ -32,6 +36,18 @@ pub struct TomlTheme {
pub key_value_pairs: Option<KeyValueToml>,
}

#[derive(Deserialize, Debug)]
pub struct KeywordToml {
pub keyword: Vec<String>,
pub style: Style,
}

#[derive(Deserialize, Debug)]
pub struct RegexToml {
pub regex: String,
pub style: Style,
}

#[derive(Deserialize, Debug)]
pub struct NumberToml {
pub number: Option<Style>,
Expand Down

0 comments on commit 381856a

Please sign in to comment.