Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding functionality to build SyntectAdapters with custom themes, syntax sets, etc. #239

Merged
merged 3 commits into from
Oct 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/plugins/syntect.rs
Original file line number Diff line number Diff line change
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),
}
}
}