Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions crates/forge_display/src/markdown.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::OnceLock;

use derive_setters::Setters;
use regex::Regex;
use termimad::crossterm::style::{Attribute, Color};
Expand All @@ -13,7 +15,7 @@ pub struct MarkdownFormat {
skin: MadSkin,
max_consecutive_newlines: usize,
#[setters(skip)]
highlighter: SyntaxHighlighter,
highlighter: OnceLock<SyntaxHighlighter>,
}

impl Default for MarkdownFormat {
Expand All @@ -39,7 +41,7 @@ impl MarkdownFormat {
Self {
skin,
max_consecutive_newlines: 2,
highlighter: SyntaxHighlighter::default(),
highlighter: OnceLock::new(),
}
}

Expand All @@ -55,10 +57,8 @@ impl MarkdownFormat {

// Render with termimad, then restore highlighted code
let rendered = self.skin.term_text(processed.markdown()).to_string();
processed
.restore(&self.highlighter, rendered)
.trim()
.to_string()
let highlighter = self.highlighter.get_or_init(SyntaxHighlighter::default);
processed.restore(highlighter, rendered).trim().to_string()
}

fn strip_excessive_newlines(&self, content: &str) -> String {
Expand Down
22 changes: 15 additions & 7 deletions crates/forge_services/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,25 @@ use forge_app::{EnvironmentInfra, FileReaderInfra, TemplateService};
use forge_domain::Template;
use futures::future;
use handlebars::Handlebars;
use tokio::sync::RwLock;
use tokio::sync::{OnceCell, RwLock};

#[derive(Clone)]
pub struct ForgeTemplateService<F> {
hb: Arc<RwLock<Handlebars<'static>>>,
hb: Arc<OnceCell<RwLock<Handlebars<'static>>>>,
infra: Arc<F>,
}

impl<F: EnvironmentInfra + FileReaderInfra> ForgeTemplateService<F> {
pub fn new(infra: Arc<F>) -> Self {
let hb = forge_app::TemplateEngine::handlebar_instance();
Self { hb: Arc::new(RwLock::new(hb)), infra }
Self { hb: Arc::new(OnceCell::new()), infra }
}

/// Returns a reference to the lazily-initialized Handlebars RwLock,
/// creating the instance on the first call.
async fn get_hb(&self) -> &RwLock<Handlebars<'static>> {
self.hb
.get_or_init(|| async { RwLock::new(forge_app::TemplateEngine::handlebar_instance()) })
.await
}

/// Reads multiple template files in parallel and returns their names and
Expand Down Expand Up @@ -74,7 +81,7 @@ impl<F: EnvironmentInfra + FileReaderInfra> TemplateService for ForgeTemplateSer
let cwd = &self.infra.get_environment().cwd;

// Discover and filter unregistered templates in one pass
let guard = self.hb.read().await;
let guard = self.get_hb().await.read().await;
let path = if path.is_absolute() {
path.to_string_lossy().to_string()
} else {
Expand All @@ -98,7 +105,7 @@ impl<F: EnvironmentInfra + FileReaderInfra> TemplateService for ForgeTemplateSer

// Register all templates if any were found
if !templates.is_empty() {
let mut guard = self.hb.write().await;
let mut guard = self.get_hb().await.write().await;
for (name, content) in templates {
let template = compile_template(&name, &content)?;
guard.register_template(&name, template);
Expand All @@ -114,7 +121,8 @@ impl<F: EnvironmentInfra + FileReaderInfra> TemplateService for ForgeTemplateSer
object: &V,
) -> anyhow::Result<String> {
let rendered = self
.hb
.get_hb()
.await
.read()
.await
.render_template(&template.template, object)?;
Expand Down
Loading