@@ -18,7 +18,7 @@ use crate::syntax_mapping::{MappingTarget, SyntaxMapping};
1818#[derive(Debug)]
1919pub struct HighlightingAssets {
2020 syntax_set_cell: LazyCell<SyntaxSet>,
21− serialized_syntax_set: Option<SerializedSyntaxSet>,
21+ serialized_syntax_set: SerializedSyntaxSet,
2222 theme_set: ThemeSet,
2323 fallback_theme: Option<&'static str>,
2424}
@@ -47,20 +47,9 @@ const IGNORED_SUFFIXES: [&str; 10] = [
4747];
4848
4949impl HighlightingAssets {
50− fn new(
51− syntax_set: Option<SyntaxSet>,
52− serialized_syntax_set: Option<SerializedSyntaxSet>,
53− theme_set: ThemeSet,
54− ) -> Self {
55− assert!(syntax_set.is_some() || serialized_syntax_set.is_some());
56−
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");
60− }
61−
50+ fn new(serialized_syntax_set: SerializedSyntaxSet, theme_set: ThemeSet) -> Self {
6251 HighlightingAssets {
63− syntax_set_cell,
52+ syntax_set_cell: LazyCell::new(),
6453 serialized_syntax_set,
6554 theme_set,
6655 fallback_theme: None,
@@ -71,127 +60,27 @@ impl HighlightingAssets {
7160 "Monokai Extended"
7261 }
7362
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()
78− } else {
79− ThemeSet::new()
80− };
81−
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 {
86− println!(
87− "Failed to load one or more themes from '{}' (reason: '{}')",
88− theme_dir.to_string_lossy(),
89− err,
90− );
91− }
92− } else {
93− println!(
94− "No themes were found in '{}', using the default set",
95− theme_dir.to_string_lossy()
96− );
97− }
98−
99− let mut syntax_set_builder = if !include_integrated_assets {
100− let mut builder = syntect::parsing::SyntaxSetBuilder::new();
101− builder.add_plain_text_syntax();
102− builder
103− } else {
104− from_binary::<SyntaxSet>(get_serialized_integrated_syntaxset()).into_builder()
105− };
106−
107− let syntax_dir = source_dir.join("syntaxes");
108− if syntax_dir.exists() {
109− syntax_set_builder.add_from_folder(syntax_dir, true)?;
110− } else {
111− println!(
112− "No syntaxes were found in '{}', using the default set.",
113− syntax_dir.to_string_lossy()
114− );
115− }
116−
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);
121− }
122−
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);
129− }
130− }
131−
132− Ok(HighlightingAssets::new(Some(syntax_set), None, theme_set))
133− }
134−
13563 pub fn from_cache(cache_path: &Path) -> Result<Self> {
13664 Ok(HighlightingAssets::new(
137− None,
138− Some(SerializedSyntaxSet::FromFile(
139− cache_path.join("syntaxes.bin"),
140− )),
65+ SerializedSyntaxSet::FromFile(cache_path.join("syntaxes.bin")),
14166 asset_from_cache(&cache_path.join("themes.bin"), "theme set")?,
14267 ))
14368 }
14469
14570 pub fn from_binary() -> Self {
14671 HighlightingAssets::new(
147− None,
148− Some(SerializedSyntaxSet::FromBinary(
149− get_serialized_integrated_syntaxset(),
150− )),
72+ SerializedSyntaxSet::FromBinary(get_serialized_integrated_syntaxset()),
15173 get_integrated_themeset(),
15274 )
15375 }
15476
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);
158− asset_to_cache(
159− self.get_theme_set(),
160− &target_dir.join("themes.bin"),
161− "theme set",
162− )?;
163− asset_to_cache(
164− self.get_syntax_set()?,
165− &target_dir.join("syntaxes.bin"),
166− "syntax set",
167− )?;
168−
169− print!(
170− "Writing metadata to folder {} ... ",
171− target_dir.to_string_lossy()
172− );
173− crate::assets_metadata::AssetsMetadata::new(current_version).save_to_folder(target_dir)?;
174− println!("okay");
175−
176− Ok(())
177− }
178−
17977 pub fn set_fallback_theme(&mut self, theme: &'static str) {
18078 self.fallback_theme = Some(theme);
18179 }
18280
18381 pub(crate) fn get_syntax_set(&self) -> Result<&SyntaxSet> {
184− if !self.syntax_set_cell.filled() {
185− self.syntax_set_cell.fill(
186− self.serialized_syntax_set
187− .as_ref()
188− .expect("a dev forgot to setup serialized_syntax_set, please report to https://github.com/sharkdp/bat/issues")
189− .deserialize()?
190− ).unwrap();
191− }
192−
193− // It is safe to .unwrap() because we just made sure it was .filled()
194− Ok(self.syntax_set_cell.borrow().unwrap())
82+ self.syntax_set_cell
83+ .try_borrow_with(|| self.serialized_syntax_set.deserialize())
19584 }
19685
19786 /// Use [Self::get_syntaxes] instead
@@ -393,6 +282,9 @@ impl HighlightingAssets {
393282 }
394283}
395284
285+#[cfg(feature = "build-assets")]
286+pub use crate::build_assets::build_assets as build;
287+
396288/// A SyntaxSet in serialized form, i.e. bincoded and flate2 compressed.
397289/// We keep it in this format since we want to load it lazily.
398290#[derive(Debug)]
@@ -413,28 +305,14 @@ impl SerializedSyntaxSet {
413305 }
414306}
415307
416−fn get_serialized_integrated_syntaxset() -> &'static [u8] {
308+pub(crate) fn get_serialized_integrated_syntaxset() -> &'static [u8] {
417309 include_bytes!("../assets/syntaxes.bin")
418310}
419311
420−fn get_integrated_themeset() -> ThemeSet {
312+pub(crate) fn get_integrated_themeset() -> ThemeSet {
421313 from_binary(include_bytes!("../assets/themes.bin"))
422314}
423315
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(|| {
428− format!(
429− "Could not save {} to {}",
430− description,
431− path.to_string_lossy()
432− )
433− })?;
434− println!("okay");
435− Ok(())
436−}
437−
438316fn asset_from_cache<T: serde::de::DeserializeOwned>(path: &Path, description: &str) -> Result<T> {
439317 let contents = fs::read(path).chain_err(|| {
440318 format!(