use r3bl_rs_utils_core::*;
use syntect::parsing::SyntaxSet;
use crate::*;
pub type SyntectStyle = syntect::highlighting::Style;
pub type SyntectStyleStrSpan<'a> = (SyntectStyle, &'a str);
pub type SyntectStyleStrSpanLine<'a> = Vec<SyntectStyleStrSpan<'a>>;
pub fn try_get_syntax_ref<'a>(
syntax_set: &'a SyntaxSet,
file_extension: &'a str,
) -> Option<&'a syntect::parsing::SyntaxReference> {
syntax_set.find_syntax_by_extension(file_extension)
}
pub fn from_syntect_to_tui(
syntect_highlighted_line: SyntectStyleStrSpanLine,
) -> StyleUSSpanLine {
let mut it = StyleUSSpanLine::from(syntect_highlighted_line);
it.iter_mut()
.for_each(|StyleUSSpan { style, text: _ }| style.remove_bg_color());
it
}
mod syntect_support {
use super::*;
impl From<SyntectStyleStrSpanLine<'_>> for StyledTexts {
fn from(value: SyntectStyleStrSpanLine) -> Self { StyledTexts::from(&value) }
}
impl From<&SyntectStyleStrSpanLine<'_>> for StyledTexts {
fn from(syntect_styles: &SyntectStyleStrSpanLine) -> Self {
let mut acc = StyledTexts::default();
for (syntect_style, text) in syntect_styles {
let my_style = Style::from(*syntect_style);
acc += styled_text!(@style: my_style, @text: text.to_string());
}
acc
}
}
impl From<SyntectStyleStrSpanLine<'_>> for StyleUSSpanLine {
fn from(value: SyntectStyleStrSpanLine) -> Self {
pub fn from_vec_styled_str(
vec_styled_str: &SyntectStyleStrSpanLine,
) -> StyleUSSpanLine {
let mut it: StyleUSSpanLine = Default::default();
for (style, text) in vec_styled_str {
let my_style = Style::from(*style);
let unicode_string = US::from(*text);
it.push(StyleUSSpan::new(my_style, unicode_string));
}
it
}
from_vec_styled_str(&value)
}
}
}