Introduce an enum for a separate highlighted src block type.

This commit is contained in:
Tom Alexander
2025-02-22 15:22:35 -05:00
parent b06424cb17
commit c067ca9cc8
2 changed files with 52 additions and 14 deletions

View File

@@ -1,3 +1,4 @@
use std::borrow::Borrow;
use std::borrow::Cow;
use super::macros::intermediate;
@@ -9,7 +10,20 @@ use tree_sitter_highlight::HighlightEvent;
use tree_sitter_highlight::Highlighter;
#[derive(Debug, Clone)]
pub(crate) struct ISrcBlock {
pub(crate) enum ISrcBlock {
Plain(PlainSrcBlock),
Highlighted(HighlightedSrcBlock),
}
#[derive(Debug, Clone)]
pub(crate) struct PlainSrcBlock {
pub(crate) lines: Vec<String>,
pub(crate) language: Option<String>,
pub(crate) post_blank: organic::types::PostBlank,
}
#[derive(Debug, Clone)]
pub(crate) struct HighlightedSrcBlock {
pub(crate) lines: Vec<String>,
pub(crate) language: Option<String>,
pub(crate) post_blank: organic::types::PostBlank,
@@ -66,18 +80,20 @@ intermediate!(
.collect();
let language = original.language.map(str::to_owned);
let lines = match language.as_ref().map(String::as_str) {
match language.as_ref().map(String::as_str) {
Some("nix") => {
// foo
highlight_nix(lines)?
let highlighted = highlight_nix(&lines);
if let Ok(highlighted) = highlighted {
// return the other type
}
}
_ => lines,
_ => {}
};
Ok(ISrcBlock {
Ok(ISrcBlock::Plain(PlainSrcBlock {
lines,
language,
post_blank: original.get_post_blank(),
})
}))
}
);
@@ -91,7 +107,10 @@ fn ascii_whitespace_value(c: char) -> usize {
}
}
fn highlight_nix(lines: Vec<String>) -> Result<Vec<String>, CustomError> {
fn highlight_nix<L>(lines: &[L]) -> Result<Vec<String>, CustomError>
where
L: Borrow<str>,
{
let highlight_names = ["comment", "keyword"];
// Need 1 highlighter per thread
let mut highlighter = Highlighter::new();