@@ -18,7 +18,7 @@ use crate::syntax_mapping::{MappingTarget, SyntaxMapping};
18#[derive(Debug)]
18#[derive(Debug)]
19pub struct HighlightingAssets {
19pub struct HighlightingAssets {
20 syntax_set_cell: LazyCell<SyntaxSet>,
20 syntax_set_cell: LazyCell<SyntaxSet>,
21− serialized_syntax_set: Option<SerializedSyntaxSet>,
21+ serialized_syntax_set: SerializedSyntaxSet,
22 theme_set: ThemeSet,
22 theme_set: ThemeSet,
23 fallback_theme: Option<&'static str>,
23 fallback_theme: Option<&'static str>,
@@ -47,20 +47,9 @@ const IGNORED_SUFFIXES: [&str; 10] = [
49impl HighlightingAssets {
49impl HighlightingAssets {
51− syntax_set: Option<SyntaxSet>,
52− serialized_syntax_set: Option<SerializedSyntaxSet>,
50+ fn new(serialized_syntax_set: SerializedSyntaxSet, theme_set: ThemeSet) -> Self {
55− assert!(syntax_set.is_some() || serialized_syntax_set.is_some());
57− let syntax_set_cell = LazyCell::new();
58− if let Some(syntax_set) = syntax_set {
59− syntax_set_cell.fill(syntax_set).expect("can never fail");
62 HighlightingAssets {
51 HighlightingAssets {
63− syntax_set_cell,
52+ syntax_set_cell: LazyCell::new(),
64 serialized_syntax_set,
53 serialized_syntax_set,
65 theme_set,
54 theme_set,
66 fallback_theme: None,
55 fallback_theme: None,
@@ -71,127 +60,27 @@ impl HighlightingAssets {
71 "Monokai Extended"
60 "Monokai Extended"
74− #[cfg(feature = "build-assets")]
75− pub fn from_files(source_dir: &Path, include_integrated_assets: bool) -> Result<Self> {
76− let mut theme_set = if include_integrated_assets {
77− get_integrated_themeset()
82− let theme_dir = source_dir.join("themes");
83− if theme_dir.exists() {
84− let res = theme_set.add_from_folder(&theme_dir);
85− if let Err(err) = res {
87− "Failed to load one or more themes from '{}' (reason: '{}')",
88− theme_dir.to_string_lossy(),
94− "No themes were found in '{}', using the default set",
95− theme_dir.to_string_lossy()
99− let mut syntax_set_builder = if !include_integrated_assets {
100− let mut builder = syntect::parsing::SyntaxSetBuilder::new();
101− builder.add_plain_text_syntax();
104− from_binary::<SyntaxSet>(get_serialized_integrated_syntaxset()).into_builder()
107− let syntax_dir = source_dir.join("syntaxes");
108− if syntax_dir.exists() {
109− syntax_set_builder.add_from_folder(syntax_dir, true)?;
112− "No syntaxes were found in '{}', using the default set.",
113− syntax_dir.to_string_lossy()
117− if std::env::var("BAT_PRINT_SYNTAX_DEPENDENCIES").is_ok() {
118− // To trigger this code, run:
119− // BAT_PRINT_SYNTAX_DEPENDENCIES=1 cargo run -- cache --build --source assets --blank --target /tmp
120− crate::syntax_dependencies::print_syntax_dependencies(&syntax_set_builder);
123− let syntax_set = syntax_set_builder.build();
124− let missing_contexts = syntax_set.find_unlinked_contexts();
125− if !missing_contexts.is_empty() {
126− println!("Some referenced contexts could not be found!");
127− for context in missing_contexts {
128− println!("- {}", context);
132− Ok(HighlightingAssets::new(Some(syntax_set), None, theme_set))
135 pub fn from_cache(cache_path: &Path) -> Result<Self> {
63 pub fn from_cache(cache_path: &Path) -> Result<Self> {
136 Ok(HighlightingAssets::new(
64 Ok(HighlightingAssets::new(
138− Some(SerializedSyntaxSet::FromFile(
65+ SerializedSyntaxSet::FromFile(cache_path.join("syntaxes.bin")),
139− cache_path.join("syntaxes.bin"),
141 asset_from_cache(&cache_path.join("themes.bin"), "theme set")?,
66 asset_from_cache(&cache_path.join("themes.bin"), "theme set")?,
145 pub fn from_binary() -> Self {
70 pub fn from_binary() -> Self {
146 HighlightingAssets::new(
71 HighlightingAssets::new(
148− Some(SerializedSyntaxSet::FromBinary(
149− get_serialized_integrated_syntaxset(),
72+ SerializedSyntaxSet::FromBinary(get_serialized_integrated_syntaxset()),
151 get_integrated_themeset(),
73 get_integrated_themeset(),
155− #[cfg(feature = "build-assets")]
156− pub fn save_to_cache(&self, target_dir: &Path, current_version: &str) -> Result<()> {
157− let _ = fs::create_dir_all(target_dir);
159− self.get_theme_set(),
160− &target_dir.join("themes.bin"),
164− self.get_syntax_set()?,
165− &target_dir.join("syntaxes.bin"),
170− "Writing metadata to folder {} ... ",
171− target_dir.to_string_lossy()
173− crate::assets_metadata::AssetsMetadata::new(current_version).save_to_folder(target_dir)?;
179 pub fn set_fallback_theme(&mut self, theme: &'static str) {
77 pub fn set_fallback_theme(&mut self, theme: &'static str) {
180 self.fallback_theme = Some(theme);
78 self.fallback_theme = Some(theme);
183 pub(crate) fn get_syntax_set(&self) -> Result<&SyntaxSet> {
81 pub(crate) fn get_syntax_set(&self) -> Result<&SyntaxSet> {
184− if !self.syntax_set_cell.filled() {
82+ self.syntax_set_cell
185− self.syntax_set_cell.fill(
186− self.serialized_syntax_set
83+ .try_borrow_with(|| self.serialized_syntax_set.deserialize())
188− .expect("a dev forgot to setup serialized_syntax_set, please report to https://github.com/sharkdp/bat/issues")
193− // It is safe to .unwrap() because we just made sure it was .filled()
194− Ok(self.syntax_set_cell.borrow().unwrap())
197 /// Use [Self::get_syntaxes] instead
86 /// Use [Self::get_syntaxes] instead
@@ -393,6 +282,9 @@ impl HighlightingAssets {
285+#[cfg(feature = "build-assets")]
286+pub use crate::build_assets::build_assets as build;
396/// A SyntaxSet in serialized form, i.e. bincoded and flate2 compressed.
288/// A SyntaxSet in serialized form, i.e. bincoded and flate2 compressed.
397/// We keep it in this format since we want to load it lazily.
289/// We keep it in this format since we want to load it lazily.
398#[derive(Debug)]
290#[derive(Debug)]
@@ -413,28 +305,14 @@ impl SerializedSyntaxSet {
416−fn get_serialized_integrated_syntaxset() -> &'static [u8] {
308+pub(crate) fn get_serialized_integrated_syntaxset() -> &'static [u8] {
417 include_bytes!("../assets/syntaxes.bin")
309 include_bytes!("../assets/syntaxes.bin")
420−fn get_integrated_themeset() -> ThemeSet {
312+pub(crate) fn get_integrated_themeset() -> ThemeSet {
421 from_binary(include_bytes!("../assets/themes.bin"))
313 from_binary(include_bytes!("../assets/themes.bin"))
424−#[cfg(feature = "build-assets")]
425−fn asset_to_cache<T: serde::Serialize>(asset: &T, path: &Path, description: &str) -> Result<()> {
426− print!("Writing {} to {} ... ", description, path.to_string_lossy());
427− syntect::dumps::dump_to_file(asset, &path).chain_err(|| {
429− "Could not save {} to {}",
431− path.to_string_lossy()
438fn asset_from_cache<T: serde::de::DeserializeOwned>(path: &Path, description: &str) -> Result<T> {
316fn asset_from_cache<T: serde::de::DeserializeOwned>(path: &Path, description: &str) -> Result<T> {
439 let contents = fs::read(path).chain_err(|| {
317 let contents = fs::read(path).chain_err(|| {