Repository: sharkdp/bat
File: src/assets.rs (Rust)

Code before:
            );
        }

        let mut syntax_set_builder = if !include_integrated_assets {
            let mut builder = SyntaxSetBuilder::new();
            builder.add_plain_text_syntax();
            builder
        } else {
            get_integrated_syntaxset().into_builder()
        };

        let syntax_dir = source_dir.join("syntaxes");
        if syntax_dir.exists() {
            syntax_set_builder.add_from_folder(syntax_dir, true)?;
        } else {
            println!(
                "No syntaxes were found in '{}', using the default set.",
                syntax_dir.to_string_lossy()
            );
        }

        // Disabled for now. Prepration for improving startup speed.
        if false {
            // To trigger this code, run:
            //   cargo run -- cache --build --source assets --blank --target /tmp
            crate::syntax_analysis::print_syntax_dependencies(&syntax_set_builder);
        }

        Ok(HighlightingAssets::new(
            Some(syntax_set_builder.build()),
            None,
            theme_set,
        ))
    }

    pub fn from_cache(cache_path: &Path) -> Result<Self> {
        Ok(HighlightingAssets::new(
            None,
            Some(SerializedSyntaxSet::FromFile(
                cache_path.join("syntaxes.bin"),
            )),
            asset_from_cache(&cache_path.join("themes.bin"), "theme set")?,
        ))
    }

    pub fn from_binary() -> Self {
        HighlightingAssets::new(
            None,
            Some(SerializedSyntaxSet::FromBinary(
                get_serialized_integrated_syntaxset(),
            )),

Review comment (performance):
Do we want to merge this as-is? (with the `if false`?)

Other ideas:
- hide behind a cargo-feature (so we can trigger it with something like `cargo run --features print_syntax_dependencies -- cache --build …`)
- enable it with a environment variable `if std::env::var("BAT_PRINT_SYNTAX_DEPENDENCIES").is_ok() { …`. Contrary to the other two options, this would always include the necessary code in the binary. The env-var check wouldn't hurt at this place though.

Code after:
            );
        }

        let mut syntax_set_builder = if !include_integrated_assets {
            let mut builder = SyntaxSetBuilder::new();
            builder.add_plain_text_syntax();
            builder
        } else {
            get_integrated_syntaxset().into_builder()
        };

        let syntax_dir = source_dir.join("syntaxes");
        if syntax_dir.exists() {
            syntax_set_builder.add_from_folder(syntax_dir, true)?;
        } else {
            println!(
                "No syntaxes were found in '{}', using the default set.",
                syntax_dir.to_string_lossy()
            );
        }

        if std::env::var("BAT_PRINT_SYNTAX_DEPENDENCIES").is_ok() {
            // To trigger this code, run:
            // BAT_PRINT_SYNTAX_DEPENDENCIES=1 cargo run -- cache --build --source assets --blank --target /tmp
            crate::syntax_dependencies::print_syntax_dependencies(&syntax_set_builder);
        }

        let syntax_set = syntax_set_builder.build();
        let missing_contexts = syntax_set.find_unlinked_contexts();
        if !missing_contexts.is_empty() {
            println!("Some referenced contexts could not be found!");
            for context in missing_contexts {
                println!("- {}", context);
            }
        }

        Ok(HighlightingAssets::new(Some(syntax_set), None, theme_set))
    }

    pub fn from_cache(cache_path: &Path) -> Result<Self> {
        Ok(HighlightingAssets::new(
            None,
            Some(SerializedSyntaxSet::FromFile(
                cache_path.join("syntaxes.bin"),
            )),
            asset_from_cache(&cache_path.join("themes.bin"), "theme set")?,
        ))
    }

    pub fn from_binary() -> Self {
        HighlightingAssets::new(