Skip to main content

vvv_engine/capabilities/
file.rs

1//! File preview with syntax colouring and declarations from one source snapshot.
2use crate::EngineError;
3use serde::{Deserialize, Serialize};
4use vvv_core::{Highlight, RelPath, Span, Symbol, SymbolKind};
5
6/// The workspace's visible files, independent of registered languages.
7#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
8#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
9pub struct WorkspaceFilesQuery {}
10
11#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
12#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
13pub struct WorkspaceFiles {
14    pub paths: Vec<RelPath>,
15}
16
17impl WorkspaceFilesQuery {
18    pub fn execute(self, engine: &crate::Engine) -> Result<WorkspaceFiles, EngineError> {
19        let _operation = engine.operation();
20        self.execute_in(engine)
21    }
22    pub(crate) fn execute_in(self, engine: &crate::Engine) -> Result<WorkspaceFiles, EngineError> {
23        let workspace = engine.workspace();
24        let mut paths: Vec<RelPath> = workspace
25            .files()?
26            .into_iter()
27            .map(|path| RelPath::from(workspace.normalize(&path)))
28            .collect();
29        paths.sort();
30        paths.dedup();
31        Ok(WorkspaceFiles { paths })
32    }
33}
34
35impl crate::report::Document {
36    pub(crate) fn workspace_files(files: &WorkspaceFiles) -> Self {
37        let mut document = Self::new();
38        for path in &files.paths {
39            document.body([crate::protocol::display::Line::single(
40                crate::protocol::display::Role::Path,
41                path.to_string(),
42            )]);
43        }
44        document
45    }
46}
47
48/// One file as it is now, with its syntax colouring: what a picker shows.
49#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
50#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
51pub struct FileQuery {
52    pub path: RelPath,
53}
54
55/// One file as it is, with syntax colouring and declaration ranges.
56#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
57#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
58pub struct File {
59    pub path: RelPath,
60    pub text: String,
61    #[serde(default, skip_serializing_if = "Vec::is_empty")]
62    pub highlights: Vec<Highlight>,
63    /// Declarations from the same source snapshot as the text and highlights.
64    #[serde(default, skip_serializing_if = "Vec::is_empty")]
65    pub symbols: Vec<Symbol>,
66    /// Versioned identifier occurrences from this exact source.
67    #[serde(default, skip_serializing_if = "Vec::is_empty")]
68    pub identifiers: Vec<crate::SourceAnchor>,
69}
70
71impl File {
72    /// The nearest declaration of `kind` strictly enclosing this source range.
73    pub fn enclosing(&self, span: Span, kind: SymbolKind) -> Option<&Symbol> {
74        if span.is_empty() || self.text.get(span.start..span.end).is_none() {
75            return None;
76        }
77        self.symbols
78            .iter()
79            .filter(|symbol| {
80                symbol.kind == kind
81                    && self.text.get(symbol.span.start..symbol.span.end).is_some()
82                    && symbol.span != span
83                    && symbol.span.start <= span.start
84                    && span.end <= symbol.span.end
85            })
86            .min_by_key(|symbol| symbol.span.end - symbol.span.start)
87    }
88}
89
90/// One file as it is now, coloured by its language when one claims it.
91impl FileQuery {
92    /// Answer with the concrete result of this query.
93    pub fn execute(self, engine: &crate::Engine) -> Result<File, EngineError> {
94        let _operation = engine.operation();
95        self.execute_in(engine)
96    }
97
98    pub(crate) fn execute_in(self, engine: &crate::Engine) -> Result<File, EngineError> {
99        let path = self.path.as_path();
100        // Always read current contents, even within a session's trusted walk.
101        // Cached parser output proves nothing about the filesystem by itself.
102        let file = engine.workspace().load(path)?;
103        let content = file.content_id();
104        let normalized = RelPath::from(file.path());
105        if let Some(cached) = engine
106            .file_previews
107            .lock()
108            .unwrap_or_else(std::sync::PoisonError::into_inner)
109            .get(&normalized, &content)
110        {
111            return Ok(cached);
112        }
113        let (highlights, symbols, identifiers) = match engine.languages().for_path(path) {
114            Some(language) => {
115                let facts =
116                    file.facts(language.as_ref())
117                        .map_err(|source| EngineError::Search {
118                            path: path.into(),
119                            source,
120                        })?;
121                let identifiers = facts
122                    .tokens()
123                    .map(|(_, _, span)| crate::SourceAnchor {
124                        path: file.path().into(),
125                        content: file.content_id(),
126                        span,
127                    })
128                    .collect();
129                (facts.highlights, facts.symbols, identifiers)
130            }
131            None => (Vec::new(), Vec::new(), Vec::new()),
132        };
133        let preview = File {
134            path: normalized,
135            text: file.text().to_owned(),
136            highlights,
137            symbols,
138            identifiers,
139        };
140        engine
141            .file_previews
142            .lock()
143            .unwrap_or_else(std::sync::PoisonError::into_inner)
144            .insert(&preview, content);
145        Ok(preview)
146    }
147}
148
149/// Parsed file snapshots, independent of graph trust. Keys require a fresh read
150/// and a complete content digest; edits and deletes cannot hit an old snapshot.
151pub(crate) struct FileCache {
152    entries: std::collections::VecDeque<CachedFile>,
153    bytes: usize,
154    max_bytes: usize,
155    max_entries: usize,
156}
157
158struct CachedFile {
159    file: File,
160    content: crate::ContentId,
161    bytes: usize,
162}
163
164impl Default for FileCache {
165    fn default() -> Self {
166        Self {
167            entries: std::collections::VecDeque::new(),
168            bytes: 0,
169            max_bytes: 64 * 1024 * 1024,
170            max_entries: 32,
171        }
172    }
173}
174
175impl FileCache {
176    fn get(&mut self, path: &RelPath, content: &crate::ContentId) -> Option<File> {
177        let index = self
178            .entries
179            .iter()
180            .position(|entry| &entry.file.path == path)?;
181        let entry = self.entries.remove(index)?;
182        if &entry.content != content {
183            self.bytes -= entry.bytes;
184            return None;
185        }
186        let file = entry.file.clone();
187        self.entries.push_back(entry);
188        Some(file)
189    }
190
191    fn insert(&mut self, file: &File, content: crate::ContentId) {
192        if let Some(index) = self
193            .entries
194            .iter()
195            .position(|entry| entry.file.path == file.path)
196            && let Some(entry) = self.entries.remove(index)
197        {
198            self.bytes -= entry.bytes;
199        }
200        let mut charge = FileCharge::default();
201        if serde_json::to_writer(&mut charge, file).is_err() {
202            return;
203        }
204        let bytes = charge.0.saturating_mul(4).saturating_add(256);
205        if bytes > self.max_bytes || self.max_entries == 0 {
206            return;
207        }
208        while self.bytes.saturating_add(bytes) > self.max_bytes
209            || self.entries.len() >= self.max_entries
210        {
211            let Some(entry) = self.entries.pop_front() else {
212                break;
213            };
214            self.bytes -= entry.bytes;
215        }
216        self.bytes += bytes;
217        self.entries.push_back(CachedFile {
218            file: file.clone(),
219            content,
220            bytes,
221        });
222    }
223}
224
225/// Conservative serialized-payload accounting without allocating another copy.
226#[derive(Default)]
227struct FileCharge(usize);
228
229impl std::io::Write for FileCharge {
230    fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
231        self.0 = self.0.saturating_add(bytes.len());
232        Ok(bytes.len())
233    }
234    fn flush(&mut self) -> std::io::Result<()> {
235        Ok(())
236    }
237}
238
239#[cfg(test)]
240mod tests {
241    use super::*;
242
243    #[test]
244    fn cache_evicts_least_recent_files_and_skips_oversized_text_and_metadata() {
245        let mut cache = FileCache {
246            max_bytes: 4_096,
247            max_entries: 2,
248            ..FileCache::default()
249        };
250        let file = |path: &str, text: &str| File {
251            path: path.into(),
252            text: text.into(),
253            highlights: vec![],
254            symbols: vec![],
255            identifiers: vec![],
256        };
257        let content = crate::ContentId::of("small");
258        cache.insert(&file("a.p", "small"), content.clone());
259        cache.insert(&file("b.p", "small"), content.clone());
260        assert!(cache.get(&"a.p".into(), &content).is_some());
261        cache.insert(&file("c.p", "small"), content.clone());
262        assert!(cache.get(&"b.p".into(), &content).is_none());
263        assert!(cache.get(&"a.p".into(), &content).is_some());
264        let large = file("large.p", &"x".repeat(4_096));
265        cache.insert(&large, crate::ContentId::of(&large.text));
266        assert!(
267            cache
268                .get(&large.path, &crate::ContentId::of(&large.text))
269                .is_none()
270        );
271        let mut metadata = file("metadata.p", "small");
272        metadata.identifiers = (0..100)
273            .map(|_| crate::SourceAnchor {
274                path: metadata.path.clone(),
275                content: content.clone(),
276                span: Span::new(0, 1),
277            })
278            .collect();
279        cache.insert(&metadata, content.clone());
280        assert!(cache.get(&metadata.path, &content).is_none());
281        assert!(cache.get(&"a.p".into(), &content).is_some());
282        assert!(cache.get(&"c.p".into(), &content).is_some());
283        cache.max_entries = 32;
284        for i in 0..32 {
285            cache.insert(&file(&format!("{i}.p"), &"x".repeat(100)), content.clone());
286            assert!(cache.bytes <= cache.max_bytes);
287        }
288        assert!(
289            cache.entries.len() < cache.max_entries,
290            "the byte budget also evicts"
291        );
292    }
293}