Skip to main content

brink_db/
db.rs

1use std::collections::HashMap;
2
3use brink_ir::suppressions::{Suppressions, parse_suppressions};
4use brink_ir::{
5    Diagnostic, DiagnosticCode, FileId, HirFile, SymbolManifest, lower, lower_single_knot,
6    lower_top_level,
7};
8use brink_syntax::ast::AstNode as _;
9use brink_syntax::{Parse, parse_with_cache};
10use rowan::{GreenNode, NodeCache};
11use tracing::debug;
12
13use crate::file_state::{FileState, TopLevelEntry};
14use crate::include_graph::IncludeGraph;
15use crate::knot_cache::KnotEntry;
16
17/// Stateful incremental project database.
18///
19/// Caches parsed trees and lowered HIR per file, enabling efficient re-analysis
20/// when individual files change. Both the compiler (one-shot) and LSP
21/// (long-lived) use this as their project model.
22pub struct ProjectDb {
23    files: HashMap<FileId, FileState>,
24    path_to_id: HashMap<String, FileId>,
25    id_to_path: HashMap<FileId, String>,
26    next_id: u32,
27    include_graph: IncludeGraph,
28    node_cache: NodeCache,
29}
30
31impl ProjectDb {
32    /// Create an empty project database.
33    pub fn new() -> Self {
34        Self {
35            files: HashMap::new(),
36            path_to_id: HashMap::new(),
37            id_to_path: HashMap::new(),
38            next_id: 0,
39            include_graph: IncludeGraph::new(),
40            node_cache: NodeCache::default(),
41        }
42    }
43
44    /// Add or replace a file. Performs full parse + lower + cache.
45    pub fn set_file(&mut self, path: &str, source: String) -> FileId {
46        let file_id = self.get_or_create_id(path);
47
48        let parse = parse_with_cache(&source, &mut self.node_cache);
49        let tree = parse.tree();
50
51        // Per-knot lowering
52        let knot_entries: Vec<KnotEntry> = tree
53            .knots()
54            .map(|knot_ast| {
55                let green = knot_ast.syntax().green().into();
56                let offset = knot_ast.syntax().text_range().start();
57                let (knot, manifest, diagnostics) = lower_single_knot(file_id, &knot_ast);
58                KnotEntry {
59                    green,
60                    offset,
61                    knot,
62                    manifest,
63                    diagnostics,
64                }
65            })
66            .collect();
67
68        // Top-level lowering
69        let top_level = Self::lower_top_level_entry(file_id, &tree);
70
71        // Assemble full HirFile and SymbolManifest
72        let (hir, manifest, mut diagnostics) =
73            Self::assemble(file_id, &knot_entries, &top_level, &tree);
74        // Surface parser/syntax errors as compile diagnostics alongside lowering.
75        diagnostics.extend(Self::syntax_diagnostics(file_id, &parse));
76
77        let suppressions = parse_suppressions(&source);
78
79        let state = FileState {
80            source,
81            parse,
82            knot_entries,
83            top_level,
84            hir,
85            manifest,
86            diagnostics,
87            suppressions,
88        };
89
90        // Update include graph
91        let include_ids: Vec<FileId> = state
92            .hir
93            .includes
94            .iter()
95            .filter_map(|inc| {
96                let resolved = resolve_include_path(path, &inc.file_path);
97                self.path_to_id.get(&resolved).copied()
98            })
99            .collect();
100        self.include_graph.update(file_id, include_ids);
101
102        self.files.insert(file_id, state);
103
104        debug!(path, id = file_id.0, "set_file complete");
105        file_id
106    }
107
108    /// Incrementally update a file. Re-parses, diffs knots by green-node
109    /// identity, and only re-lowers changed knots.
110    pub fn update_file(&mut self, path: &str, source: String) -> FileId {
111        let file_id = self.get_or_create_id(path);
112
113        // If the file doesn't exist yet, fall through to set_file
114        if !self.files.contains_key(&file_id) {
115            return self.set_file(path, source);
116        }
117
118        let parse = parse_with_cache(&source, &mut self.node_cache);
119        let tree = parse.tree();
120
121        // Top-level is always re-lowered (cheap relative to knots)
122        let top_level = Self::lower_top_level_entry(file_id, &tree);
123
124        // Diff knots by green-node identity
125        let new_knot_asts: Vec<_> = tree.knots().collect();
126        let old_state = self.files.get(&file_id);
127
128        let mut knot_entries = Vec::with_capacity(new_knot_asts.len());
129        let mut reused = 0u32;
130
131        for (i, knot_ast) in new_knot_asts.iter().enumerate() {
132            let new_green: GreenNode = knot_ast.syntax().green().into();
133
134            let new_offset = knot_ast.syntax().text_range().start();
135            let reuse_entry = old_state
136                .and_then(|s| s.knot_entries.get(i))
137                .filter(|old| old.green == new_green && old.offset == new_offset);
138
139            if let Some(old_entry) = reuse_entry {
140                knot_entries.push(KnotEntry {
141                    green: new_green,
142                    offset: new_offset,
143                    knot: old_entry.knot.clone(),
144                    manifest: old_entry.manifest.clone(),
145                    diagnostics: old_entry.diagnostics.clone(),
146                });
147                reused += 1;
148            } else {
149                let (knot, manifest, diagnostics) = lower_single_knot(file_id, knot_ast);
150                knot_entries.push(KnotEntry {
151                    green: new_green,
152                    offset: new_offset,
153                    knot,
154                    manifest,
155                    diagnostics,
156                });
157            }
158        }
159
160        debug!(
161            path,
162            total = new_knot_asts.len(),
163            reused,
164            "knot diff complete"
165        );
166
167        let (hir, manifest, mut diagnostics) =
168            Self::assemble(file_id, &knot_entries, &top_level, &tree);
169        diagnostics.extend(Self::syntax_diagnostics(file_id, &parse));
170
171        let suppressions = parse_suppressions(&source);
172
173        let state = FileState {
174            source,
175            parse,
176            knot_entries,
177            top_level,
178            hir,
179            manifest,
180            diagnostics,
181            suppressions,
182        };
183
184        // Update include graph
185        let include_ids: Vec<FileId> = state
186            .hir
187            .includes
188            .iter()
189            .filter_map(|inc| {
190                let resolved = resolve_include_path(path, &inc.file_path);
191                self.path_to_id.get(&resolved).copied()
192            })
193            .collect();
194        self.include_graph.update(file_id, include_ids);
195
196        self.files.insert(file_id, state);
197
198        file_id
199    }
200
201    /// Remove a file from the database.
202    pub fn remove_file(&mut self, path: &str) {
203        if let Some(id) = self.path_to_id.remove(path) {
204            self.id_to_path.remove(&id);
205            self.files.remove(&id);
206            self.include_graph.remove(id);
207        }
208    }
209
210    /// Look up a file's ID by path.
211    pub fn file_id(&self, path: &str) -> Option<FileId> {
212        self.path_to_id.get(path).copied()
213    }
214
215    /// Look up a file's path by ID.
216    pub fn file_path(&self, id: FileId) -> Option<&str> {
217        self.id_to_path.get(&id).map(String::as_str)
218    }
219
220    /// Iterate over all registered file IDs.
221    pub fn file_ids(&self) -> impl Iterator<Item = FileId> + '_ {
222        let mut ids: Vec<_> = self.files.keys().copied().collect();
223        ids.sort_by_key(|id| id.0);
224        ids.into_iter()
225    }
226
227    /// Return file IDs in topological include order (included files before
228    /// the files that include them), matching ink's `INCLUDE` paste semantics.
229    pub fn file_ids_topo(&self, entry: FileId) -> Vec<FileId> {
230        let all: Vec<_> = self.files.keys().copied().collect();
231        self.include_graph.topological_order(entry, &all)
232    }
233
234    /// Get the cached parse tree for a file.
235    pub fn parse(&self, id: FileId) -> Option<&Parse> {
236        self.files.get(&id).map(|s| &s.parse)
237    }
238
239    /// Get the cached HIR for a file.
240    pub fn hir(&self, id: FileId) -> Option<&HirFile> {
241        self.files.get(&id).map(|s| &s.hir)
242    }
243
244    /// Get the cached symbol manifest for a file.
245    pub fn manifest(&self, id: FileId) -> Option<&SymbolManifest> {
246        self.files.get(&id).map(|s| &s.manifest)
247    }
248
249    /// Get the source text for a file.
250    pub fn source(&self, id: FileId) -> Option<&str> {
251        self.files.get(&id).map(|s| s.source.as_str())
252    }
253
254    /// Get per-file diagnostics (parse + lowering).
255    pub fn file_diagnostics(&self, id: FileId) -> Option<&[Diagnostic]> {
256        self.files.get(&id).map(|s| s.diagnostics.as_slice())
257    }
258
259    /// Get parsed suppression directives for a file.
260    pub fn suppressions(&self, id: FileId) -> Option<&Suppressions> {
261        self.files.get(&id).map(|s| &s.suppressions)
262    }
263
264    /// Rebuild include graph edges for all files.
265    ///
266    /// Must be called after batch-loading files (e.g. workspace discovery)
267    /// because `set_file` can only create edges to files already in the db.
268    /// Files loaded before their include targets will have missing edges.
269    pub fn rebuild_include_graph(&mut self) {
270        let file_list: Vec<(FileId, String)> = self
271            .files
272            .keys()
273            .filter_map(|&id| self.id_to_path.get(&id).map(|p| (id, p.clone())))
274            .collect();
275
276        for (file_id, file_path) in &file_list {
277            if let Some(state) = self.files.get(file_id) {
278                let include_ids: Vec<FileId> = state
279                    .hir
280                    .includes
281                    .iter()
282                    .filter_map(|inc| {
283                        let resolved = resolve_include_path(file_path, &inc.file_path);
284                        self.path_to_id.get(&resolved).copied()
285                    })
286                    .collect();
287                self.include_graph.update(*file_id, include_ids);
288            }
289        }
290    }
291
292    /// Detect cycles in the include graph.
293    ///
294    /// Returns the first cycle found as an ordered path of file IDs.
295    pub fn find_cycle(&self) -> Option<Vec<FileId>> {
296        self.include_graph.find_cycle()
297    }
298
299    /// Compute independent projects from include relationships.
300    ///
301    /// Returns `(root, members)` pairs sorted by root `FileId`.
302    pub fn compute_projects(&self) -> Vec<(FileId, Vec<FileId>)> {
303        let all: Vec<_> = self.files.keys().copied().collect();
304        self.include_graph.compute_projects(&all)
305    }
306
307    /// Snapshot analysis inputs for a subset of files.
308    ///
309    /// Like `analysis_inputs()` but filtered to the given set.
310    pub fn analysis_inputs_for(
311        &self,
312        file_ids: &[FileId],
313    ) -> Vec<(FileId, HirFile, SymbolManifest)> {
314        let mut inputs: Vec<_> = file_ids
315            .iter()
316            .filter_map(|&id| {
317                let state = self.files.get(&id)?;
318                Some((id, state.hir.clone(), state.manifest.clone()))
319            })
320            .collect();
321        inputs.sort_by_key(|(id, _, _)| id.0);
322        inputs
323    }
324
325    /// Snapshot all analysis inputs for background analysis.
326    ///
327    /// Returns `(FileId, HirFile, SymbolManifest)` tuples cloned out of the db,
328    /// so the caller can run `brink_analyzer::analyze()` without holding the lock.
329    pub fn analysis_inputs(&self) -> Vec<(FileId, HirFile, SymbolManifest)> {
330        let mut inputs: Vec<_> = self
331            .files
332            .iter()
333            .map(|(&id, state)| (id, state.hir.clone(), state.manifest.clone()))
334            .collect();
335        inputs.sort_by_key(|(id, _, _)| id.0);
336        inputs
337    }
338
339    /// Snapshot file metadata for diagnostic publishing.
340    ///
341    /// Returns `(FileId, path, source)` tuples for all files in the db.
342    pub fn file_metadata(&self) -> Vec<(FileId, String, String)> {
343        let mut meta: Vec<_> = self
344            .files
345            .keys()
346            .filter_map(|&id| {
347                let path = self.id_to_path.get(&id)?.clone();
348                let source = self.files.get(&id)?.source.clone();
349                Some((id, path, source))
350            })
351            .collect();
352        meta.sort_by_key(|(id, _, _)| id.0);
353        meta
354    }
355
356    // ── Internal helpers ──────────────────────────────────────────────
357
358    fn get_or_create_id(&mut self, path: &str) -> FileId {
359        if let Some(&id) = self.path_to_id.get(path) {
360            return id;
361        }
362        let id = FileId(self.next_id);
363        self.next_id += 1;
364        self.path_to_id.insert(path.to_string(), id);
365        self.id_to_path.insert(id, path.to_string());
366        id
367    }
368
369    fn lower_top_level_entry(
370        file_id: FileId,
371        tree: &brink_syntax::ast::SourceFile,
372    ) -> TopLevelEntry {
373        let green_children = Self::collect_top_level_green(tree);
374        let (root_content, top_level_knots, manifest, diagnostics) = lower_top_level(file_id, tree);
375        TopLevelEntry {
376            green_children,
377            root_content,
378            top_level_knots,
379            manifest,
380            diagnostics,
381        }
382    }
383
384    /// Collect green nodes of non-knot direct children for diffing.
385    fn collect_top_level_green(tree: &brink_syntax::ast::SourceFile) -> Vec<GreenNode> {
386        use brink_syntax::SyntaxKind;
387
388        tree.syntax()
389            .children()
390            .filter(|child| child.kind() != SyntaxKind::KNOT_DEF)
391            .map(|child| child.green().into())
392            .collect()
393    }
394
395    /// Assemble a complete `HirFile` and `SymbolManifest` from cached pieces.
396    fn assemble(
397        file_id: FileId,
398        knot_entries: &[KnotEntry],
399        top_level: &TopLevelEntry,
400        tree: &brink_syntax::ast::SourceFile,
401    ) -> (HirFile, SymbolManifest, Vec<Diagnostic>) {
402        // We need declarations from the full lower to build HirFile.
403        // lower_top_level only returns (Block, SymbolManifest, diagnostics).
404        // For the declarations (variables, constants, lists, externals, includes),
405        // we need to call `lower()` or extract them from the AST.
406        //
407        // Approach: use `lower()` to get the full HirFile, then replace knots
408        // with our cached versions. This means top-level lowering happens twice
409        // on change, but it's simple and correct.
410        let (mut full_hir, _full_manifest, _full_diag) = lower(file_id, tree);
411
412        // Replace knots with our cached (possibly reused) versions,
413        // plus any top-level stitches promoted to knots.
414        full_hir.knots = knot_entries.iter().filter_map(|e| e.knot.clone()).collect();
415        full_hir.knots.extend(top_level.top_level_knots.clone());
416        full_hir.root_content = top_level.root_content.clone();
417
418        // Merge manifests: top-level + all knots
419        let mut manifest = top_level.manifest.clone();
420        for entry in knot_entries {
421            merge_manifest_into(&mut manifest, &entry.manifest);
422        }
423
424        // Merge diagnostics
425        let mut diagnostics = top_level.diagnostics.clone();
426        for entry in knot_entries {
427            diagnostics.extend(entry.diagnostics.iter().cloned());
428        }
429
430        (full_hir, manifest, diagnostics)
431    }
432
433    /// Convert parser/syntax errors into compile diagnostics (`E037`), so
434    /// malformed source fails the compile instead of being silently ignored.
435    fn syntax_diagnostics(file_id: FileId, parse: &Parse) -> Vec<Diagnostic> {
436        parse
437            .errors()
438            .iter()
439            .map(|e| Diagnostic {
440                file: file_id,
441                range: e.range,
442                message: e.message.clone(),
443                code: DiagnosticCode::E037,
444            })
445            .collect()
446    }
447}
448
449impl Default for ProjectDb {
450    fn default() -> Self {
451        Self::new()
452    }
453}
454
455/// Merge `src` manifest fields into `dst`.
456fn merge_manifest_into(dst: &mut SymbolManifest, src: &SymbolManifest) {
457    dst.knots.extend(src.knots.iter().cloned());
458    dst.stitches.extend(src.stitches.iter().cloned());
459    dst.variables.extend(src.variables.iter().cloned());
460    dst.constants.extend(src.constants.iter().cloned());
461    dst.lists.extend(src.lists.iter().cloned());
462    dst.externals.extend(src.externals.iter().cloned());
463    dst.labels.extend(src.labels.iter().cloned());
464    dst.list_items.extend(src.list_items.iter().cloned());
465    dst.locals.extend(src.locals.iter().cloned());
466    dst.unresolved.extend(src.unresolved.iter().cloned());
467    dst.docs
468        .extend(src.docs.iter().map(|(k, v)| (k.clone(), v.clone())));
469}
470
471/// Resolve an INCLUDE path relative to the including file's directory.
472///
473/// Uses string-based path manipulation (`rfind('/')`) rather than
474/// `std::path::Path` to avoid platform-specific separator issues and
475/// to work in WASM contexts. The joined path is normalized so `.`/`..`
476/// segments collapse to a clean project-relative key (e.g.
477/// `a/b/../d.ink` → `a/d.ink`) — consistent across the compiler, runtime,
478/// and IDE so upward-relative includes resolve to real files.
479pub fn resolve_include_path(from_file: &str, include_path: &str) -> String {
480    let joined = match from_file.rfind('/') {
481        Some(i) => format!("{}/{include_path}", &from_file[..i]),
482        None => include_path.to_string(),
483    };
484    normalize_path(&joined)
485}
486
487/// Collapse `.` and `..` segments in a `/`-separated path. A `..` pops the
488/// previous real segment; a `..` with nothing to pop (or above an existing
489/// `..`) is kept literally rather than escaping the root. A leading `/`
490/// (absolute path) is preserved — the test harness and disk-backed compiles
491/// resolve against absolute filesystem paths.
492fn normalize_path(path: &str) -> String {
493    let absolute = path.starts_with('/');
494    let mut out: Vec<&str> = Vec::new();
495    for seg in path.split('/') {
496        match seg {
497            "" | "." => {}
498            ".." if matches!(out.last(), Some(&s) if s != "..") => {
499                out.pop();
500            }
501            s => out.push(s),
502        }
503    }
504    let joined = out.join("/");
505    if absolute {
506        format!("/{joined}")
507    } else {
508        joined
509    }
510}
511
512/// Compute the relative INCLUDE target to reach `to_file` from `from_file`'s
513/// directory — the inverse of [`resolve_include_path`]:
514/// `normalize(resolve_include_path(from_file, compute_relative_path(from_file, to_file))) == to_file`
515/// for both forward and `..`-traversing layouts. Used when a file is
516/// renamed/moved to rewrite every `INCLUDE` that points at it (and the moved
517/// file's own includes).
518pub fn compute_relative_path(from_file: &str, to_file: &str) -> String {
519    let mut from_dirs: Vec<&str> = from_file.split('/').collect();
520    from_dirs.pop(); // drop the including file's own name
521    let to_all: Vec<&str> = to_file.split('/').collect();
522    let Some((to_name, to_dirs)) = to_all.split_last() else {
523        return to_file.to_owned();
524    };
525
526    // Longest common directory prefix.
527    let mut k = 0;
528    while k < from_dirs.len() && k < to_dirs.len() && from_dirs[k] == to_dirs[k] {
529        k += 1;
530    }
531
532    let mut parts: Vec<&str> = Vec::new();
533    parts.extend(std::iter::repeat_n("..", from_dirs.len() - k));
534    parts.extend_from_slice(&to_dirs[k..]);
535    parts.push(to_name);
536    parts.join("/")
537}
538
539#[cfg(test)]
540mod path_tests {
541    use super::{compute_relative_path, resolve_include_path};
542
543    #[test]
544    fn resolve_forward_includes() {
545        assert_eq!(
546            resolve_include_path("src/main.ink", "utils.ink"),
547            "src/utils.ink"
548        );
549        assert_eq!(resolve_include_path("story.ink", "other.ink"), "other.ink");
550        assert_eq!(resolve_include_path("a/b/c.ink", "d/e.ink"), "a/b/d/e.ink");
551    }
552
553    #[test]
554    fn resolve_normalizes_dot_and_dotdot() {
555        assert_eq!(resolve_include_path("a/b/c.ink", "../d.ink"), "a/d.ink");
556        assert_eq!(resolve_include_path("a/b/c.ink", "./d.ink"), "a/b/d.ink");
557        assert_eq!(resolve_include_path("a/b/c.ink", "../../d.ink"), "d.ink");
558        assert_eq!(
559            resolve_include_path("a/b/c.ink", "../x/../d.ink"),
560            "a/d.ink"
561        );
562    }
563
564    #[test]
565    fn compute_relative_is_inverse_of_resolve() {
566        // (from including file, target file) round-trips through resolve.
567        let cases = [
568            ("main.ink", "scenes/intro.ink"), // move into a subdir
569            ("a/b/c.ink", "a/d.ink"),         // sibling dir (needs ..)
570            ("a/b/c.ink", "a/b/renamed.ink"), // rename in place
571            ("scenes/intro.ink", "lib.ink"),  // up to root (needs ..)
572            ("a/b/c.ink", "x/y/z.ink"),       // fully divergent
573            ("main.ink", "other.ink"),        // both at root
574        ];
575        for (from, to) in cases {
576            let rel = compute_relative_path(from, to);
577            assert_eq!(
578                resolve_include_path(from, &rel),
579                to,
580                "round-trip failed for from={from} to={to} rel={rel}",
581            );
582        }
583    }
584
585    #[test]
586    fn resolve_preserves_absolute_paths() {
587        // The test harness / disk-backed compiles pass absolute paths — the
588        // leading slash must survive normalization.
589        assert_eq!(
590            resolve_include_path("/proj/tier3/main.ink", "included.ink"),
591            "/proj/tier3/included.ink",
592        );
593        assert_eq!(
594            resolve_include_path("/proj/a/b/c.ink", "../d.ink"),
595            "/proj/a/d.ink"
596        );
597    }
598
599    #[test]
600    fn compute_relative_rename_in_place_is_bare_name() {
601        assert_eq!(
602            compute_relative_path("a/b/c.ink", "a/b/renamed.ink"),
603            "renamed.ink"
604        );
605        assert_eq!(
606            compute_relative_path("main.ink", "renamed.ink"),
607            "renamed.ink"
608        );
609    }
610}