Skip to content

Commit

Permalink
[Fix] auto detect content which should use source syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
mistricky committed Apr 15, 2024
1 parent 7657e1d commit 5a7ae92
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions generator/src/highlight.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

use cosmic_text::{Attrs, Family, Style, Weight};
use syntect::{
easy::HighlightLines,
Expand All @@ -8,11 +10,14 @@ use syntect::{

use crate::components::interface::render_error::RenderError;

type SourceMap = HashMap<&'static str, &'static str>;

pub struct Highlight {
content: String,
code_file_path: String,
extension: Option<String>,
font_family: String,
highlighting_language_source_map: SourceMap,
}

pub type HighlightResult<'a> = Vec<(&'a str, Attrs<'a>)>;
Expand All @@ -29,6 +34,7 @@ impl Highlight {
code_file_path,
extension,
font_family,
highlighting_language_source_map: HashMap::from([("PHP", "<?php")]),
}
}

Expand All @@ -47,6 +53,15 @@ impl Highlight {
))?,
};

if let Some(identifier) = self.highlighting_language_source_map.get(&syntax.name[..]) {
if !self.content.contains(identifier) {
return Ok(syntax_set
.find_syntax_by_name(&format!("{} Source", &syntax.name))
.unwrap_or(syntax)
.to_owned());
}
}

Ok(syntax.to_owned())
}

Expand Down

0 comments on commit 5a7ae92

Please sign in to comment.