Skip to content

Commit

Permalink
Merge pull request #239 from ArvinSKushwaha/main
Browse files Browse the repository at this point in the history
Adding functionality to build SyntectAdapters with custom themes, syntax sets, etc.
  • Loading branch information
kivikakk committed Oct 2, 2022
2 parents ca70b8f + fe4ca0b commit 939a9d3
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/plugins/syntect.rs
Expand Up @@ -107,3 +107,52 @@ impl SyntaxHighlighterAdapter for SyntectAdapter<'_> {
build_opening_tag("code", attributes)
}
}

#[derive(Debug, Default)]
/// A builder for [`SyntectAdapter`].
///
/// Allows customization of `Theme`, [`ThemeSet`], and [`SyntaxSet`].
pub struct SyntectAdapterBuilder<'a> {
theme: Option<&'a str>,
syntax_set: Option<SyntaxSet>,
theme_set: Option<ThemeSet>,
}

impl<'a> SyntectAdapterBuilder<'a> {
/// Creates a new empty [`SyntectAdapterBuilder`]
pub fn new() -> Self {
Default::default()
}

/// Sets the theme
pub fn theme(mut self, s: &'a str) -> Self {
self.theme.replace(s);
self
}

/// Sets the syntax set
pub fn syntax_set(mut self, s: SyntaxSet) -> Self {
self.syntax_set.replace(s);
self
}

/// Sets the theme set
pub fn theme_set(mut self, s: ThemeSet) -> Self {
self.theme_set.replace(s);
self
}

/// Builds the [`SyntectAdapter`]. Default values:
/// - `theme`: `InspiredGitHub`
/// - `syntax_set`: [`SyntaxSet::load_defaults_newlines()`]
/// - `theme_set`: [`ThemeSet::load_defaults()`]
pub fn build(self) -> SyntectAdapter<'a> {
SyntectAdapter {
theme: self.theme.unwrap_or("InspiredGitHub"),
syntax_set: self
.syntax_set
.unwrap_or_else(SyntaxSet::load_defaults_newlines),
theme_set: self.theme_set.unwrap_or_else(ThemeSet::load_defaults),
}
}
}

0 comments on commit 939a9d3

Please sign in to comment.