Skip to main content

lattice_snippet/
builtins.rs

1//! Built-in snippet packs embedded in the binary.
2//!
3//! Bundled friendly-snippets-compatible JSON packs (the same
4//! TextMate format [`crate::load::load_pack`] reads) so a fresh
5//! editor has a useful starter set without the user installing
6//! anything. They load at boot into the shared
7//! [`SnippetRegistry`]; user packs in `~/.config/lattice/snippets`
8//! overlay them via `:reload-snippets`.
9//!
10//! Adding a language: drop `<language>.json` under
11//! `crates/lattice-snippet/snippets/` and add an `include_str!`
12//! line to [`builtin_packs`]. `_global.json` maps to the `"*"`
13//! all-languages bucket.
14
15use crate::load::load_pack_from_str;
16use crate::registry::SnippetRegistry;
17
18/// `(language, pack-json)` for every embedded pack. `"*"` is the
19/// all-languages bucket (the snippet source walks it alongside the
20/// active language).
21pub fn builtin_packs() -> &'static [(&'static str, &'static str)] {
22    &[
23        ("rust", include_str!("../snippets/rust.json")),
24        ("*", include_str!("../snippets/_global.json")),
25    ]
26}
27
28/// Build a fresh registry holding every built-in pack.
29pub fn load_builtins() -> SnippetRegistry {
30    let mut registry = SnippetRegistry::new();
31    load_builtins_into(&mut registry);
32    registry
33}
34
35/// Overlay the built-in packs onto an existing registry. Used by
36/// `:reload-snippets` to seed the rebuild before user packs (so
37/// user packs augment / override the built-ins rather than the
38/// reload wiping them).
39///
40/// A pack that fails to parse is skipped: it cannot happen in a
41/// shipped binary — `builtin_packs_all_parse` guards every pack at
42/// test time — but the runtime path stays defensive rather than
43/// panicking on the hot-adjacent boot path.
44pub fn load_builtins_into(registry: &mut SnippetRegistry) {
45    for (language, json) in builtin_packs() {
46        if let Ok(snippets) = load_pack_from_str(json) {
47            for snippet in snippets {
48                registry.insert(language, snippet);
49            }
50        }
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    /// Every embedded pack must parse — a malformed bundled pack
59    /// is a build-time bug, caught here instead of silently
60    /// dropping snippets in the user's editor.
61    #[test]
62    fn builtin_packs_all_parse() {
63        for (language, json) in builtin_packs() {
64            load_pack_from_str(json)
65                .unwrap_or_else(|e| panic!("builtin pack `{language}` must parse: {e}"));
66        }
67    }
68
69    #[test]
70    fn load_builtins_indexes_rust_and_global() {
71        let reg = load_builtins();
72        assert!(!reg.is_empty());
73        // A known rust prefix resolves.
74        assert!(
75            !reg.lookup("rust", "for").is_empty(),
76            "rust `for` snippet present"
77        );
78        // A known global prefix resolves under the "*" bucket.
79        assert!(
80            !reg.lookup("*", "todo").is_empty(),
81            "global `todo` snippet present"
82        );
83    }
84}