Skip to main content

hjkl_theme/
captures.rs

1use std::collections::HashMap;
2
3use crate::style::StyleSpec;
4
5/// Flat map of tree-sitter capture names to resolved `StyleSpec` values.
6///
7/// Use `resolve` for fallback-chain lookup; `get` for exact-match only.
8#[derive(Clone, Default, Debug)]
9pub struct CaptureMap {
10    flat: HashMap<String, StyleSpec>,
11}
12
13impl CaptureMap {
14    pub(crate) fn from_map(flat: HashMap<String, StyleSpec>) -> Self {
15        Self { flat }
16    }
17
18    /// Exact-match lookup. No fallback.
19    pub fn get(&self, capture: &str) -> Option<&StyleSpec> {
20        self.flat.get(capture)
21    }
22
23    /// Walk the fallback chain: `a.b.c` -> `a.b` -> `a` -> `None`.
24    pub fn resolve(&self, capture: &str) -> Option<&StyleSpec> {
25        let mut key = capture;
26        loop {
27            if let Some(spec) = self.flat.get(key) {
28                return Some(spec);
29            }
30            {
31                let pos = key.rfind('.')?;
32                key = &key[..pos]
33            }
34        }
35    }
36}